Note that there are some explanatory texts on larger screens.

plurals
  1. POhow read all the server's responses from a socket?
    primarykey
    data
    text
    <p>I have a very big problem... I'm working with sockets in C. I send a request to the server which sends me many responses. The problem is that the client receives the first response and then the connection is closed. What can I do? I tried with <code>setsockopt()</code>... SO_KEEPALIVE or SO_LINGER but I haven't resolved the problem. Can you help me? Thanks a lot </p> <hr> <p>To be more clear here is the code. The socket is automatically closed after a certain amount of time or after the client received the first response... I'm not sure.</p> <pre><code>char* demarre_client( client_args * c_args,char* message, /*char* SERVEUR_PORT*/int port){ int socket_client=socket(PF_INET,SOCK_STREAM,0); memset(&amp;(c_args-&gt;adresse_serveur),0,sizeof(c_args-&gt;adresse_serveur)); c_args-&gt;adresse_serveur.sin_family=AF_INET; c_args-&gt;adresse_serveur.sin_addr.s_addr=inet_addr(SERVEUR_IP); //int port=APHash(SERVEUR_PORT,strlen(SERVEUR_PORT)); c_args-&gt;adresse_serveur.sin_port=htons(port); int err=0; if ((err=connect(socket_client, (struct sockaddr *) &amp;(c_args-&gt;adresse_serveur), sizeof(c_args-&gt;adresse_serveur)))&lt;0) { printf("CONNECT error %d\n", errno); exit(-1); } if (send(socket_client, message, strlen(message), 0)!=strlen(message)) { printf("send error!!\n"); exit(-2); } char* raspFin=(char* )malloc(sizeof(char)*1024); strcpy(raspFin,""); int num=-1; int nn=0; char* rasp=(char* )malloc(sizeof(char)*1024); memset (rasp, 0, 1024 * sizeof(char )); /* here it reads the first response and after he get out of while */ while ((num=recv(socket_client, rasp,1024,MSG_WAITALL))&gt;0) { printf("recu %s mun=%d\n" , rasp,num); strcat(raspFin,rasp); strcat(raspFin,"\n"); rasp=(char* )malloc(sizeof(char)*1024); memset (rasp, 0, 1024 * sizeof(char )); } if (num&lt;0) { printf("rasp error!!\n"); exit(-3); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload