简体   繁体   English

UDP客户端的数据包分类

[英]UDP client's packet categorization

I've created my own UDP server/client to see how client->server comunication works and i was wondering if i can make the server to read a specific value... For example, i have a login form that sends ID & password to the UDP server. 我创建了自己的UDP服务器/客户端,以查看客户端->服务器通讯的工作方式,我想知道是否可以让服务器读取特定值...例如,我有一个发送ID和密码的登录表单到UDP服务器。 How can i make the UDP server to recognize the packet that contains the id/password ? 如何使UDP服务器识别包含ID /密码的数据包? A friend told me that you can set a "packet header" in C/C++ but not in C#. 一位朋友告诉我,您可以在C / C ++中设置“数据包头”,但不能在C#中设置。

Some code examples or ideas would be greate! 一些代码示例或想法会很棒!

My UDP server's code: 我的UDP服务器的代码:

 Configuration _conf = Configuration.Load("realmConfig.lua");
        int realmPort = _conf["AUTH"]["authPort"].GetValue<int>();

       string data = "";

    UdpClient __AUTH__ = new UdpClient(realmPort);


    IPEndPoint remoteIPEndPoint = new IPEndPoint(IPAddress.Any, 0);


    Console.WriteLine(" S E R V E R   IS   S T A R T E D ");
    Console.WriteLine("* Waiting for Client...");
    while (data != "q")
    {
        byte[] receivedBytes = __AUTH__.Receive(ref remoteIPEndPoint);
        data = Encoding.ASCII.GetString(receivedBytes);
        Console.WriteLine("Handling client at " + remoteIPEndPoint + " - ");
        Console.WriteLine("Message Received " + data.TrimEnd());

        __AUTH__.Send(receivedBytes, receivedBytes.Length,remoteIPEndPoint);
        Console.WriteLine("Message Echoed to" + remoteIPEndPoint + data);
    }

Client: 客户:

string data = "";
            byte[] sendBytes = new Byte[1024];
            byte[] rcvPacket = new Byte[1024];
            UdpClient client = new UdpClient();
            IPAddress address = IPAddress.Parse(IPAddress.Broadcast.ToString());
            client.Connect(address, 15000);
            IPEndPoint remoteIPEndPoint = new IPEndPoint(IPAddress.Any, 0);

            Console.WriteLine("Client is Started");
            Console.WriteLine("Type your message");

            while (data != "q")
            {
                data = Console.ReadLine();
                sendBytes = Encoding.ASCII.GetBytes(DateTime.Now.ToString() + " " + data);
                client.Send(sendBytes, sendBytes.GetLength(0));
                rcvPacket = client.Receive(ref remoteIPEndPoint);

                string rcvData = Encoding.ASCII.GetString(rcvPacket);
                Console.WriteLine("Handling client at " + remoteIPEndPoint + " - ");

                Console.WriteLine("Message Received: " + rcvPacket.ToString());
            }
            Console.WriteLine("Close Port Command Sent");  //user feedback
            Console.ReadLine();
            client.Close();  //close connection

The UDP/IP packet headers are used for network and transport purposes. UDP / IP数据包头用于网络和传输目的。 All of your application information should be in the UDP payload. 您的所有应用程序信息都应在UDP有效负载中。 You can put any bytes you want there, using any structure you want. 您可以使用所需的任何结构将所需的任何字节放在此处。 Think of the payload as a very small file which you are passing from one application to another and structure it the same way you would a file. 将有效负载视为一个很小的文件,您将从一个应用程序传递到另一个应用程序,并以与文件相同的方式对其进行结构化。 For example, the first byte might be a number indicating the type of data in the rest of the payload. 例如,第一个字节可能是一个数字,指示净荷其余部分中的数据类型。 In this way, you create your own application-level header. 这样,您可以创建自己的应用程序级标头。

Just as with a file, you need to remember that word alignment, byte packing, and endian may not be same on different machines. 与文件一样,您需要记住,字对齐,字节打包和字节序在不同的机器上可能并不相同。 You are sending a sequence of raw bytes, and need to pay attention to how higher level structures will be converted and interpreted. 您正在发送一系列原始字节,并且需要注意如何转换和解释更高级别的结构。

In addition, individual UDP datagrams are very limited in size. 此外,单个UDP数据报的大小非常有限。 On most networks you will have problems with payloads much bigger than about 1400 bytes, and it is safest to keep payloads below about 512 bytes. 在大多数网络上,有效载荷要远大于约1400字节,这是有问题的,将有效载荷保持在约512字节以下是最安全的。

As always with UDP, remember that you are responsible for all flow control and error recovery. 与UDP一样,请记住您负责所有流控制和错误恢复。 If a packet is lost for any reason, you will receive no notification: it simply fails to arrive. 如果数据包由于任何原因丢失,您将不会收到任何通知:它只是无法到达。 If you send packets too quickly, they will be lost. 如果发送数据包的速度太快,它们将丢失。 If you do not intend to implement your own specialized flow control and error recovery, consider using TCP instead. 如果您不打算实现自己的专用流控制和错误恢复,请考虑改用TCP。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM