开发者

SO_REUSEADDR with UDP datagrams - Resource unavailable

I'm using SO_REUSEADDR option, but I'm not sure why am getting Resource temporary unvailable option.

I'm testing client server code on 127.0.0.1

if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 )
{
    perror("socket() error!!\n");
    exit(1);
}

if ( setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse) ) < 0 ) {
    perror("SO_REUSEADDR failed::");
    exit(1);
}

while(1) {

    nbytes_read = recvfrom(sockfd, (void *)&recvd_msg, sizeof(recvd_msg),
                           flags, &from, &from_len);
    printf("nbytes_read = %d\n", nbytes_read);
    if(nbytes_read == -1) {
        perror("client: recvfrom() failed");
        return FAILED;
    }
    if (nbytes_read > 0) {
        if(recvd_msg.hdr.msgtype == DATA)
            printf("recvd %d bytes from server\n", recvd_msg.hdr.payload_size);
            ftp_show_payload(&recvd_msg);
    }
    if(recvd_msg.hdr.is_last == TRUE) {
        break;
    }
}

Error message开发者_JAVA百科: " client: recvfrom() failed: Resource temporarily unavailable"

errno:11

After trying to run client for 3-4 times, I get the data, I'm not sure whats happening.

Also, this problem is on Ubuntu Linux, but when I run the same client server on Solaris, it works fine!!


SO_REUSEADDR is useful when you use bind(), but here you are not using bind.

I dont see any problem if recvfrom() returns -1

Use bind() and replace your call recvfrom() with recv(). recv() will receive all the packets at the port you used in your bind call.


Are you trimming out any other socket configuration? EAGAIN is typically returned when you read a non-blocking socket and there's no data available. The manpage for recvfrom lists the possible errnos that will be set on failure with an explanation for each one.


  1. Your test is invalid. recvfrom() can return zero, which doesn't indicate an error. It is only valid to call perror() if you get -1. So you may not have a problem at all ..

  2. I don't see why you're using SO_REUSEADDR at all here, as you're not binding to a specific port.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜