基于c++的C/S网络通信 从入门到精通(三)
程序员文章站
2022-05-02 13:09:42
...
为了实现客户端与服务器的相互通信,这次我们将加入多线程:一个线程发送数据,一个线程接受数据
客户端代码如下:
#include <iostream>
#include <winsock2.h>
#pragma comment (lib,"WS2_32.lib")
using namespace std;
SOCKET clientsocket;
HANDLE hMutex;//互斥使用输出窗体
int flag_connect = 1;
DWORD WINAPI Fun_Recv(LPVOID);
DWORD WINAPI Fun_Send(LPVOID);
int main()
{
//加载套接字库
WSADATA wsadata;
int iRet=WSAStartup(MAKEWORD(2, 2), &wsadata);
if (iRet != 0)
{
cout << "套接字库加载失败" << endl;
return -1;
}
else
{
cout << "套接字库加载成功" << endl;
}
//创建套接字
clientsocket = socket(AF_INET, SOCK_STREAM, 0);
if (clientsocket == INVALID_SOCKET)
{
cout << "套接字创建失败" << endl;
return -1;
}
else
{
cout << "套接字创建成功" << endl;
}
//初始化客户端地址族变量
SOCKADDR_IN addrSrv;
addrSrv.sin_family = AF_INET;
addrSrv.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
addrSrv.sin_port = htons(8000);
//连接
iRet=connect(clientsocket, (SOCKADDR*)&addrSrv, sizeof(SOCKADDR));
if (iRet == SOCKET_ERROR)
{
cout << "连接失败" << endl;
return -1;
}
else
{
cout << "连接成功" << endl;
}
HANDLE Send_Thread = CreateThread(NULL, 0, Fun_Send, 0, 0, NULL);//创建发送线程
HANDLE Recv_Thread = CreateThread(NULL, 0, Fun_Recv, 0, 0, NULL);//创建接受线程
CloseHandle(Send_Thread);//关闭句柄
CloseHandle(Recv_Thread);//关闭句柄
hMutex = CreateMutex(NULL, FALSE, L"mutex");//创建互斥量
while (flag_connect)
{
Sleep(100);//必须使用sleep
}
return 0;
}
DWORD WINAPI Fun_Send(LPVOID)
{
while (flag_connect)
{
char sendBuff[255] ;
cin >> sendBuff;
int iRet = send(clientsocket, sendBuff, sizeof(sendBuff), 0);
if (iRet <= 0)
{
cout << "服务器关闭连接" << endl;
closesocket(clientsocket);
WSACleanup();
flag_connect = 0;
return -1;
}
}
return 0;
}
DWORD WINAPI Fun_Recv(LPVOID)
{
while (flag_connect)
{
char recvBuff[255];
int iRet = recv(clientsocket, recvBuff, 255, 0);
if (iRet <= 0)
{
cout << "服务器关闭连接" << endl;
closesocket(clientsocket);
WSACleanup();
flag_connect = 0;
return -1;
}
cout << recvBuff << endl;
}
return 0;
}
服务器端代码如下:
#include <iostream>
#include <winsock2.h>
#pragma comment (lib,"WS2_32.lib")
using namespace std;
SOCKET connectSocket;
SOCKET serversocket;
HANDLE hMutex;//互斥使用输出窗体
int flag_connect = 1;
DWORD WINAPI Fun_Recv(LPVOID);
DWORD WINAPI Fun_Send(LPVOID);
int main()
{
//加载套接字库
WSADATA wsadata;
int iRet=WSAStartup(MAKEWORD(2, 2), &wsadata);
if (iRet != 0)
{
cout << "套接字库加载失败" << endl;
return -1;
}
//创建套接字
serversocket = socket(AF_INET, SOCK_STREAM, 0);
if (serversocket == INVALID_SOCKET)
{
cout << "套接字创建失败" << endl;
return -1;
}
else
{
cout << "套接字创建成功" << endl;
}
//初始化服务器地址族变量
SOCKADDR_IN addrSrv;
addrSrv.sin_family = AF_INET;
addrSrv.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
addrSrv.sin_port = htons(8000);
//绑定
iRet = bind(serversocket, (SOCKADDR*)&addrSrv, sizeof(SOCKADDR));
if (iRet == SOCKET_ERROR)
{
cout << "绑定失败" << endl;
return -1;
}
else
{
cout << "绑定成功" << endl;
}
//监听
iRet = listen(serversocket, 10);
if (iRet == SOCKET_ERROR)
{
cout << "开启监听失败" << endl;
return -1;
}
else
{
cout << "开启监听成功" << endl;
}
//接收连接
SOCKADDR_IN addrClient;
int len = sizeof(SOCKADDR);
connectSocket=accept(serversocket, (SOCKADDR*)&addrClient, &len);
if (INVALID_SOCKET == connectSocket)
{
cout << "接受连接失败" << endl;
return -1;
}
else
{
cout << "接受连接成功" << endl;
}
//接收数据
HANDLE Send_Thread = CreateThread(NULL, 0, Fun_Send, 0, 0, NULL);//创建发送线程
HANDLE Recv_Thread = CreateThread(NULL, 0, Fun_Recv, 0, 0, NULL);//创建接受线程
CloseHandle(Send_Thread);//关闭句柄
CloseHandle(Recv_Thread);//关闭句柄
hMutex = CreateMutex(NULL, FALSE, L"mutex");//创建互斥量
int i = 0;
while (flag_connect)
{
Sleep(100);//必须使用Sleep
}
return 0;
}
DWORD WINAPI Fun_Send(LPVOID)
{
while (flag_connect)
{
char sendBuff[255];
cin >> sendBuff;
int iRet = send(connectSocket, sendBuff, sizeof(sendBuff), 0);
if (iRet <= 0)
{
cout << "发送线程关闭连接" << endl;
flag_connect = 0;
return -1;
}
}
return 0;
}
DWORD WINAPI Fun_Recv(LPVOID)
{
while (flag_connect)
{
char recvBuff[255];
int iRet = recv(connectSocket, recvBuff, 255, 0);
if (iRet <= 0)
{
cout << "接受线程关闭连接" << endl;
closesocket(serversocket);
WSACleanup();
flag_connect = 0;
return -1;
}
cout << recvBuff << endl;
}
return 0;
}
问题:如果是多个客户端又该如何实现呢?
打包代码下载地址:阻塞式CS通信
上一篇: 队列阻塞浅析