#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就是连不了上面这个错误服务器,实在找不到问题在哪了,求解答。