AczQc 2020-12-08 00:15 采纳率: 100%
浏览 44
已结题

SocketError 10061新手求解答

#include<WinSock2.h>
#include<stdio.h>
#include<Windows.h>
#pragma comment (lib,"WS2_32.lib")
void Error(char* str) {
	printf("%s失败 错误码为%d",str ,WSAGetLastError());
	WSACleanup();
	exit(0);
}
int main() {
	//第一步打开WSAStartup
	WORD wdVersion = MAKEWORD(2,2);
	WSADATA WsaData ;
	int Ret = WSAStartup(wdVersion,&WsaData);
	if (!Ret) printf("0");
	else Error("建立",0);

	//创建socket

	SOCKET ServerSock = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
	if (ServerSock == INVALID_SOCKET) {
		Error("socket创建");	
	}
	else printf("1");

	//创建sockaddr_in 结构体
	SOCKADDR_IN si = { 0 };
	si.sin_family = AF_INET;
	si.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
	si.sin_port = htons("17899");

	//bind
	int bind_Ret = bind(ServerSock, (struct socaddr *) &si, sizeof(SOCKADDR_IN));
	if (bind_Ret == SOCKET_ERROR) {//绑定是否成功
		int bind_Error = WSAGetLastError();
		printf("绑定失败,错误码为:%d", bind_Error);
		closesocket(ServerSock);
		WSACleanup();
		return 0;
	}
	else {
		printf("bind完成\n");
	}

	//listen
	int listen_Ret = listen(ServerSock, SOMAXCONN);//错误返回值也是SOCKET_ERROR	
	if (listen_Ret == SOCKET_ERROR) {//绑定是否成功
		int listen_Error = WSAGetLastError();
		printf("listen失败,错误码为:%d", listen_Error);
		closesocket(ServerSock);
		WSACleanup();
		return 0;
	}
	else {
		printf("listen完成\n");
	}


	//accept
	/*SOCKADDR_IN client_addr ;
	int len = sizeof(client_addr);
	SOCKET client_sock = accept(ServerSock, (struct sockaddr *)&client_addr, &len);
	if (client_sock != INVALID_SOCKET) {
		char send_msg[1500] = { "hello 客户端\n" };
		send(client_sock, send_msg, sizeof("hello 客户端\n"), 0);
	}*/
	SOCKADDR_IN clientmsg;
	int len = sizeof(clientmsg);
	SOCKET client = accept(ServerSock, (struct sockaddr *)&clientmsg, &len);
	if (client == INVALID_SOCKET) {
		int accept_Error = WSAGetLastError();
		printf("accept失败,错误码为:%d", accept_Error);
		closesocket(ServerSock);
		WSACleanup();
		return 0;
	}
	else {
		printf("accpet接收成功\n");
	}

	char recv_msg[1500] = { 0 };
	recv(client, recv_msg,1499,0);
	printf("%S", recv_msg);

	closesocket(ServerSock);
	closesocket(client);
	WSACleanup();
	return 0;
 }

以上是我的服务器代码 

下面是客户端代码 

#include<stdio.h>
#include<WinSock2.h>
#pragma comment (lib,"WS2_32.lib")
int main() {
	WORD wdVersion = MAKEWORD(2, 2);
	WSADATA lpWsMsg;
	int WsRet = WSAStartup(wdVersion, &lpWsMsg);
	if (!WsRet) {
		printf("建立成功\n");
	}
	else
	{
		WSACleanup();
		printf("错误码为%d\n", WSAGetLastError());
	}

	SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	/*
	参数一:
	AF_INET IPV4 || AF_INET6 IPV6;
	参数二:
	SOCK_STREAM(流格式套接字/面向连接的套接字) // 信息不完整重发 强调顺序 发送接收并不同步
	SOCK_DGRAM(数据报套接字/无连接的套接字)	//不进行重新传送 强调高速而非顺序 发送接收同步
	参数三:
	IPPROTO_TCP //tcp
	IPPTOTO_UDP	//udp
	以上均为常用宏
	更多看B站
	*/
	if (sock == INVALID_SOCKET) {
		printf("sock创建失败\n错误码为%d\n", WSAGetLastError());
		WSACleanup();
		return 0;
	}
	else {
		printf("sock创建完成\n");
	}

	SOCKADDR_IN si_sever = { 0 };
	si_sever.sin_family = AF_INET;
	si_sever.sin_port = htons(17899);
	si_sever.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");

	if (!connect(sock, (SOCKADDR*)&si_sever, sizeof(SOCKADDR_IN))) {
		char str_recv[1500] = { 0 };
		int length_recv = recv(sock, str_recv, 1499, NULL);
		printf("%s  (%d)\n", str_recv, length_recv);
		send(sock, "你好", sizeof("你好"), 0);
	}
	else
	{	//CONNECT错误返回值为SOCKET_ERROR
		printf("connect失败,错误码为%d\n", WSAGetLastError());
		closesocket(sock);
		WSACleanup();
		getchar();
		return 0;
	}
	closesocket(sock);
	WSACleanup();
	getchar();
	return 0;
}

两段代码均在本机运行,另一个服务器代码(服务器A)在我电脑上可以运行,相同的客户端代码可以连A就是连不了上面这个错误服务器,实在找不到问题在哪了,求解答。

  • 写回答

1条回答 默认 最新

  • wlj1234 2020-12-08 15:19
    关注

    服务器端端口号设置

    si.sin_port = htons("17899");

    应该是 

    si.sin_port = htons(17899);

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 8月5日

悬赏问题

  • ¥15 准备学习小程序搭建,谁能手把手的教我啊?
  • ¥15 关于#嵌入式硬件#的问题:树莓派第一天重装配置python和opencv后第二天打开就成这样,瞎捣鼓搞出来文件夹还是没把原来的界面调回来
  • ¥20 Arduino 循迹小车程序电路出错故障求解
  • ¥20 Arduino 循迹小车程序电路出错故障求解
  • ¥100 AT89C52单片机C语言调试之后再回答
  • ¥15 AT89C52单片机C语言串口助手发送数据包返回值
  • ¥15 C++数组中找第二小的数字程序纠错
  • ¥15 wannier复现图像时berry曲率极值点与高对称点严重偏移
  • ¥15 利用决策森林为什么会出现这样·的问题(关键词-情感分析)
  • ¥15 DispatcherServlet.noHandlerFound No mapping found for HTTP request with URI[/untitled30_war_e