Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C Windows socket
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 10:15am
Comments
pasztorpisti at 3-Aug-12 15:44pm
   
Sorry I posted a totally wrong answer!
pasztorpisti at 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 at 3-Aug-12 16:25pm
   
indeed you have to specify the size of the structure that will contain the peer address. +5
pasztorpisti at 3-Aug-12 16:33pm
   
Thank you!
TheArchMage at 4-Aug-12 23:07pm
   
I'm an idiot. Thanks for your help.
pasztorpisti at 5-Aug-12 1:37am
   
You are welcome! Let yourself to make mistakes, to err is human! :-)
Philip Stuyck at 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
0 Zoltán Zörgő 300
1 /\jmot 205
2 Peter Leow 165
3 CHill60 145
4 CPallini 135
0 Sergey Alexandrovich Kryukov 9,383
1 OriginalGriff 6,881
2 Peter Leow 4,607
3 Zoltán Zörgő 4,284
4 CHill60 2,892


Advertise | Privacy | Mobile
Web03 | 2.8.150129.1 | Last Updated 7 May 2013
Copyright © CodeProject, 1999-2015
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