简体   繁体   English

recvfrom函数中的文件描述符错误

[英]Bad file descriptor in recvfrom function

This is my server side code for udp 这是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... 我也改变了文件描述符的名称2次...仍然没有工作......

Your recvfrom is bad. 你的recvfrom很糟糕。 Instead of strlen(buffer), you should use sizeof(buffer). 您应该使用sizeof(缓冲区)而不是strlen(缓冲区)。 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. 由于缓冲区位于堆栈上,因此您可能在其中有一个大字符串,如果recvfrom获取大量数据,则会溢出缓冲区。

I will studying it up some more if that doesn't help. 如果这没有用,我会再研究一下。

The problem is in your call to recvfrom : 问题在于您对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. 但相反,你在缓冲区上调用strlen Because buffer is uninitialized, calling strlen on it is reading uninitialized data which can cause undefined behavior . 因为buffer是未初始化的,所以在其上调用strlen是读取未初始化的数据,这可能导致未定义的行为

Also, unless the client is sending a null terminated string (ie 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. 此外,除非客户端发送空终止字符串(即一些打印机字符后跟空字节),否则对printf的调用也将调用未定义的行为,因为任何超过读取内容的字节都将是未初始化的。

Pass in the buffer size minus 1 (to leave space for a null byte) instead of calling strlen , and clear the buffer just beforehand. 传入缓冲区大小减1(为空字节留空间)而不是调用strlen ,并事先清除缓冲区。

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

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. Bad file descriptor可能引用无效的文件描述符,或者在代码中的其他位置关闭,或者它已在其他地方使用。 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. 您也可以尝试使用不同的端口号创建新套接字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM