Thread StartSelectServer(out UInt16 tcpListenPort, out UInt16 udpReceivePort) { IPEndPoint listenEndPoint = new IPEndPoint(IPAddress.Any, 0); Socket udpReceiveSock = new Socket(listenEndPoint.AddressFamily, SocketType.Dgram, ProtocolType.Udp); udpReceiveSock.Bind(listenEndPoint); udpReceivePort = (UInt16)((IPEndPoint)udpReceiveSock.LocalEndPoint).Port; Socket tcpListenSock = new Socket(listenEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp); tcpListenSock.Bind(listenEndPoint); tcpListenPort = (UInt16)((IPEndPoint)tcpListenSock.LocalEndPoint).Port; tcpListenSock.Listen(8); var server = new SharedArraySelectServer(); server.AddReceiveSocket(udpReceiveSock, UdpReceiveHandler); server.AddListenSocket(tcpListenSock, TcpAcceptHandler); Thread serverThread = new Thread(() => { server.Run(); }); serverThread.Name = "SelectServer"; serverThread.Start(); return(serverThread); }
void TcpAcceptHandler(SharedArraySelectServer server, Socket sock) { Socket newSock = sock.Accept(); Console.WriteLine("[SelectServer] Accepted new connection from '{0}'", newSock.RemoteEndPoint); server.AddReceiveSocket(newSock, TcpReceiveHandler); }