none
Regaring UDP communication implementation RRS feed

  • Question

  • hello,

    I have one requirement it a client-server application here server is hardware.client is a windows application.

    the communication protocol used is UDP. Here Client has to send a request to the server after processing the request server has to send the response back to the client
    once the client receives the response client has to send an acknowledgment message to the server. This is for preventing data lose.

    here I want to simulate the hardware using a UDP server(Windows application).
    once client sends request it has to wait for getting response.
    Can anyone suggest a good link for my prototype work?

    Coding.....................................

    Saturday, August 24, 2019 9:33 AM

All replies

  • Hi,
    Socket programming is supported by most of the languages in today's world. I don't know the actual hardware, which you are talking about. They must have a listener on specific port.

    a) You need to connect.

    b) Then send your data.

    c) Receive reply and parse.

    Here is an quick example in Github.


    Avik Das

    Saturday, August 24, 2019 10:58 AM
  • Regarding how to implement the acknowledgment mechanism.

    You'll need to send certain identifier with your application, then push the identifier and the timestamp on a Collection (i.e.: List<T> or other array/hashtable kind of storage).

    On the receiving side, it should return your designed ACK message with that identifier.

    On the receiving side of your application, once your message parser received a ACK message, it will remove the corresponding item from the Collection.

    Then you have a Timer periodically check the Collection for items that has timestamp older than threshold. In that case, either resend the message or prompt for error. If you choose to resend message, depending on your need you also need to save the original message to the Collection or to save the kind/source of message if you would rather send updated content of that message instead.

    If you just want to throw error no matter want, the Collection may only contain the identifer of remote server, and the latest identifier (if you use ever-incrementing 64-bit number as identifier)

    Tuesday, August 27, 2019 1:21 AM
    Answerer
  • OK,Thanks...i tried this,i am ablle to send data to the device after processing the requiest a response is sent to the program.But i am not able to recieve that message.

    I am using UdpClient class for my program.

    I pasted my code snippet to below.

    i am sending message to the device ip address and port number,then device has to send the response from which ip address and port they recieved.I have created a sample application for testing this there its working.

    But in real environ ment its not working i checked this using packet sniffer there i can see tthe device is sending the message to the port but not able to recieve.I think device is recieved some port called 'x' but after processing the request they are sending the response to some other port 'y'.So i cannot read this message.

    How can i get this message? Is there any binding mechanism i missed in client side.plz help me to resolve this issue.

    Client side                          


        byte[] data = Encoding.ASCII.GetBytes("Hello" + DateTime.Now);
                byte[] d = new byte[1024];

                IPEndPoint ep = new IPEndPoint(ip, 11000);
                //  Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                System.Net.Sockets.UdpClient u = new System.Net.Sockets.UdpClient(AddressFamily.InterNetwork);
                u.Connect(ep);
                while (true)
                {


                if (u.Client.Connected)
                {

                    myRemote = u.Client.RemoteEndPoint;

                }
                else
                    myRemote = ep;// new IPEndPoint(ip, 12000);
                // client.Bind(ep);

                //var s = client.Connected;
                EndPoint e = myRemote;

                u.Client.SendTo(data, SocketFlags.None, e);
                //Console.WriteLine("Data sent1  {0} - {1}:", ep.GetHashCode().ToString(), Encoding.ASCII.GetString(d, 0, d.Length));

                u.Client.ReceiveFrom(d, SocketFlags.None, ref e);

    Server side(Actual device)  its used for simulating my device

                int recv;
                byte[] data = new byte[1024];
                       var host = Dns.GetHostName();
                var ip = Dns.GetHostEntry(host).AddressList[0];//Here m using ip address only.its a sample
                IPEndPoint ep = new IPEndPoint(ip, listenPort);
                Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                socket.Bind(ep);  here m binding the endpoint
                Console.WriteLine("Waiting for a client");


                EndPoint remote = new IPEndPoint(ip, 11000);


                recv = socket.ReceiveFrom(data, ref remote);
                var data1 = ASCIIEncoding.ASCII.GetBytes("Hi client1");


    Coding.....................................


    • Edited by La07K Friday, September 20, 2019 8:14 AM
    Friday, September 20, 2019 7:57 AM
  • OK,Thanks...i tried this,i am ablle to send data to the device after processing the requiest a response is sent to the program.But i am not able to recieve that message.

    I am using UdpClient class for my program.

    I pasted my code snippet to below.

    i am sending message to the device ip address and port number,then device has to send the response from which ip address and port they recieved.I have created a sample application for testing this there its working.

    But in real environ ment its not working i checked this using packet sniffer there i can see tthe device is sending the message to the port but not able to recieve.I think device is recieved some port called 'x' but after processing the request they are sending the response to some other port 'y'.So i cannot read this message.

    How can i get this message? Is there any binding mechanism i missed in client side.plz help me to resolve this issue.

    Client side                          

        byte[] data = Encoding.ASCII.GetBytes("Hello" + DateTime.Now);
                byte[] d = new byte[1024];

                IPEndPoint ep = new IPEndPoint(ip, 11000);
                //  Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                System.Net.Sockets.UdpClient u = new System.Net.Sockets.UdpClient(AddressFamily.InterNetwork);
                u.Connect(ep);
                while (true)
                {


                if (u.Client.Connected)
                {

                    myRemote = u.Client.RemoteEndPoint;

                }
                else
                    myRemote = ep;// new IPEndPoint(ip, 12000);
                // client.Bind(ep);

                //var s = client.Connected;
                EndPoint e = myRemote;

                u.Client.SendTo(data, SocketFlags.None, e);
                //Console.WriteLine("Data sent1  {0} - {1}:", ep.GetHashCode().ToString(), Encoding.ASCII.GetString(d, 0, d.Length));

                u.Client.ReceiveFrom(d, SocketFlags.None, ref e);

    Server side(Actual device)  its used for simulating my device

                int recv;
                byte[] data = new byte[1024];
                var host = Dns.GetHostName();
                var ip = Dns.GetHostEntry(host).AddressList[0];//Here m using ip address only.its a sample
                IPEndPoint ep = new IPEndPoint(ip, listenPort);
                Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                socket.Bind(ep);  here m binding the endpoint
                Console.WriteLine("Waiting for a client");


                EndPoint remote = new IPEndPoint(ip, 11000);


                recv = socket.ReceiveFrom(data, ref remote);
                var data1 = ASCIIEncoding.ASCII.GetBytes("Hi client1");


    Coding.....................................


    • Edited by La07K Friday, September 20, 2019 8:13 AM
    Friday, September 20, 2019 7:57 AM