IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C# Discussion :

Réponse UDP non reçu [Débutant]


Sujet :

C#

  1. #1
    Nouveau Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Mai 2019
    Messages
    15
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 26
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mai 2019
    Messages : 15
    Points : 0
    Points
    0
    Par défaut Réponse UDP non reçu
    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
    Nom : Capture.JPG
Affichages : 95
Taille : 61,7 Ko


    Mon Code

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
     
     
    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

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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!

    j'ai essayé également de créer un UDPClient pour l'envoie et un autre pour la reception

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
     
       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);
                    senderClient.Close();
                    MessageBox.Show("Message Sent");
                }
                catch (Exception err)
                {
                    MessageBox.Show(err.ToString());
                }
            }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
     
     
     public void readerUdpClient()
            {
                string serverIP = "192.168.2.11";
                int receivePort = 40963;
                try
                {
                    var remoteEP = new IPEndPoint(IPAddress.Parse(serverIP), receivePort);
                    UdpClient readerClient = new UdpClient();
                    readerClient.Connect(remoteEP);
                    byte[] bytesReceived = readerClient.Receive(ref remoteEP);
                }
                catch (Exception err)
                {
                    MessageBox.Show(err.ToString());
                }
            }

    et j'ai utilisé les sockets C#

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
     
     
     public void senderUdpClient()
            {
                string serverIP = "192.168.2.11";
                int sendPort = 40960;
                int receiport = 40963;
     
            try
                {
                    // Create socket
                    Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                    // Bind
                    IPEndPoint myEP = new IPEndPoint(IPAddress.Any, 0);
                    s.Bind(myEP);
     
     
                    // Send it
                    IPEndPoint sEP = new IPEndPoint(IPAddress.Parse(serverIP), sendPort);
                    int res = s.SendTo(packedMessage2, sEP);
     
                    // Receive the response
                    byte[] receiveBytes = new Byte[1024];
                    EndPoint recEP = new IPEndPoint(IPAddress.Parse(serverIP), receiport);
                    res = s.ReceiveFrom(receiveBytes, ref recEP);
                    Console.WriteLine("We've got some data from the server!!! " + res + " bytes.");
                }
                catch (Exception e)
                {
                    Console.WriteLine("ERROR: " + e.ToString());
                }
     
     
            }

    Aucun des trois code marche pour moi et toujours il se bloque dans la ligne ou je remplis le tableau avec les données reçu et pareil pour Wireshark également je vois toujours un message envoyé et un message reçu comme montre la photo

    quelqu'un aurait une explications svp

  2. #2
    Nouveau Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Mai 2019
    Messages
    15
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 26
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mai 2019
    Messages : 15
    Points : 0
    Points
    0
    Par défaut
    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

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
     
    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 " +
                                                SendendPont.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());
            }
        }

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [Mail] mail supposé envoyé mais non reçu
    Par juliano_bipso dans le forum Langage
    Réponses: 9
    Dernier message: 28/03/2009, 17h15
  2. Socket UDP non accessible via le web
    Par guish59 dans le forum Réseau
    Réponses: 8
    Dernier message: 12/12/2008, 23h45
  3. Réponses: 1
    Dernier message: 07/11/2008, 02h46
  4. Réponses: 32
    Dernier message: 18/06/2007, 09h28
  5. Réponses: 16
    Dernier message: 28/11/2005, 20h09

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo