Note that there are some explanatory texts on larger screens.

plurals
  1. POimplementing a non blocking udp socket by select()
    text
    copied!<p>I wanted to create an asynchronous/non-blocking udp client server application where the client and server were supposed to chat away with each other without waiting for each other's turns. I came to know that this could be done by select()...</p> <p>here is my Server(Mentioning only the communication part):</p> <pre><code>fd_set readfds,writefds; while(1){ FD_ZERO(&amp;readfds); FD_ZERO(&amp;writefds); FD_SET(sd,&amp;readfds); FD_SET(sd,&amp;writefds); int rv = select(n, &amp;readfds, NULL, NULL, NULL); if(rv==-1) { printf("Error in Select!!!\n"); exit(0); } if(rv==0) { printf("Timeout occurred\n"); } if (FD_ISSET(sd, &amp;readfds)) { int client_length = (int)sizeof(struct sockaddr_in); memset(&amp;buffer,0,sizeof(buffer)); int bytes_received = recvfrom(sd, buffer,SIZE, 0, (struct sockaddr *)&amp;client, &amp;client_length); if (bytes_received &lt; 0) { fprintf(stderr, "Could not receive datagram.\n"); closesocket(sd); WSACleanup(); exit(0); } } printf("\nClient says: %s",buffer); printf("\nWrite :"); fgets(buffer,SIZE,stdin); if(FD_ISSET(sd,&amp;writefds)) { int client_length = (int)sizeof(struct sockaddr_in); if(sendto(sd, buffer,strlen(buffer), 0, (struct sockaddr *) &amp;client,client_length)&lt;0) { printf("Error sending the file! \n"); printf("%d\n",WSAGetLastError()); exit(1); } } } closesocket(sd); WSACleanup(); return 0; </code></pre> <p>}</p> <p>and this is my client:</p> <pre><code>fd_set readfds,writefds; while(1) { FD_ZERO(&amp;readfds); FD_ZERO(&amp;writefds); FD_SET(cs,&amp;readfds); FD_SET(cs,&amp;writefds); int rv=select(n,&amp;readfds,&amp;writefds,NULL,NULL); if(rv==-1) { printf("Error in Select!!!\n"); exit(0); } if(rv==0) { printf("Timeout occurred\n"); } printf("\nWrite "); fgets(send_buffer,SIZE,stdin); if(FD_ISSET(cs,&amp;writefds)) { int server_length = sizeof(struct sockaddr_in); FD_CLR(cs,&amp;writefds); if (sendto(cs, send_buffer, (int)strlen(send_buffer) + 1, 0, (struct sockaddr *)&amp;server, server_length) == -1) { fprintf(stderr, "Error transmitting data.\n"); closesocket(cs); WSACleanup(); exit(0); } } char file_buffer[SIZE]; //Reply reception from the server:"Ready to receive file" int data2=0; if (FD_ISSET(cs, &amp;readfds)) { FD_CLR(cs,&amp;readfds); int server_length = sizeof(struct sockaddr_in); data2=recvfrom(cs,file_buffer,strlen(file_buffer)-1,0,(struct sockaddr *)&amp;server,&amp;server_length); //file_buffer[data2]='\0'; if(data2&lt;0) { printf("Server is not on:(\n"); exit(0); } } //printf("%d",data2); printf("\nServer says:"); for(int i=0;i&lt;data2;i++) { putchar(file_buffer[i]); } } return 0; } </code></pre> <p>At first on the server side I wrote:<code>int rv = select(n, &amp;readfds, &amp;writefds, NULL, NULL);</code></p> <p>but that led to the printing of an entire empty array on the server console when the server initialised in addition to the fact that communication between the server and client was not proper. removing "&amp;writefds" did remove the redundant data but the improper communication issue still persists... So I would be really thankful if somebody helped me out...</p>
 

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