Click here to Skip to main content
12,394,769 members (70,230 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C Windows Sockets
Why does recvfrom always fail and I get the error 10014 (Bad address)?
#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 3-Aug-12 9:15am
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.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

int senderaddrsize = sizeof(senderaddr)
  Permalink  
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.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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!!!
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 7 May 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100