我刚学网络通信,建立了一个客户端和一个服务端。
服务端运行没问题,
但是运行客户端时却和预期不一样,客户端闪了一下就退出了
这是服务器代码:
#include<iostream>
#include<WinSock2.h>
#include<stdlib.h>
#pragma comment(lib,"ws2_32.lib")
#pragma warning(disable:4996)
using namespace std;
int ires;
DWORD WINAPI threadpro(LPVOID pParam)
{
SOCKET hsock = (SOCKET)pParam;
char buffer[1024] = { 0 };
char sendBuffer[1024];
if (hsock != INVALID_SOCKET)
{
cout << "欢迎来到六(1)之家1.0\n你可以输入“我的班级”,“作者的名字”,“我们班的外号们”" << endl << endl << endl;
}
while (1)//循环接收发送内容
{
int num = recv(hsock, buffer, 1024, 0);//阻塞函数,等待接收内容
if (num >= 0)
{
cout << "RECEIVE FORM CLINET" << buffer << endl;
}
cout << WSAGetLastError() << endl;
if (!strcmp(buffer, "我的班级"))
{
memset(sendBuffer, 0, 1024);
strcpy(sendBuffer, "六(1)班");
ires = send(hsock, sendBuffer, sizeof(sendBuffer), 0);
cout << "SEND TO CLIENT" << sendBuffer << endl;
}
else if (!strcmp(buffer, "作者的名字"))
{
memset(sendBuffer, 0, 1024);
strcpy(sendBuffer, "张宇轩");
ires = send(hsock, sendBuffer, sizeof(sendBuffer), 0);
cout << "SEND TO CLIENT" << sendBuffer << endl;
}
else if (!strcmp(buffer, "我们班的外号们"))
{
memset(sendBuffer, 0, 1024);
strcpy(sendBuffer, "卷毛狮王\n廖蛋卷\n赖肥肠\n黑老蔡\n蔡子笨\n考拉\n村姑\n村长\n陈日立\n叶方贱\n赖嘎豪\n刘无敌");
ires = send(hsock, sendBuffer, sizeof(sendBuffer), 0);
cout << "SEND TO CLIENT" << sendBuffer << endl;
}
else
{
memset(sendBuffer, 0, 1024);
strcpy(sendBuffer, "你输入的似乎不正确");
ires = send(hsock, sendBuffer, sizeof(sendBuffer), 0);
}
}
}
int main()
{
WSADATA wsd;
DWORD err = WSAStartup(MAKEWORD(2, 2), & wsd);
cout << err << endl;
SOCKET m_SockServer;
sockaddr_in serveraddr;
sockaddr_in serveraddrfrom;
SOCKET m_Server[20];
serveraddr.sin_family = AF_INET; //设置服务器地址家族
serveraddr.sin_port = htons(4600); //设置服务器端口号
serveraddr.sin_addr.S_un.S_addr = inet_addr("192.168.1.1"); //书上填的是127.0.0.1
m_SockServer = socket(AF_INET, SOCK_STREAM, 0);
int i = bind(m_SockServer, (sockaddr*)&serveraddr, sizeof(serveraddr));
cout << "bind:" << i << endl;
int iMaxConnect = 20;
int iConnect = 0;
int iLisRet;
char WarnBuf[] = "It is voer Max connet\0";
char buf[] = "THIS IS SERVER\0";
int len = sizeof(sockaddr);
while (1)
{
iLisRet = listen(m_SockServer, 0);
m_Server[iConnect] = accept(m_SockServer, (sockaddr*)&serveraddrfrom, &len);
if (m_Server[iConnect], buf, sizeof(buf), 0)
{
ires = send(m_Server[iConnect], buf, sizeof(buf), 0);
cout << " ACCEPT" << ires << endl;
iConnect++;
if (iConnect > iMaxConnect)
{
ires = send(m_Server[iConnect], WarnBuf, sizeof(WarnBuf), 0);
}
else
{
HANDLE m_Handle;
DWORD nThreadId = 0;
m_Handle = (HANDLE)::CreateThread(NULL, 0, threadpro, (LPVOID)m_Server[--iConnect], 0, &nThreadId);
}
}
}
WSACleanup();
return 0;
}
在vs2022中是没有报错的
这是客户端代码,也没有报错:
#include<iostream>
#include<WinSock2.h>
#include<stdlib.h>
#pragma comment(lib,"ws2_32.lib")
#pragma warning(disable:4996)
using namespace std;
void main()
{
WSADATA wsd;
WSAStartup(MAKEWORD(2, 2), &wsd);
SOCKET m_SockClient;
sockaddr_in clientaddr;
clientaddr.sin_family = AF_INET; //设置服务器地址家族
clientaddr.sin_port = htons(4600); //设置服务器端口号
clientaddr.sin_addr.S_un.S_addr = inet_addr("192.168.1.1");
m_SockClient = socket(AF_INET, SOCK_STREAM, 0);
int i = connect(m_SockClient, (sockaddr*)&clientaddr, sizeof(clientaddr));
cout << "connect" << i << endl;
char buffer[1024];
char inBuf[1024];
int num;
num = recv(m_SockClient, buffer, 1024, 0);
if (num > 0)
{
cout << "欢迎来到六(1)之家1.0\n你可以输入“我的班级”,“作者的名字”,“我们班的外号们”,输入exit退出" << buffer << endl;
while (1)
{
num = 0;
cin >> inBuf;
if (!strcmp(inBuf, "exit"))
{
send(m_SockClient, inBuf, sizeof(inBuf), 0);
return;
}
send(m_SockClient, inBuf, sizeof(inBuf), 0);
num = recv(m_SockClient, buffer, 1024, 0);
if (num > 0) {
cout << "回答:" << buffer << endl;
}
}
}
}
但是运行结果却是这样的(我先启动服务端,再启动客户端):
服务端:
客户端闪一下就不见了,也没见输出什么内容
有大佬能解释一下吗?
在线等,急!!!