Search code examples
c#socketsudp

UDP client not receiving data


I have a problem communicating with a UDP device

public IPEndPoint sendEndPoint;
        public void senderUdpClient(byte message_Type, byte Command_Class, byte command_code,int argument1, int argument2)
        {
            string serverIP = "192.168.2.11";
            int sendPort = 40960;
            int receivePort = 40963;
            // Calcul CheckSum
            // We know the message plus the checksum has length 12
            var packedMessage2 = new byte[12];
            var packedMessage_hex = new byte[12];

            // We use the new Span feature
            var span = new Span<byte>(packedMessage2);
            // We can directly set the single bytes
            span[0] = message_Type;
            span[1] = Command_Class;
            span[2] = command_code;
            // The pack is <, so little endian. Note the use of Slice: first the position (3 or 7), then the length of the data (4 for int)
            BinaryPrimitives.WriteInt32LittleEndian(span.Slice(3, 4), argument1);
            BinaryPrimitives.WriteInt32LittleEndian(span.Slice(7, 4), argument2);
            // The checksum
            // The sum is modulo 255, because it is a single byte.
            // the unchecked is normally useless because it is standard in C#, but we write it to make it clear
            var sum = unchecked((byte)packedMessage2.Take(11).Sum(x => x));
            // We set the sum
            span[11] = sum;
             
            // Without checksum
            Console.WriteLine(string.Concat(packedMessage2.Take(11).Select(x => $@"\x{x:x2}")));
            // With checksum
            Console.WriteLine(string.Concat(packedMessage2.Select(x => $@"\x{x:x2}")));
            Console.WriteLine(string.Concat(packedMessage2.Take(1).Select(x => $@"\x{x:x2}")));
            UdpClient senderClient = new UdpClient();
            sendEndPoint = new IPEndPoint(IPAddress.Parse(serverIP), sendPort);
            try
            {
                senderClient.Connect(this.sendEndPoint);
                senderClient.Send(packedMessage2, packedMessage2.Length);
                //IPEndPoint object will allow us to read datagrams sent from any source.
                IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Parse(serverIP), receivePort);
                 Thread.Sleep(5000);
                 // Blocks until a message returns on this socket from a remote host.
                 Byte[] receiveBytes = senderClient.Receive(ref RemoteIpEndPoint);
                 string returnData = Encoding.ASCII.GetString(receiveBytes);
                senderClient.Close();
                MessageBox.Show("Message Sent");
            }
            catch (Exception err)
            {
                MessageBox.Show(err.ToString());
            }
        }

Form1

    ObjHundler.senderUdpClient(1, 1, 0x24, 0 , 0);

there I build my message and I send it via port 40960

and I got a response via port 40963

Wireshark

On wireshark I send the message and the equipment sends a response but the code crashes in this line Byte[] receiveBytes = senderClient.Receive(ref RemoteIpEndPoint);

Without filling in the table and without displaying any error message Is there something missing from my code? what could be the problem

Netstat -a cmd pour l'ip que j'utilise pour l'envoie 192.168.2.20 [CMD Netstat -a]


Solution

  • The solution = you must create a UDPClient for reading. It worked for me

    public void senderUdpClient()
        {
            string serverIP = "192.168.2.11";
            int sendPort = 40960;
            int receivePort = 40963;
            UdpClient senderClient = new UdpClient();
            sendEndPoint = new IPEndPoint(IPAddress.Parse(serverIP), sendPort);
            try
            {
                senderClient.Connect(this.sendEndPoint);
                senderClient.Send(packedMessage2, packedMessage2.Length);
                //Creates a UdpClient for reading incoming data.
                UdpClient receivingUdpClient = new UdpClient(receivePort);
                //Creates an IPEndPoint to record the IP Address and port number of the sender.
                // The IPEndPoint will allow you to read datagrams sent from any source.
                IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
                try
                {
                    // Blocks until a message returns on this socket from a remote host.
                    Byte[] receiveBytes = receivingUdpClient.Receive(ref RemoteIpEndPoint);
    
                    string returnData = Encoding.ASCII.GetString(receiveBytes);
    
                    Console.WriteLine("This is the message you received " +
                                              returnData.ToString());
                    Console.WriteLine("This message was sent from " +
                                                RemoteIpEndPoint.Address.ToString() +
                                                " on their port number " +
                                                RemoteIpEndPoint.Port.ToString());
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                }
                //string returnData = Encoding.ASCII.GetString(receiveBytes);
                senderClient.Close();
                MessageBox.Show("Message Sent");
            }
            catch (Exception err)
            {
                MessageBox.Show(err.ToString());
            }
        }