Milan Shah
Milan Shah

Reputation: 111

Bad file descriptor in recvfrom function

This is my server side code for udp

#include"headers.h"
int main(int argc,char **argv)
{
    //------------udp server socket connection------//

    int sd1;
    struct sockaddr_in serveraddr, clientaddr;
    char buffer[100];
    char *bufptr = buffer;
    int cnt=1,ret;
    socklen_t clen;
    clen=sizeof(clientaddr);

    //int buflen = sizeof(buffer);

    sd1=socket(AF_INET,SOCK_DGRAM,0);

    printf("udp socket id=%d\n",sd1);

    printf("socket created for udp..\n");

    if(sd1<0)
    {
        perror("udp_sfd");
        exit(0);
    }

    printf("server socket created..\n");

    serveraddr.sin_family=AF_INET;
    serveraddr.sin_port=htons(atoi(argv[1]));
    serveraddr.sin_addr.s_addr=INADDR_ANY;

    if(bind(sd1,(struct sockaddr *)&serveraddr,sizeof(serveraddr))<0)
    {
        perror("bind\n");
        exit(0);
    }
    else
    {

        while(1)
        {
            printf("server accept from client\n");

            ret=recvfrom(sd1,(char *)bufptr,strlen(buffer),0,(struct sockaddr *)&clientaddr,&clen);
            printf("ret=%d\n",ret);

            //printf("hello\n");
            if(ret<0)
            {
                perror("recvfrom");
                exit(0);
            }
            else
            {
                printf("UDP Server received the following:\n \"%s\" message\n", bufptr);
            }
            //close(sd1);
        }
    }

    close(sd1);
    return 0;
}

I am sending tht buffer from client side... and in server side it is giving me an error like this....

Bad file descriptor .... what should I do...

I also changed the name of file descriptor 2 times... still it is not working...

Upvotes: 4

Views: 4049

Answers (3)

dbush
dbush

Reputation: 223689

The problem is in your call to recvfrom:

ret=recvfrom(sd1,(char *)bufptr,strlen(buffer),0,(struct sockaddr *)&clientaddr,&clen);

The third parameter should be the size of the input buffer. But instead, you're calling strlen on the buffer. Because buffer is uninitialized, calling strlen on it is reading uninitialized data which can cause undefined behavior.

Also, unless the client is sending a null terminated string (i.e. some printer characters followed by a null byte) the call to printf will also invoke undefined behavior since any bytes past what was read will be uninitialized.

Pass in the buffer size minus 1 (to leave space for a null byte) instead of calling strlen, and clear the buffer just beforehand.

memset(buffer, 0, sizeof(buffer));
ret=recvfrom(sd1,(char *)bufptr,sizeof(buffer)-1,0,(struct sockaddr *)&clientaddr,&clen);

Upvotes: 1

No One in Particular
No One in Particular

Reputation: 2894

Your recvfrom is bad. Instead of strlen(buffer), you should use sizeof(buffer). Since buffer is on the stack, you may have a large string in there and then you are overflowing the buffer if the recvfrom gets a large amount of data.

I will studying it up some more if that doesn't help.

Upvotes: 1

Stubborn
Stubborn

Reputation: 780

I think you are closing the socket some where else in your program.

The Bad file descriptor may refer to an invalid file descriptor or it is closed somewhere else in your code or it is already being used somewhere else. I think you need to debug your code a little bit and then manage your socket well.

It is possible that your socket is being closed somewhere by mistake or being corrupted.

You can try creating the new socket as well with different port number.

Upvotes: 0

Related Questions