Frage

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. How can i make the UDP server to recognize the packet that contains the id/password ? A friend told me that you can set a "packet header" in C/C++ but not in C#.

Some code examples or ideas would be greate!

My UDP server's code:

 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
War es hilfreich?

Lösung

The UDP/IP packet headers are used for network and transport purposes. All of your application information should be in the UDP payload. 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. 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.

As always with UDP, remember that you are responsible for all flow control and error recovery. 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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top