示例#1
0
        private void AcceptCallback(IAsyncResult result)
        {
            try
            {
                int id;
                lock (this)
                {
                    id = ++_server.Id;
                }

                Console.WriteLine("Connection " + _server.Id + " has just connected on " + _ip + " - " + _port);
                Connection connection = new Connection(_server, id, _ip, _port);
                connection.ConnectionSocket = _serverSocket.EndAccept(result);
                StartTimer(connection);
                connection.Receive();
                Accept();
            }
            catch (SocketException socketException)
            {
                Console.WriteLine(socketException.ToString());
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.ToString());
            }
        }
示例#2
0
        //static string host = "172.26.187.156";

        static async void AcceptLoop()
        {
            CancellationToken token = source.Token;

            while (!token.IsCancellationRequested)
            {
                Connection newconn = await listener.AcceptAsync(token);

                queue.Enqueue(newconn);
                _ = Task.Run(() =>
                {
                    while (true)
                    {
                        foreach (var newconn in queue)
                        {
                            try
                            {
                                byte[] data = newconn.Receive();
                                byte[] buff = new byte[800 * 1000];
                                Buffer.BlockCopy(data, 0, buff, 0, data.Length);
                                newconn.SendBytes(buff, SendOption.FragmentedReliable);
                            }
                            catch (Exception)
                            {
                            }
                            //Console.WriteLine(Encoding.UTF8.GetString(data));
                        }
                    }
                });
            }
        }