All pastes #12568 Raw Edit

Stuff

public text v1 · immutable
#12568 ·published 2005-05-25 01:26 UTC
rendered paste body
using System;
using System.Net;
using System.Net.Sockets;

namespace testserver
{
	public class test
	{
		[STAThread]
		public static void Main(string[] args)
		{
			BaseServer test = new BaseServer();
			
			test.Start();
			
			while(true) { }
		}
	}
	
	
	public class BaseServer
	{
		
		protected Socket m_listen;

		public BaseServer()
		{
			m_asyncAcceptCallback = new AsyncCallback(AcceptCallback);
		}


		public virtual bool InitSocket()
		{
			try
			{
				m_listen = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
				m_listen.Bind(new IPEndPoint(IPAddress.Any, 10300));
			}
			catch
			{
				return false;
			}
			return true;
		}

		public virtual bool Start()
		{

			if(m_listen == null && !InitSocket())
				return false;

			try
			{
				m_listen.Listen(100);
				m_listen.BeginAccept(m_asyncAcceptCallback, this);
			}
			catch 
			{
				if (m_listen != null)
					m_listen.Close();
				return false;
			}
			return true;
		}

		private readonly AsyncCallback m_asyncAcceptCallback;

		private void AcceptCallback(IAsyncResult ar)
		{
			try
			{
				Socket sock = m_listen.EndAccept(ar);

				try
				{
					Console.WriteLine("It worked!");
				
				}
				catch
				{
				}
			}
			catch {}
			finally
			{
				m_listen.BeginAccept(m_asyncAcceptCallback, this);
			}
		}

	}
}