简体   繁体   English

非阻塞UDP端口打开时打开文件失败

[英]Open a file fails when nonblocking UDP port is open

I have a problem with a program. 我的程序有问题。 The program should be triggered by UDP messages, that's why I open a nonblocking UDP socket, because I want to use it later again. 程序应该由UDP消息触发,这就是我打开非阻塞UDP套接字的原因,因为我想稍后再使用它。 After that the program should open a file, copy out a certain amount of bytes and send it to a browser. 之后程序应该打开一个文件,复制出一定数量的字节并将其发送到浏览器。

The problem occurs when I want to open a file, then I get a "resource temporarily not availabe" fault. 当我想打开一个文件,然后我得到“资源暂时不可用”故障时,会出现问题。

Here is a simple program, which creates the same fault. 这是一个简单的程序,它会产生同样的错误。

main part: 主要部分:

udp_openPort(9999);

for(;;){
    if(udp_receiveData(temp, 32) > 0){
        printf("Received Message: %d\n",atoi(temp));
    break;
    }
} 

filefd = open("test.txt",O_RDONLY);
printf("File Open: %s\n",strerror(errno));

read(filefd,buff,sizeof(buff));

printf("Daten: %s",buff);

close(filefd);

udp_closePort();

udp_receiveData(): udp_receiveData():

int udp_receiveData(void* data, int size){
    socklen_t dummy = sizeof(NetworkAddr);
    NetworkAddr sender;
    return recvfrom(sockfd, data, size, MSG_DONTWAIT, (struct sockaddr*) (&sender), &dummy);
}

When I open the socket as a blocking socket, there is no problem with opening the file, but I need I nonblocking socket for my purpose. 当我打开套接字作为阻塞套接字时,打开文件没有问题,但我需要非阻塞套接字用于我的目的。

Did I make a mistake in coding the program or did I made a mistake, when I planned the program? 在计划程序时,我是否在编写程序时犯了错误,或者我犯了错误?

Best regards, PG 最好的问候,PG

EDIT: Here is the udp_openPort() function: 编辑:这是udp_openPort()函数:

int udp_openPort(int portNr){

//Create handle to socket
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd == -1){
    return 0;
}

// Make sure that we don't receive our own packets.
char loop = 0;
if (setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop)) == -1){

}


// Bind to the port where we receive UDP messages.
NetworkAddr addr;
memset(&addr, 0, sizeof(addr));

addr.sin_family      = AF_INET;
addr.sin_port        = htons(portNr);
addr.sin_addr.s_addr = htonl(INADDR_ANY);

if (bind(sockfd, (struct sockaddr*) &addr, sizeof(addr)) == -1){
    return 0;
}
return 1;
}

Maybe someone sees a problem in here. 也许有人在这里看到了问题。

Are you sure that you error after open is not interfere with udp_openPort ? 您确定在openerror是否会干扰udp_openPort Probably udp_openPort does something wrong and set the errno variable which you lately misinterpret as file open error. 可能udp_openPort什么并设置了errno变量,你最近误解为文件打开错误。

Looking at the manpage of open , the errno is set if and only if the error occurred, ie when returned descriptor is -1 . 查看open的manpage,当且仅当错误发生时,即当返回的描述符为-1时,才会设置errno Hence, your error handling is incorrect. 因此,您的错误处理不正确。 You should have checked the value of filefd in order to determine if the file has been opened or not. 您应该检查filefd的值,以确定文件是否已被打开。

Because the file in fact was opened correctly, errno hasn't been modified and your error message was set by udp_receiveData ; 因为文件实际上是正确打开的,所以errno没有被修改,你的错误信息是由udp_receiveData设置的; in this case, as your socket is non-blocking, there was no data on UDP socket ( resource temporarily unavailable ). 在这种情况下,由于您的套接字是非阻塞的,因此UDP套接字上没有数据( 资源暂时不可用 )。 You don't experience this with a blocking socket, as your program is then sleeping waiting for a message to arrive. 您没有遇到阻塞套接字,因为您的程序正在等待消息到达。

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

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