user1884325
user1884325

Reputation: 2550

winsock - An invalid argument was supplied (on bind)

This problem is driving me nuts.

I create a socket and set SO_REUSEADDR.

thisSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
int i = 1;
setsockopt(thisSocket , SOL_SOCKET, SO_REUSEADDR, (char *)&i, (int)sizeof(i));

When I bind:

sockaddr_in_t sock_addr;
memset(&sock_addr, 0, sizeof(sockaddr_in_t));

sock_addr.sin_family = AF_INET;
sock_addr.sin_port   = htons(_listeningPort);
sock_addr.sin_addr.s_addr = inet_addr("0.0.0.0");

bind(thisSocket, reinterpret_cast<sockaddr_t*>(&sock_addr), sizeof(sock_addr));

I sometimes get an error:

 "An invalid argument was supplied"

What am I doing wrong here? And why am I getting this error?

UPDATE

Turns out that packets were being sent on the socket before bind() was called. This caused bind() to return an error.

Upvotes: 1

Views: 1109

Answers (2)

user1884325
user1884325

Reputation: 2550

Turns out that packets were being sent on the socket before bind() was called. This caused bind() to return an error.

Upvotes: 1

Remy Lebeau
Remy Lebeau

Reputation: 595329

Per the bind() documentation:

WSAEINVAL

An invalid argument was supplied.

This error is returned of the socket s is already bound to an address.

The error means that thisSocket has already had a successful bind() called on it, and you are trying to bind() it again.

Upvotes: 0

Related Questions