Beispiel #1
0
        public static void Listener()
        {
            Console.WriteLine($"Сервер начал работу на {listenPoint.Address}:{listenPoint.Port}.");

            try
            {
                listenSocket.Bind(listenPoint);
                while (true)
                {
                    byte[]     data    = new byte[1024];
                    int        len     = listenSocket.Receive(data);
                    TcpSegment message = TcpSegment.FromByteArray(TcpSegment.GetBytes(data, len));
                    if (message.destinationPort != Config.ServerListenPort)
                    {
                        continue;
                    }
                    else if (Established.Contains(message.sourcePort))
                    {
                        continue;
                    }
                    else if (SynRecived.Contains(message.sourcePort) && message.ack)
                    {
                        AddToConnected(message);
                    }
                    else if (!SynRecived.Contains(message.sourcePort) && message.syn)
                    {
                        AddToSynReceived(message);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(ex.Message);
                Console.ResetColor();
            }
            finally
            {
                listenSocket.Close();
            }
        }
Beispiel #2
0
        private static void Run()
        {
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            try
            {
                socket.Bind(new IPEndPoint(IPAddress.Parse(Config.host), 6660));
                for (int i = 0; i < count; i++)
                {
                    TcpSegment tcp = new TcpSegment(6661 + i, Config.ServerListenPort, syn: true);
                    socket.SendTo(tcp.ToByteArray(), server);
                    byte[] data    = new byte[1024];
                    Socket socket1 = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                    try
                    {
                        socket1.Bind(new IPEndPoint(IPAddress.Parse(Config.host), 6661 + i));
                        var count = socket1.Receive(data);
                        var msg   = TcpSegment.FromByteArray(TcpSegment.GetBytes(data, count));
                        var re    = new TcpSegment(msg.destinationPort, msg.sourcePort, ack: true);
                        socket1.SendTo(re.ToByteArray(), server);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    finally
                    {
                        socket1.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                socket.Close();
            }
        }
Beispiel #3
0
        public static void Run()
        {
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            try
            {
                TcpSegment tcp = new TcpSegment(6661, Config.ServerListenPort, syn: true);
                socket.Bind(new IPEndPoint(IPAddress.Parse(Config.host), 6661));

                for (int i = Config.ServerListenPort - 10; i <= Config.ServerListenPort; i++)
                {
                    var point = new IPEndPoint(IPAddress.Parse(Config.host), Config.ServerListenPort);

                    tcp.destinationPort = i;
                    point.Port          = i;
                    socket.SendTo(tcp.ToByteArray(), point);
                }
                socket.Close();

                socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                socket.Bind(new IPEndPoint(IPAddress.Parse(Config.host), 6661));

                byte[] data    = new byte[1024];
                int    len     = socket.Receive(data);
                var    message = TcpSegment.FromByteArray(TcpSegment.GetBytes(data, len));

                Console.WriteLine($"syn = {message.syn}, ack = {message.ack} on {message.sourcePort}.");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                socket.Close();
            }
        }