예제 #1
0
        public static async Task <int> Main(string[] args)
        {
            var port = 52217;

            var socketHandler = new SocketHandler();
            await socketHandler.Run(port);

            return(0);
        }
예제 #2
0
        public void StartServer(string address, int maxClients, int port)
        {
            ThreadPool.SetMaxThreads(maxClients, maxClients + 1);
            // TODO: implement using anything but localhost
            IPAddress  ipAddress        = IPAddress.Parse(address);
            IPEndPoint localEndPoint    = new IPEndPoint(ipAddress, port);
            IPEndPoint localEndPointUDP = new IPEndPoint(ipAddress, port);

            // Create a TCP/IP socket.
            Socket listener = new Socket(ipAddress.AddressFamily,
                                         SocketType.Stream, ProtocolType.Tcp);

            Socket UDPsocket = new Socket(ipAddress.AddressFamily,
                                          SocketType.Dgram, ProtocolType.Udp);

            UDPsocket.ExclusiveAddressUse = false;
            ThreadPool.QueueUserWorkItem(new WaitCallback(UDPDeMultiplexer), UDPsocket);

            try
            {
                listener.Bind(localEndPoint);
                listener.Listen(maxClients);
                UDPsocket.Bind(localEndPointUDP);

                // Start listening for connections.
                while (true)
                {
                    Console.WriteLine("Waiting for a connection...");
                    Socket openConnection = listener.Accept();
                    // TODO: Refactor handler,make it a method maybe
                    SocketHandler socketHandler = new SocketHandler(openConnection, UDPsocket, this);
                    ThreadPool.QueueUserWorkItem(new WaitCallback(socketHandler.Start));
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }