当前位置: 动力学知识库 > 问答 > 编程问答 >

c - bind() fails with windows socket error 10049

问题描述:

I try to make a client/server program in C with IPv6 and UDP. When the program binds the socket it return the WSAError 10049. I know that this is a problem with the adress name but don't see whats the problem. I hope someone can help.

struct sockaddr_in6 server, client;

SOCKET sock;

char buffer[BUFFERSIZE];

LPTSTR recvBuff[1024];

DWORD recvBuffLen = 1024UL;

int len = sizeof(client);

WORD wVersionRequested;

WSADATA wsaData;

wVersionRequested = MAKEWORD(1,1);

WSAStartup(wVersionRequested, &wsaData);

sock = socket(AF_INET6, SOCK_DGRAM, 0);

if (sock < 0)

error("Fehler beim Anlegen des Sockets");

server.sin6_family = AF_INET6;

server.sin6_port = htons(6000);

server.sin6_addr = in6addr_any;

if (bind(sock, (struct sockaddr *) &server, sizeof(server)) == -1)

error("Fehler beim binden des Sockets");

网友答案:

This normally results from an attempt to bind to an address that is not valid for the local computer..

You should use PF_INET here instead of AF_INET. They have the same value, but you're not specifying an address family AF here, you're specifying a protocol family PF. This is just a style recommendation.

I would suggest to memset zero the below arrays,structures:

struct sockaddr_in6 server, client;
SOCKET sock;
char buffer[BUFFERSIZE];
LPTSTR recvBuff[1024];
网友答案:

Before you can use the sockaddr_in6 struct, you will have to memset it to zero:

  memset(server, 0, sizeof(struct sockaddr_in6));

The reason is that the struct sockaddr_in6 structure contains other fields which you are not initializing (such as sin6_scope_id) and which might contain garbage.

网友答案:

I had that same error code when calling bind() under windows.

The reason in my case was not the same as in the initial poster's code, but i guess other will have made the very same mistake as me:

I generated the local address on which i want the server to be bound locally using the inet_addr()-function. I assigned this result to the local address structure struct sockaddr_in localaddr this way:

localaddr.sin_addr.s_addr = htonl(inaddr);

But inet_addr() already returns the address in byte-network-order, so the call htonl(inaddr) was wrong in my code and caused error 10049:

SOCKET tcpsock_bindlisten(unsigned short port, const char* bindaddr)
{
    SOCKET srvsock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);

    unsigned long inaddr = bindaddr ? inet_addr(bindaddr) : INADDR_ANY;

    struct sockaddr_in localaddr;
    memset(&localaddr, 0, sizeof(struct sockaddr_in));
    localaddr.sin_family        = AF_INET;
    localaddr.sin_port          = htons(port);  

    // ERROR HERE! address returned from inet_addr is already in network-byte-order!
    localaddr.sin_addr.s_addr   = htonl(inaddr); 

    // CORRECT THIS WAY:
    localaddr.sin_addr.s_addr   = inaddr;   

    if (bind(srvsock, (struct sockaddr *) &localaddr, sizeof(localaddr)) != 0)
    {
        print_socketerror("tcpsock bind()");
        return INVALID_SOCKET;
    }

    if (listen(srvsock, SVRSOCK_BACKLOG) != 0)
    {
        print_socketerror("tcpsock listen()");
        return INVALID_SOCKET;
    }

    return srvsock;
}

When calling bind() using "all local interfaces" (INADDR_ANY) it worked, because of this coincidence INADDR_ANY == htonl(INADDR_ANY):

int main()
{
    ...
    // this works for this special case:
    SOCKET svrsock1 = tcpsock_bindlisten(4444, NULL); 

    // did not work!
    SOCKET svrsock2 = tcpsock_bindlisten(5555, "192.168.0.123"); 
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: