如下代码:
#ifdef WIN32
#include <Windows.h>
#define socklen_t int
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <arpa/inet.h>
#define closesocket close
#endif
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <thread>
using namespace std;
class TcpThread
{
public:
void Main()
{
char buffer[1024] = { 0 };
for (;;)
{
int recvlen = recv(client, buffer, sizeof(buffer) - 1, 0); //第四个参数这个0是flag和系统相关,有很多自定一的操作,我们这边0就是不对他设置
if (recvlen <= 0)
break;
buffer[recvlen] = '\0';
if (strstr(buffer, "quit") != NULL)
{
char re[] = "quit success!\n";
send(client, re, strlen(re) + 1, 0);
break;
}
int sendlen = send(client, "ok\n", 4, 0); //参数分析,第三个参数设置三个字节,第四个参数flag和操作系统有关,这里先写为0
printf("recv %s\n", buffer);