LeBigornot Profil : Jeune recrue | 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 :
- #include <stdio.h>
- #include <netinet/in.h>
- #include <fcntl.h>
- #include <sys/signal.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netdb.h>
- #include <errno.h>
- #define SERVEURNAME "192.168.0.1"
- #define PORT 23456
- #define CONN_OPEN 0
- #define CONN_CLOSED -1
- #define CONN_INPROG -2
- int setNonblocking(int sd)
- {
- int flags;
- /* If they have O_NONBLOCK, use the Posix way to do it */
- #if defined(O_NONBLOCK)
- /* Fixme: O_NONBLOCK is defined but broken on SunOS 4.1.x and AIX 3.2.5. */
- if (-1 == (flags = fcntl(sd, F_GETFL, 0)))
- flags = 0;
- return fcntl(sd, F_SETFL, flags | O_NONBLOCK);
- #else
- /* Otherwise, use the old way of doing it */
- flags = 1;
- return ioctl(sd, FIOBIO, &flags);
- #endif
- }
- int main(int argc, char **argv)
- {
- char *server_name = SERVEURNAME;
- struct sockaddr_in serverSockAddr;
- struct hostent *hp;
- long hostAddr;
- char buffer[512];
- int sd = -1;
- int result;
- int connstatus = CONN_CLOSED;
- struct timeval tv;
- fd_set fdset;
- int sel;
- int err;
- size_t sz;
- printf("Test Agent connection\n" );
- /* find host machine */
- if (((void*)hp = gethostbyname(server_name)) == 0) {
- printf("error with gethostbyname" );
- exit(1);
- }
- /* fill in the socket structure with host information */
- memset(&serverSockAddr, 0, sizeof(serverSockAddr));
- serverSockAddr.sin_family = AF_INET;
- bcopy(hp->h_addr, &serverSockAddr.sin_addr.s_addr, sizeof (serverSockAddr.sin_addr.s_addr));
- serverSockAddr.sin_port = htons(PORT);
- while (1) {
- if (sd < 0) { /* create a socket (first connection )*/
- printf ("Socket Creation\n" );
- sd = socket(AF_INET, SOCK_STREAM,0);
- if(sd < 0) printf("socket creation failed !!!\n" );
- setNonblocking(sd);
- }
- sleep(1);
- printf("===============================================================\n" );
- switch(connstatus) {
- case(CONN_CLOSED):
- printf("connecting...\n" );
- result = connect(sd, (struct sockaddr *)&serverSockAddr, sizeof(serverSockAddr));
- if (result >= 0) {
- connstatus = CONN_OPEN;
- printf("connected\n" );
- }
- else {
- switch (errno) {
- case EINPROGRESS:
- printf("connection in progress\n" );
- connstatus = CONN_INPROG;
- break;
- case EALREADY:
- printf("connection already in progress\n" );
- connstatus = CONN_INPROG;
- break;
- case EISCONN:
- printf("already connected\n" );
- connstatus = CONN_OPEN;
- fcntl(sd, F_SETFL, 0); /* blocking */
- break;
- case ECONNABORTED:
- printf("Software caused connection abort\n" );
- break;
- default:
- printf("connection KO %d\n",errno);
- 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);
- }
- }
- break;
- case(CONN_INPROG):
- tv.tv_sec = 0;
- tv.tv_usec = 0;
- FD_ZERO(&fdset);
- FD_SET(sd, &fdset);
- result = select(((int)sd)+1, NULL, &fdset, NULL, &tv);
- if (result > 0) { /* no more in progress */
- sz = sizeof(err);
- result = getsockopt(sd, SOL_SOCKET, SO_ERROR, &err, &sz);
- if ((result == 0) && (err == 0)) { /* connected */
- printf("connected socket = %d\n",sd);
- connstatus = CONN_OPEN;
- fcntl(sd, F_SETFL, 0); /* blocking */
- }
- else {
- switch (err) {
- case EINPROGRESS:
- printf("connection in progress 2\n" );
- connstatus = CONN_INPROG;
- break;
- case EALREADY:
- printf("connection already in progress 2\n" );
- connstatus = CONN_INPROG;
- break;
- case EISCONN:
- printf("already connected 2\n" );
- connstatus = CONN_OPEN;
- fcntl(sd, F_SETFL, 0); /* blocking */
- break;
- default:
- printf("connection failed: %d\n",err);
- connstatus = CONN_CLOSED;
- /* 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);*/
- }
- }
- }
- else {
- if (errno == EINTR) {
- printf("connection already in progress 3\n" );
- connstatus = CONN_INPROG;
- }
- else {
- printf("select failed ! %d\n",errno);
- printf("EBADF:%d EINTR;%d EINVAL:%d ENOMEM:%d\n",EBADF,EINTR,EINVAL,ENOMEM);
- connstatus = CONN_CLOSED;
- }
- }
- break;
-
- case CONN_OPEN:
- printf("test connection...\n" );
- tv.tv_sec = 0;
- tv.tv_usec = 0;
- FD_ZERO(&fdset);
- FD_SET(sd, &fdset);
- result = select((int)(sd + 1), &fdset, NULL, NULL, &tv);
- if (result == 0) {/* nothing to read */
- printf("nothing to read !\n" );
- }
- else if (result > 0) {/* datas to read */
- result = recv(sd, buffer, 512, 0);
- if (result < 0) { /* error in recv */
- printf("error in recv\n" );
- connstatus = CONN_CLOSED;
- close(sd);
- sd = -1;
- }
- else if (result > 0 ) {
- buffer[result] = '\0';
- printf("Reçu(%d): %s\n",result, buffer);
- }
- else if (result == 0) { /* disconnected */
- printf("disconnected\n" );
- connstatus = CONN_CLOSED;
- close(sd);
- sd = -1;
- }
- }
- if (connstatus == CONN_OPEN ) {
- printf("sending toto ...\n" );
-
- result = send(sd,"toto",4,0);
- if (result == -1) {
- printf("ERROR in send %d\n",errno);
- 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);
- }
- else printf("OK\n" );
- }
- break;
- }
- }
- }
|
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 |