Aayman Khalid
Aayman Khalid

Reputation: 468

implementing a non blocking udp socket by select()

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()...

here is my Server(Mentioning only the communication part):

fd_set readfds,writefds;

while(1){
    FD_ZERO(&readfds);
    FD_ZERO(&writefds);
    FD_SET(sd,&readfds);
    FD_SET(sd,&writefds);

    int rv = select(n, &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, &readfds))
    {     
        int client_length = (int)sizeof(struct sockaddr_in);
        memset(&buffer,0,sizeof(buffer));
        int bytes_received = recvfrom(sd, buffer,SIZE, 0, (struct sockaddr *)&client, &client_length);
        if (bytes_received < 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,&writefds)) {
        int client_length = (int)sizeof(struct sockaddr_in);
        if(sendto(sd, buffer,strlen(buffer), 0, (struct sockaddr *) &client,client_length)<0) {
            printf("Error sending the file! \n");
            printf("%d\n",WSAGetLastError());
            exit(1);
        }
    }
}
closesocket(sd);
WSACleanup();

return 0;

}

and this is my client:

fd_set readfds,writefds;
while(1)
{
    FD_ZERO(&readfds);
    FD_ZERO(&writefds);
    FD_SET(cs,&readfds);
    FD_SET(cs,&writefds);
    int rv=select(n,&readfds,&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,&writefds))
    {
        int server_length = sizeof(struct sockaddr_in);
        FD_CLR(cs,&writefds);

        if (sendto(cs, send_buffer, (int)strlen(send_buffer) + 1, 0, (struct sockaddr *)&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, &readfds))
    {
        FD_CLR(cs,&readfds);
        int server_length = sizeof(struct sockaddr_in);
        data2=recvfrom(cs,file_buffer,strlen(file_buffer)-1,0,(struct sockaddr *)&server,&server_length);
        //file_buffer[data2]='\0';
        if(data2<0)
        {
            printf("Server is not on:(\n");
            exit(0);
        }
    }
    //printf("%d",data2);
    printf("\nServer says:");
    for(int i=0;i<data2;i++)
    {
        putchar(file_buffer[i]);
    } 
}

return 0;
}

At first on the server side I wrote:int rv = select(n, &readfds, &writefds, NULL, NULL);

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 "&writefds" did remove the redundant data but the improper communication issue still persists...

Upvotes: 1

Views: 6090

Answers (1)

Xymostech
Xymostech

Reputation: 9850

I see a couple of problems with your code.

First of all, if you want to wait for input from both the socket and the terminal, you should put both of those fds into the readfds set:

FD_SET(cs, &readfds);
FD_SET(stdin, &readfds);

Since you're not doing any writing, you shouldn't be using writefds.

Next, you should make sure that you're only trying to read from the terminal if it has input ready. So, you should be doing something like:

if (FD_ISSET(stdin, &readfds)) {
    // Read in from terminal...
}

You're currently trying to read each time, no matter what happens.

Last, you're measuring the size of your file_buffer incorrectly when you make your recvfrom call. strlen only works once you've already put data into it. You should be using sizeof(file_buffer).

Upvotes: 1

Related Questions