多线程版本的的socket的server端
#include "socket.hpp"
#include <iostream>
#include <string>
#include <memory>
DWORD WINAPI threadProc(LPVOID lp)
{
SOCKET sClient = *(SOCKET*)(lp);
while (true) {
char buff[1024] = { 0 };
int result = recv(sClient, buff, 1024, 0);
if (result > 0) {
std::cout << "接收到了数据" << buff << std::endl;
}
else
{
std::cout << "客户端断开连接" << std::endl;
closesocket(sClient);
break;
}
}
//closesocket(sClient);
return NULL;
}
int main()
{
SocketInit socketInit;
//创建监听套接字
SOCKET sListen = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sListen == SOCKET_ERROR)
{
std::cout << "监听失败" << std::endl;
}
//绑定套接字
sockaddr_in sock_in;
sock_in.sin_family = AF_INET;
sock_in.sin_port = htons(1234);
sock_in.sin_addr.S_un.S_addr = INADDR_ANY;
int ret = bind(sListen, (sockaddr*)(&sock_in), sizeof(sock_in));
if (ret == SOCKET_ERROR)
{
std::cout << "绑定套接字失败" << std::endl;
closesocket(sListen);
return -1;
}
//
if (listen(sListen, 10) == SOCKET_ERROR)
{
std::cout << "监听失败" << std::endl;
return -1;
}
sockaddr_in sock_client;
int nlen = sizeof(sockaddr_in);
while (true)
{
//接受客户端的连接
SOCKET sClient = accept(sListen, (sockaddr*)(&sock_client), &nlen);
if (sClient == SOCKET_ERROR)
{
std::cout << "接收客户端失败" << std::endl;
closesocket(sListen);
return -1;
}
std::cout << "与客户端连接成功...." << std::endl;
CreateThread(NULL, 0, threadProc, (LPVOID*)(&sClient), NULL, NULL);
}
closesocket(sListen);
getchar();
return 0;
}
poll和epoll只能在linux环境下适用
select模型 代码
#define _WINSOCK_DEPRECATED_NO_WARNINGS
/*
select是一个I/O多路复用的系统调用函数,主要用于处理多个文件描述符的读写操作。
它常常被用于网络编程中,可以同时监听多个文件描述符的状态变化,从而实现高并发的网络通信。
以下是一个在Windows平台上使用select模型的demo代码:
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <WinSock2.h>
#pragma comment(lib, "ws2_32.lib")
#define BUF_SIZE 1024
#define PORT 1234
int main() {
// 初始化Winsock2库
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
printf("WSAStartup() failed!\n");
exit(EXIT_FAILURE);
}
// 创建Socket套接字
SOCKET listen_sock = socket(AF_INET, SOCK_STREAM, 0);
if (listen_sock == INVALID_SOCKET) {
printf("socket() failed: %d\n", WSAGetLastError());
exit(EXIT_FAILURE);
}
// 绑定到本地端口
sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(PORT);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(listen_sock, (sockaddr*)&addr, sizeof(addr)) < 0) {
printf("bind() failed: %d\n", WSAGetLastError());
exit(EXIT_FAILURE);
}
// 监听端口
if (listen(listen_sock, SOMAXCONN) < 0) {
printf("listen() failed: %d\n", WSAGetLastError());
exit(EXIT_FAILURE);
}
fd_set read_fds, tmp_fds;
FD_ZERO(&read_fds);
FD_SET(listen_sock, &read_fds);
printf("Server listening on port %d...\n", PORT);
while (1) {
tmp_fds = read_fds;
// 多路复用I/O
if (select(0, &tmp_fds, NULL, NULL, NULL) < 0) {
printf("select() failed: %d\n", WSAGetLastError());
exit(EXIT_FAILURE);
}
// 检查所有Socket文件描述符是否有可读事件
for (int i = 0; i < tmp_fds.fd_count; ++i) {
SOCKET sock = tmp_fds.fd_array[i];
// 如果有新的连接请求,接受并将新连接加入文件描述符集合
if (sock == listen_sock && FD_ISSET(listen_sock, &tmp_fds)) {
sockaddr_in clnt_addr;
int clnt_len = sizeof(clnt_addr);
SOCKET clnt_sock = accept(listen_sock, (sockaddr*)&clnt_addr, &clnt_len);
if (clnt_sock == INVALID_SOCKET) {
printf("accept() failed: %d\n", WSAGetLastError());
continue;
}
FD_SET(clnt_sock, &read_fds);
printf("New client connected: %s\n", inet_ntoa(clnt_addr.sin_addr));
// 如果是已连接的Socket,读取并处理数据
}
else {
char buf[BUF_SIZE] = { 0 };
int recv_len = recv(sock, buf, BUF_SIZE, 0);
if (recv_len <= 0) {
printf("Socket closed: %d\n", sock);
closesocket(sock);
FD_CLR(sock, &read_fds);
}
else {
printf("Received message from client[%d]: %s\n", sock, buf);
send(sock, buf, recv_len, 0);
}
}
}
}
closesocket(listen_sock);
WSACleanup();
return 0;
}
/*
这个demo实现了一个简单的TCP服务器,使用select
百宝箱助理 :
模型处理多个客户端的连接请求和数据读写操作。
其中主要的代码是在while循环中的select调用和对文件描述符事件的处理逻辑。
通过多路复用的方式,我们可以检查多个文件描述符的状态,并实现有限资源下的高并发网络通信。
*/
window平台多线程实现客户端收发数据分离
/*
以下是 Windows 平台下用多线程实现 Socket 客户端收发数据分离的示例代码:
*/
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <WinSock2.h>
#include <process.h>
#pragma comment(lib, "ws2_32.lib")
#define BUF_SIZE 1024
#define PORT 1234
// 子线程函数:接收服务端发来的数据并输出到控制台
unsigned __stdcall recv_thread(void* arg) {
SOCKET sock = *(SOCKET*)arg;
char buf[BUF_SIZE] = { 0 };
int recv_len = 0;
while ((recv_len = recv(sock, buf, BUF_SIZE, 0)) > 0) {
printf("Received message from server: %s\n", buf);
memset(buf, 0, sizeof(buf));
}
printf("Connection closed by server.\n");
return 0;
}
int main() {
// 初始化 Winsock2 库
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
printf("WSAStartup() failed!\n");
exit(EXIT_FAILURE);
}
// 创建 Socket 套接字
SOCKET sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == INVALID_SOCKET) {
printf("socket() failed: %d\n", WSAGetLastError());
exit(EXIT_FAILURE);
}
// 连接服务端
sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(PORT);
addr.sin_addr.s_addr = inet_addr("127.0.0.1");
if (connect(sock, (const sockaddr*)&addr, sizeof(addr)) < 0) {
printf("connect() failed: %d\n", WSAGetLastError());
exit(EXIT_FAILURE);
}
// 创建子线程用于接收数据
HANDLE recv_hThread = (HANDLE)_beginthreadex(NULL, 0, &recv_thread, &sock, 0, NULL);
if (recv_hThread == NULL) {
printf("Create thread failed: %d\n", GetLastError());
exit(EXIT_FAILURE);
}
// 主线程用于发送数据
char buf[BUF_SIZE] = { 0 };
printf("Please enter messages to send (input 'quit' to exit):\n");
while (fgets(buf, BUF_SIZE, stdin) != NULL) {
if(strcmp(buf, "quit\n") == 0) {
printf("Quit.\n");
break;
}
send(sock, buf, strlen(buf), 0);
memset(buf, 0, sizeof(buf));
}
// 等待子线程结束并释放资源
WaitForSingleObject(recv_hThread, INFINITE);
CloseHandle(recv_hThread);
closesocket(sock);
WSACleanup();
return 0;
}
/*
在本示例中,我们启动了一个子线程用于接收服务端的数据,
并将主线程用于发送数据。子线程在接收到数据后将数据输出到控制台,
而主线程则从标准输入中读取数据并发送给服务端。这样就达到了收发数据分离的目的,
主线程在输入数据时不会被阻塞,程序可以更加流畅地运行。
*/