Winsock bind error (UDP)

Started by
3 comments, last by RonHiler 20 years, 8 months ago
This is my client network initialization code. These are asyncrhronous sockets. I''m failing at the bind call with a 10049 error (WSAEADDRNOTAVAIL), but I honestly don''t see the problem. What am I doing wrong here? (error checking removed) //Start up WinSock WinSockError = WSAStartup(MAKEWORD(2,2), &Network.WinSockData); //create the input socket InputSocket = WSASocket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL, 0, 0); //bind the socket to our port InternetAddr.sin_family = AF_INET; InternetAddr.sin_addr.s_addr = inet_addr(INADDR_ANY); InternetAddr.sin_port = htons(INPUT_PORT); WinSockError = bind (InputSocket, (PSOCKADDR)&InternetAddr, sizeof(InternetAddr)); //asociate the socket with a window handle WinSockError = WSAAsyncSelect(InputSocket, Window, WM_SOCKET, FD_READ); //create the Output socket OutputSocket = WSASocket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL, 0, 0); //asociate the socket with a window handle WinSockError = WSAAsyncSelect(OutputSocket, Window, WM_SOCKET, FD_WRITE);
Creation is an act of sheer will
Advertisement
What''s INPUT_PORT''s value? It could be in a forbidden range.

Ports can be active a bit after the app crashes and a new app instance will be refused access. Set the SO_REUSEADDR bit with ''setsockopt()'' before the ''bind()''.

Check for existing applications using the port with ''netstat -a''.

-cb
quote:Original post by cbenoi1
What''s INPUT_PORT''s value? It could be in a forbidden range.

4828

quote:
Ports can be active a bit after the app crashes and a new app instance will be refused access. Set the SO_REUSEADDR bit with ''setsockopt()'' before the ''bind()''.

Okay, done. No change

Am I supposed to bind a client socket when I use UDP? I''m not clear on this.
Creation is an act of sheer will
You can''t use inet_addr the way you are using it. It is supposed to take a string containing an IPv4 address. Instead use htonl(INADDR_ANY). Of course with INADDR_ANY the htonl call isn''t really needed since in Win32 INADDR_ANY is defined as a long 0.
.
Yep, you are right, Mastaba, that was the problem. I accidentally left that in when I switched to using INADDR_ANY from a explicit address. Oops Works fine now.
Creation is an act of sheer will

This topic is closed to new replies.

Advertisement