];
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. 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!) { 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"); }