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

Réseau C Discussion :

Probleme Boucle RECV


Sujet :

Réseau C

  1. #1
    Membre confirmé
    Profil pro
    Inscrit en
    Février 2008
    Messages
    126
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2008
    Messages : 126
    Par défaut Probleme Boucle RECV
    Bonjour a tous, j'ai parcouru de nombreux postes sur le net tentant de trouver une solution a mon problème, mais rien y fait ,je suis toujours bloquer.
    J'utilise une boucle pour recevoir mes données sur ma socket et malgrè avoir utilisé la fonction (ioctlsocket) pour rendre mon socket NON BLOQUANT je n'arrive toujours pas a sortir de ma boucle du moins c'est ce que je pense puisque apres ma boucle en question ,je fait un simple (printf) et celui-ci ne fonctionne pas.J' ai aussi tenté d'utilisé (select) pour controler l'état de mes données sur ma socket, mais ça na pas marché non plus alors il semble que ce soit du a ma boucle.Je poste le code ,si une âme charitable pouvait m'aider,je lui en serai reconnaissant, merci par avance.

    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
    45
    46
    47
    48
    49
    50
    51
    52
     
     
     
    void clean();
     
    int main()
     
    {
        size_t ve=0;
        unsigned char BT[4048];
     
       int result=1;
       unsigned long arg=0;
     
     
        WSADATA WSAData;
        WSAStartup(MAKEWORD(2,0),&WSAData);
     
        SOCKADDR_IN sin;
        SOCKET sock;
     
        sock=socket(AF_INET,SOCK_STREAM,0);
        sin.sin_addr.s_addr=inet_addr("192.168.1.4");
        sin.sin_family=AF_INET;
        sin.sin_port=htons(23);
     
        connect(sock, (SOCKADDR *)&sin, sizeof(sin));
        result = ioctlsocket (sock1, FIONBIO, &arg); 
     
     
        do{
     
          clean();
          recv(sock,BT,sizeof(BT)-1,0);
          ve=strlen(BT);
          printf("%s\n",BT);
     
         }while(ve>0);
     
        printf("Reception Des Donnees Réussie");
     
    }
     
    void clean()
    {
     
    int i;
    for(i=0;i<sizeof(BT)-1;i++)
    {
    BT[i]=0;
    }
    }

  2. #2
    Expert éminent
    Avatar de Emmanuel Delahaye
    Profil pro
    Retraité
    Inscrit en
    Décembre 2003
    Messages
    14 512
    Détails du profil
    Informations personnelles :
    Âge : 68
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Retraité

    Informations forums :
    Inscription : Décembre 2003
    Messages : 14 512
    Par défaut
    Citation Envoyé par chuko Voir le message
    Bonjour a tous, j'ai parcouru de nombreux postes sur le net tentant de trouver une solution a mon problème, mais rien y fait ,je suis toujours bloquer.
    J'utilise une boucle pour recevoir mes données sur ma socket et malgrè avoir utilisé la fonction (ioctlsocket) pour rendre mon socket NON BLOQUANT je n'arrive toujours pas a sortir de ma boucle du moins c'est ce que je pense puisque apres ma boucle en question ,je fait un simple (printf) et celui-ci ne fonctionne pas.J' ai aussi tenté d'utilisé (select) pour controler l'état de mes données sur ma socket, mais ça na pas marché non plus alors il semble que ce soit du a ma boucle.Je poste le code ,si une âme charitable pouvait m'aider,je lui en serai reconnaissant, merci par avance.
    Ton code est incomplet.
    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
    45
    46
     
     
    -------------- Build: Debug in hello ---------------
     
    Compiling: main.c
    Linking console executable: bin\Debug\hello.exe
    C:\dev\hello\main.c:4: warning: function declaration isn't a prototype
    C:\dev\hello\main.c:8: warning: function declaration isn't a prototype
    C:\dev\hello\main.c: In function `main':
    C:\dev\hello\main.c:9: error: `size_t' undeclared (first use in this function)
    C:\dev\hello\main.c:9: error: (Each undeclared identifier is reported only once
    C:\dev\hello\main.c:9: error: for each function it appears in.)
    C:\dev\hello\main.c:9: error: syntax error before "ve"
    C:\dev\hello\main.c:16: error: `WSADATA' undeclared (first use in this function)
    C:\dev\hello\main.c:16: error: syntax error before "WSAData"
    C:\dev\hello\main.c:17: warning: implicit declaration of function `WSAStartup'
    C:\dev\hello\main.c:17: warning: implicit declaration of function `MAKEWORD'
    C:\dev\hello\main.c:17: error: `WSAData' undeclared (first use in this function)
    C:\dev\hello\main.c:19: error: `SOCKADDR_IN' undeclared (first use in this function)
    C:\dev\hello\main.c:19: error: syntax error before "sin"
    C:\dev\hello\main.c:20: error: `SOCKET' undeclared (first use in this function)
    C:\dev\hello\main.c:22: error: `sock' undeclared (first use in this function)
    C:\dev\hello\main.c:22: warning: implicit declaration of function `socket'
    C:\dev\hello\main.c:22: error: `AF_INET' undeclared (first use in this function)
    C:\dev\hello\main.c:22: error: `SOCK_STREAM' undeclared (first use in this function)
    C:\dev\hello\main.c:23: error: `sin' undeclared (first use in this function)
    C:\dev\hello\main.c:23: warning: implicit declaration of function `inet_addr'
    C:\dev\hello\main.c:25: warning: implicit declaration of function `htons'
    C:\dev\hello\main.c:27: warning: implicit declaration of function `connect'
    C:\dev\hello\main.c:27: error: `SOCKADDR' undeclared (first use in this function)
    C:\dev\hello\main.c:27: error: syntax error before ')' token
    C:\dev\hello\main.c:28: warning: implicit declaration of function `ioctlsocket'
    C:\dev\hello\main.c:28: error: `sock1' undeclared (first use in this function)
    C:\dev\hello\main.c:28: error: `FIONBIO' undeclared (first use in this function)
    C:\dev\hello\main.c:34: warning: implicit declaration of function `recv'
    C:\dev\hello\main.c:35: error: `ve' undeclared (first use in this function)
    C:\dev\hello\main.c:35: warning: implicit declaration of function `strlen'
    C:\dev\hello\main.c:35: warning: pointer targets in passing arg 1 of `strlen' differ in signedness
    C:\dev\hello\main.c:36: warning: implicit declaration of function `printf'
    C:\dev\hello\main.c: At top level:
    C:\dev\hello\main.c:45: warning: function declaration isn't a prototype
    C:\dev\hello\main.c: In function `clean':
    C:\dev\hello\main.c:48: error: `BT' undeclared (first use in this function)
    C:\dev\hello\main.c:48: warning: comparison of unsigned expression < 0 is always false
    Process terminated with status 1 (0 minutes, 0 seconds)
    20 errors, 15 warnings
    Ceci compile correctement :
    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
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
     
    #ifdef __cplusplus
    #error Be sure you are using a C compiler...
    #endif
     
    #include <winsock2.h>
     
    #include <stdio.h>
     
    void clean (char BT[], size_t size)
    {
     
       size_t i;
       for (i = 0; i < size - 1; i++)
       {
          BT[i] = 0;
       }
    }
     
    int main (void)
    {
       size_t ve = 0;
       char BT[4048];
     
       int result = 1;
       unsigned long arg = 0;
     
       WSADATA WSAData;
       WSAStartup (MAKEWORD (2, 0), &WSAData);
     
       {
          SOCKET sock = socket (AF_INET, SOCK_STREAM, 0);
          SOCKADDR_IN sin;
          sin.sin_addr.s_addr = inet_addr ("192.168.0.1");
          sin.sin_family = AF_INET;
          sin.sin_port = htons (23);
     
          connect (sock, (SOCKADDR *) & sin, sizeof (sin));
          result = ioctlsocket (sock, FIONBIO, &arg);
     
          do
          {
             clean (BT, sizeof BT);
             recv (sock, BT, sizeof (BT) - 1, 0);
             ve = strlen (BT);
             printf ("%s\n", BT);
          }
          while (ve > 0);
     
          printf ("Reception Des Donnees Réussie");
       }
       WSACleanup ();
     
       return 0;
    }
    Je déconseille d'utiliser les sockets non bloquants. C'est rarement justifié et plutôt complexe.

    Si tu as un problème de concurrence, soit tu utilises select()

    http://emmanuel-delahaye.developpez....aux.htm#select

    soit les threads :

    http://emmanuel-delahaye.developpez.com/pthreads.htm

    Pour savoir si la connexion est toujours établie, on teste le code retour de recv(). D'ailleurs, la valeur sert à placer correctement le 0 dans les données reçues, pour en faire un chaine C valide (inutile d'effacer le buffer à chaque fois).

    Pour ton utilisation, pour le moment, ceci suffit (connexion au serveur web de mon routeur) :
    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
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
     
    #ifdef __cplusplus
    #error Be sure you are using a C compiler...
    #endif
     
    #include <winsock2.h>
     
    #include <stdio.h>
     
    #define IP "192.168.0.1"
    #define PORT 80
     
    #define NELEM(a) (sizeof(a)/sizeof*(a))
     
    static void psock_perror (char const *cmt)
    {
       fprintf (stderr, "%s: ", cmt);
       {
          DWORD err = WSAGetLastError ();
          wchar_t msg[1024];
          FormatMessageW (FORMAT_MESSAGE_FROM_SYSTEM, NULL, err, 0,
                          msg, NELEM (msg), NULL);
          fwprintf (stderr, L"%s\n", msg);
       }
    }
     
    int main (void)
    {
       WSADATA WSAData;
       int err = WSAStartup (MAKEWORD (2, 0), &WSAData);
     
       if (!err)
       {
          SOCKET sock = socket (AF_INET, SOCK_STREAM, 0);
          if (sock != INVALID_SOCKET)
          {
             SOCKADDR_IN sin;
     
             sin.sin_addr.s_addr = inet_addr (IP);
             sin.sin_family = AF_INET;
             sin.sin_port = htons (PORT);
     
             err = connect (sock, (SOCKADDR *) & sin, sizeof (sin));
     
             if (!err != SOCKET_ERROR)
             {
                int n;
                char BT[4048];
                {
                   char msg[] = "GET index.htm HTTP/1.0\n\r\n\r";
                   n = send (sock, msg, strlen (msg), 0);
                   printf ("send: n = %d\n", n);
                }
                if (n > 0)
                {
                   do
                   {
                      n = recv (sock, BT, sizeof BT - 1, 0);
                      printf ("recv: n = %d\n", n);
                      if (n > 0)
                      {
                         BT[n] = 0;
                         printf ("%s", BT);
                         fflush (stdout);
                      }
                   }
                   while (n > 0);
     
                   if (n < 0)
                   {
                      psock_perror ("connect()");
                   }
                   else
                   {
                      printf ("deconnexion par le serveur\n");
                   }
                }
                else
                {
                   printf ("deconnexion par le serveur\n");
                }
                closesocket (sock);
             }
             else
             {
                psock_perror ("recv()");
             }
          }
          else
          {
             psock_perror ("connect()");
          }
          WSACleanup ();
       }
       else
       {
          psock_perror ("WSAStartup ()");
       }
     
       return 0;
    }
    Détails ici : http://emmanuel-delahaye.developpez.com/reseaux.htm
    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
     
    send: n = 26
    recv: n = 80
    HTTP/1.1 404 Not Found
    Content-Type: text/html
    Server: ZyXEL-RomPager/3.02
     
    recv: n = 213
    <HTML>
    <HEAD>
    <TITLE>Object Not Found</TITLE>
    </HEAD>
    <BODY>
    <H1>Object Not Found</H1>
    The requested URL 'index.htm' was not found on the RomPager server.<P>
    Return to <A HREF="">last page</A><P>
     
    </BODY>
    </HTML>
    recv: n = 0
    deconnexion par le serveur
     
    Process returned 0 (0x0)   execution time : 0.070 s
    Press any key to continue.

  3. #3
    Membre confirmé
    Profil pro
    Inscrit en
    Février 2008
    Messages
    126
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2008
    Messages : 126
    Par défaut re re re
    Merci pour votre reponse je vais me diriger du cote des thread merci encore.

Discussions similaires

  1. Probleme boucle et tableau
    Par orj30754 dans le forum C
    Réponses: 6
    Dernier message: 27/12/2006, 19h24
  2. Probleme boucle for
    Par griese dans le forum Linux
    Réponses: 4
    Dernier message: 06/12/2006, 14h39
  3. Probleme Boucle infinie Recordset.movenext "muet"?
    Par shub dans le forum VBA Access
    Réponses: 7
    Dernier message: 14/06/2006, 13h22
  4. [Tableaux] Probleme boucle et post
    Par Caprichosa dans le forum Langage
    Réponses: 4
    Dernier message: 04/05/2006, 14h06
  5. [LG]Problême Boucle
    Par pierrOPSG dans le forum Langage
    Réponses: 1
    Dernier message: 29/04/2004, 16h48

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