Forums Rue-Montgallet.com
Rue-Montgallet.comRue-Hardware.comRue-Occasion.comRue-DVD.comRue-Jeuxvideo.comRue-AudioVideo.comRue-Telephone.comForums
S'inscrire | S'identifier |
| Recherche avancée | Aide
 
 

Il y a 61 utilisateurs connus et inconnus. Pour voir la liste des connectés connus, cliquez ici

 Mot :   Pseudo :  
 
Bas de page
Auteur
 Sujet :

[UNIX] [SOCKET] [C] Client non-bloquant

 
n°9497
LeBigornot
Profil : Jeune recrue
Posté le 13-07-2004 à 11:32:13  profilanswer
 

Bonjour,
Je developpent un programme client en C qui doit se connecté a plusieurs serveurs (d'ou le mode non-bloquant pour ne pas perdre de temps) mais je n'arrive pas a m'en sortir avec les connexions non-bloquantes...
Voici le code de mon prog de test (un peu long dsl  8)  ):

Code :
  1. #include <stdio.h>
  2. #include <netinet/in.h>
  3. #include <fcntl.h>
  4. #include <sys/signal.h>
  5. #include <sys/types.h>
  6. #include <sys/socket.h>
  7. #include <netdb.h>
  8. #include <errno.h>
  9. #define SERVEURNAME "192.168.0.1"
  10. #define PORT 23456
  11. #define CONN_OPEN 0
  12. #define CONN_CLOSED -1
  13. #define CONN_INPROG -2
  14. int setNonblocking(int sd)
  15. {
  16.     int flags;
  17.     /* If they have O_NONBLOCK, use the Posix way to do it */
  18. #if defined(O_NONBLOCK)
  19.     /* Fixme: O_NONBLOCK is defined but broken on SunOS 4.1.x and AIX 3.2.5. */
  20.     if (-1 == (flags = fcntl(sd, F_GETFL, 0)))
  21.         flags = 0;
  22.     return fcntl(sd, F_SETFL, flags | O_NONBLOCK);
  23. #else
  24.     /* Otherwise, use the old way of doing it */
  25.     flags = 1;
  26.     return ioctl(sd, FIOBIO, &flags);
  27. #endif
  28. }   
  29. int main(int argc, char **argv)
  30. {
  31. char *server_name = SERVEURNAME;
  32. struct sockaddr_in serverSockAddr;
  33. struct hostent *hp;
  34. long hostAddr;
  35. char buffer[512];
  36. int sd = -1;
  37. int result;
  38. int connstatus = CONN_CLOSED;
  39. struct timeval tv;
  40. fd_set fdset;
  41. int sel;
  42. int err;
  43. size_t sz;
  44. printf("Test Agent connection\n" );
  45. /* find host machine */
  46. if (((void*)hp = gethostbyname(server_name)) == 0) {
  47.  printf("error with gethostbyname" );
  48.  exit(1);
  49. }
  50. /* fill in the socket structure with host information */
  51. memset(&serverSockAddr, 0, sizeof(serverSockAddr));
  52. serverSockAddr.sin_family = AF_INET;
  53. bcopy(hp->h_addr, &serverSockAddr.sin_addr.s_addr, sizeof (serverSockAddr.sin_addr.s_addr));
  54. serverSockAddr.sin_port = htons(PORT);
  55. while (1) {
  56.  if (sd < 0) { /* create a socket (first connection )*/
  57.   printf ("Socket Creation\n" );
  58.   sd = socket(AF_INET, SOCK_STREAM,0);
  59.   if(sd < 0) printf("socket creation failed !!!\n" );
  60.   setNonblocking(sd);
  61.  }
  62.  sleep(1);
  63.  printf("===============================================================\n" );
  64.  switch(connstatus) {
  65.   case(CONN_CLOSED):
  66.    printf("connecting...\n" );
  67.    result = connect(sd, (struct sockaddr *)&serverSockAddr, sizeof(serverSockAddr));
  68.    if (result >= 0) {
  69.     connstatus = CONN_OPEN;
  70.     printf("connected\n" );
  71.    }
  72.    else {
  73.     switch (errno) {
  74.      case EINPROGRESS:
  75.       printf("connection in progress\n" );
  76.       connstatus = CONN_INPROG;
  77.       break;
  78.      case EALREADY:
  79.       printf("connection already in progress\n" );
  80.       connstatus = CONN_INPROG;
  81.       break;
  82.      case EISCONN:
  83.       printf("already connected\n" );
  84.       connstatus = CONN_OPEN;
  85.           fcntl(sd, F_SETFL,  0); /* blocking */
  86.       break;
  87.      case ECONNABORTED:
  88.       printf("Software caused connection abort\n" );
  89.       break;
  90.      default:
  91.       printf("connection KO %d\n",errno);
  92.       printf("EBADF:%d EFAULT:%d ENOTSOCK:%d EISCONN:%d ECONNREFUSED:%d ETIMEDOUT:%d ENETUNREACH:%d EHOSTUNREACH:%d EADDRINUSE:%d EALREADY:%d\n",EBADF,EFAULT,ENOTSOCK,EISCONN,ECONNREFUSED,ETIMEDOUT,ENETUNREACH,EHOSTUNREACH,EADDRINUSE,EALREADY);
  93.     }
  94.    }
  95.    break;
  96.   case(CONN_INPROG):
  97.    tv.tv_sec = 0;
  98.    tv.tv_usec = 0;
  99.    FD_ZERO(&fdset);
  100.    FD_SET(sd, &fdset);
  101.      result = select(((int)sd)+1, NULL, &fdset, NULL, &tv);
  102.      if (result > 0) { /* no more in progress */
  103.       sz = sizeof(err);
  104.       result = getsockopt(sd, SOL_SOCKET, SO_ERROR, &err, &sz);
  105.       if ((result == 0) && (err == 0)) {  /* connected */
  106.        printf("connected socket = %d\n",sd);
  107.      connstatus = CONN_OPEN;
  108.      fcntl(sd, F_SETFL,  0); /* blocking */
  109.     }
  110.     else {
  111.      switch (err) {
  112.       case EINPROGRESS:
  113.        printf("connection in progress 2\n" );
  114.        connstatus = CONN_INPROG;
  115.        break;
  116.       case EALREADY:
  117.        printf("connection already in progress 2\n" );
  118.        connstatus = CONN_INPROG;
  119.        break;
  120.       case EISCONN:
  121.        printf("already connected 2\n" );
  122.        connstatus = CONN_OPEN;
  123.        fcntl(sd, F_SETFL,  0); /* blocking */
  124.        break;
  125.       default:
  126.        printf("connection failed: %d\n",err);
  127.        connstatus = CONN_CLOSED;
  128.       /* printf("EBADF:%d EFAULT:%d ENOTSOCK:%d EISCONN:%d ECONNREFUSED:%d ETIMEDOUT:%d ENETUNREACH:%d EHOSTUNREACH:%d EADDRINUSE:%d EALREADY:%d\n",EBADF,EFAULT,ENOTSOCK,EISCONN,ECONNREFUSED,ETIMEDOUT,ENETUNREACH,EHOSTUNREACH,EADDRINUSE,EALREADY);*/
  129.      }
  130.     }
  131.    }
  132.      else {
  133.       if (errno == EINTR) {
  134.        printf("connection already in progress 3\n" );
  135.        connstatus = CONN_INPROG;
  136.       }
  137.       else {
  138.        printf("select failed ! %d\n",errno);
  139.        printf("EBADF:%d EINTR;%d EINVAL:%d ENOMEM:%d\n",EBADF,EINTR,EINVAL,ENOMEM);
  140.        connstatus = CONN_CLOSED;
  141.       }
  142.      }
  143.      break;
  144.  
  145.     case CONN_OPEN:
  146.        printf("test connection...\n" );
  147.      tv.tv_sec = 0;
  148.    tv.tv_usec = 0;
  149.    FD_ZERO(&fdset);
  150.       FD_SET(sd, &fdset);
  151.       result = select((int)(sd + 1), &fdset, NULL, NULL, &tv);
  152.    if (result == 0) {/* nothing to read */
  153.     printf("nothing to read !\n" );
  154.    }
  155.    else if (result > 0) {/* datas to read */
  156.     result = recv(sd, buffer, 512, 0);
  157.     if (result < 0) { /* error in recv */
  158.      printf("error in recv\n" );
  159.      connstatus = CONN_CLOSED;
  160.      close(sd);
  161.      sd = -1;
  162.     }
  163.     else if (result > 0 ) {
  164.      buffer[result] = '\0';
  165.      printf("Reçu(%d): %s\n",result, buffer);
  166.     }
  167.     else if (result == 0) { /* disconnected */
  168.      printf("disconnected\n" );
  169.      connstatus = CONN_CLOSED;
  170.      close(sd);
  171.      sd = -1;
  172.     }
  173.    }
  174.    if (connstatus == CONN_OPEN ) {
  175.     printf("sending toto ...\n" );
  176.      
  177.     result = send(sd,"toto",4,0);
  178.     if (result == -1) {
  179.      printf("ERROR in send %d\n",errno);
  180.      printf("EBADF:%d, ENOTSOCK:%d, EFAULT:%d, EMSGSIZE:%d, EAGAIN:%d, EWOULDBLOCK:%d, ENOBUFS:%d, EINTR:%d, ENOMEM:%d, EINVAL:%d, EPIPE:%d\n",EBADF, ENOTSOCK, EFAULT, EMSGSIZE, EAGAIN, EWOULDBLOCK, ENOBUFS, EINTR, ENOMEM, EINVAL, EPIPE);
  181.     }
  182.     else printf("OK\n" );
  183.    }
  184.    break;
  185.   }
  186. }


 
Donc mon probleme est:
je ne demarre jamais le serveur et voici se qu'il me sort  sous LINUX:
 
Test Agent connection
Socket Creation
=============================================
connecting...
connection in progress
=============================================
connection failed: 111
=============================================
connecting...
Software cause connection abort
=============================================
connecting...
connection in progress
=============================================
connection failed: 111
=============================================
connecting...
Software cause connection abort
 
 
Ceci sort en boucle et je me demande pourquoi donc je reçois ce message ECONNABORTED 1 fois sur 2 ce n'est pas normal non ? Pourquoi pas tout le temps le message 11 "connection refused" ?
que veux t'il dire exactement ?
 
Merci

n°9684
tigre01
A/V Serial ModoSniper
Profil : No Life
Posté le 27-07-2004 à 18:40:00  profilanswer
 

Pas la bonne cat ca :o
 
Plutot dans logiciel non? [:humanrage]
 

n°9822
seek2no
Profil : Jeune recrue
Posté le 06-08-2004 à 21:50:30  profilanswer
 

salut,
Pourquoi ne pas essayer quelque chose de plus simple pour savoir si le bug ne viendrait pas de ton programme:
Donc: à mon avis il faudrait peut-être commencer par envoyer une simple chaîne de caractères genre ("Bonjour monde!" ) à ton serveur de préférence local (localhost) pour commencer.
Deuxième chose (je me trompe peut-être), en parcourant rapidos ton programme j'ai vu que tu crée ton socket en mode tcp/IP(AF_INET) et tu  
utilises un système UNIX probablement en local, je ne sais pas, il faut  voir ce genre de détails,  dernière chose: dans un échange client-serveur il faut un programme-client ET un programme-serveur, encore une fois je peux me tromper.  
cordialement.


Aller à :
Ajouter une réponse