Partage
  • Partager sur Facebook
  • Partager sur Twitter

Réponse non reçu UDP

    1 février 2021 à 13:21:04

    Bonjour, j'envoie un message à un équipement via UDP sur le port 40960 et je suis censé avoir une réponse sur le port 40963

    Sur wireshark je vois bien l'envoie de message et la Réponse

    Mais je la reçois pas sur mon programme

    public IPEndPoint sendEndPoint;
            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);
                    //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());
                }
            }


    Le code se plante sur cette ligne sans aucun message d'erreur ni exception et sans remplir le tableau avec les donnée non plus

                     Byte[] receiveBytes = senderClient.Receive(ref RemoteIpEndPoint);
    


    y'a t'il quelque chose qui manque à mon code ? j'ai essayé de desactiver le firewall mais ça n'a rien changé

    --------------

    Mise à jour

    le probléme à était résolut en créant un nouveau UDPClient pour la réception des données, puisque j'envoie et je reçois sur différent port

    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());
            }
        }

    -
    Edité par QuatreHuit 2 février 2021 à 10:40:47

    • Partager sur Facebook
    • Partager sur Twitter

    Réponse non reçu UDP

    × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
    × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
    • Editeur
    • Markdown