本文介绍了C套接字错误10014的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么recvfrom总是失败并且我收到错误10014(错误的地址)?

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 ) {

	}
}

推荐答案

int senderaddrsize = sizeof(senderaddr)



这篇关于C套接字错误10014的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 21:34