Click here to Skip to main content
15,886,519 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Why does recvfrom always fail and I get the error 10014 (Bad address)?
C++
#include <winsock2.h>
#include <stdio.h>

int main() {
	short port		= 39890;
	SOCKET sock		= INVALID_SOCKET;
	int err			= 0;
	struct sockaddr_in senderaddr, recvaddr;
	int senderaddrsize;
	WSADATA wsadata;
	const int recvsize = 1024;
	char recvbuf[recvsize];
	
	if ( WSAStartup(MAKEWORD(2,2), &wsadata) != 0 ) {
		return 1;
	}
	
	sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
	
	if ( sock == INVALID_SOCKET ) {
		WSACleanup();
		return 1;
	}
	
	recvaddr.sin_family			= AF_INET;
	recvaddr.sin_port			= htons(port);
	recvaddr.sin_addr.s_addr	= htonl(INADDR_ANY);
	printf("Sever IP: %s\n", inet_ntoa(recvaddr.sin_addr));
	
	err = bind(sock, (struct sockaddr *) &recvaddr, sizeof(recvaddr));
	
	if ( err != 0 ) {
		printf("bind failed: %d\n", WSAGetLastError());
		return 1;
	}
	
	printf("waiting for datagrams\n");
	err = recvfrom(sock, recvbuf, recvsize, 0, (struct sockaddr *) &senderaddr, &senderaddrsize);
	
	if ( err == SOCKET_ERROR ) {
		printf("recvfrom error: %d\n", WSAGetLastError());
	}
	
	WSACleanup();
	while ( 1 ) {
	
	}
}
Posted
Comments
pasztorpisti 3-Aug-12 15:44pm    
Sorry I posted a totally wrong answer!
pasztorpisti 3-Aug-12 15:47pm    
Sent another answer that might help.

C++
int senderaddrsize = sizeof(senderaddr)
 
Share this answer
 
Comments
Philip Stuyck 3-Aug-12 16:25pm    
indeed you have to specify the size of the structure that will contain the peer address. +5
pasztorpisti 3-Aug-12 16:33pm    
Thank you!
TheArchMage 4-Aug-12 23:07pm    
I'm an idiot. Thanks for your help.
pasztorpisti 5-Aug-12 1:37am    
You are welcome! Let yourself to make mistakes, to err is human! :-)
Philip Stuyck 5-Aug-12 3:21am    
No you are not, this is a quite common mistake.
Please mark this as a solution.
I encounter this error when I was binding port to 443 (https). after reading some pages without any sense searched in my computer I realized VMWare Start a Windows Service named "VMWare Authorization Service" that perevent me to bind a socket in 443. after stop this service problem SOLVED!!!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900