Daisy1233 2020-10-26 16:42 采纳率: 0%
浏览 110

C++ TCP socket编程 client代码

C++编码实现TCP ,效果如图
这是client的
图片说明
图片说明
这是server的
图片说明
图片说明
以下是server源代码
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include
#include
#include
#include
#include
#include
#include
#pragma comment (lib, "ws2_32.lib")

#define portnum 16000
#define BUFFER_SIZE 1024
#define BOARD_BUFFER_SIZE 100

using std::cout;
using namespace std;

vector Board;

int make_socket(int port)
{
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
{
cout << "DLL initializes error" << endl;
return 0;
}

SOCKET servSock = socket(AF_INET, SOCK_STREAM, 0);

sockaddr_in sockAddr;
memset(&sockAddr, 0, sizeof(sockAddr));   
sockAddr.sin_family = PF_INET;   
sockAddr.sin_addr.s_addr = INADDR_ANY;      
sockAddr.sin_port = htons(port);  

if (bind(servSock, (SOCKADDR*)&sockAddr, sizeof(SOCKADDR)) == SOCKET_ERROR)
{
    cout << "bind error!" << endl;
    return 0;
}

if (listen(servSock, 5) == SOCKET_ERROR)
{
    cout << "listen error!" << endl;
    return 0;
}
return servSock;

}

void POST(vector split_message)
{
int i;
for (i = 1; i < split_message.size()-1; i++)
{
if (Board.size() < BOARD_BUFFER_SIZE)
{
Board.push_back(split_message[i]);
}
}
}

void READ(int sClient)
{
int i;
cout << "Board.size():" << Board.size() << endl;
for (i = 0; i < Board.size(); i++)
{
cout << "reading: sending " << Board[i].c_str() << endl;
if (send(sClient, Board[i].c_str(), strlen(Board[i].c_str()), 0) < 0)
{
cout << "Server: Send meassage \"" << Board[i].c_str() << "\" failed." <<endl;
}
else
{
Sleep(100);
}
}
if (send(sClient, ".", sizeof("."), 0) < 0)
{
cout << "Server: Send meassage . failed." << endl;
}

}

void handle_accept(int sClient)
{

char ok_message[3] = "OK";
char error_message[31] = "ERROR - Command not understood";
char buffer[BUFFER_SIZE];

memset(buffer, 0, BUFFER_SIZE);
const char* delim = "\n";

while (true)
{
    // cout << "Receiving...\n" << endl;
    int len = recv(sClient, buffer, BUFFER_SIZE, 0);
    if (len <= 0)
    {
        continue;
    }

    cout << "Client send " << buffer << endl;

    if (strcmp(buffer, "QUIT\n") == 0)
    {
        if (send(sClient, ok_message, strlen(ok_message), 0) < 0)
        {
            cout << "Server: Send OK meassage failed." << endl;
        }
        break;
    }

    vector<string> split_message;
    char* p = strtok(buffer, delim);
    while ( p )
    {
        string s = p;
        split_message.push_back(s);
        p = strtok(NULL, delim);
    }
    cout << "Command" << split_message[0] << endl;
    if (split_message[0] == "POST")
    {
        POST(split_message);
        for (int i = 0; i < Board.size(); i++)
        {
            cout << "Board message " << i << ":" << Board[i] << endl;
        }
        if (send(sClient, ok_message, sizeof(ok_message), 0) < 0)
        {
            cout << "Server: Send OK meassage failed." << endl;
        }
    }
    else if (split_message[0] == "READ")
    {
        cout << "reading" << endl;
        READ(sClient);
    }
    else
    {
        if (send(sClient, error_message, sizeof(error_message), 0) < 0)
        {
            cout << "Server: Send error meassage failed." << endl;
        }
        else
        {
            continue;
        }
    }
    memset(buffer, 0, BUFFER_SIZE);  
}

closesocket(sClient);  

}

int main() {
int servSock = make_socket(portnum);
while (true)
{
int socket_feed = accept(servSock, nullptr, nullptr);
handle_accept(socket_feed);
}

WSACleanup();

return 0;

}

  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2022-09-20 17:56
    关注
    不知道你这个问题是否已经解决, 如果还没有解决的话:

    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^
    评论

报告相同问题?

悬赏问题

  • ¥15 欧拉系统opt目录空间使用100%
  • ¥15 ul做导航栏格式不对怎么改?
  • ¥20 用户端如何上传图片到服务器和数据库里
  • ¥15 现在研究生在烦开题,看了一些文献,但不知道自己要做什么,求指导。
  • ¥30 vivado封装时总是显示缺少一个dcp文件
  • ¥100 pxe uefi启动 tinycore
  • ¥15 我pycharm运行jupyter时出现Jupyter server process exited with code 1,然后打开cmd显示如下
  • ¥15 可否使用carsim-simulink进行四轮独立转向汽车的联合仿真,实现四轮独立转向汽车原地旋转、斜向形式、横移等动作,如果可以的话在carsim中如何进行相应设置
  • ¥15 Caché 2016 在Java环境通过jdbc 执行sql报Parameter list mismatch错误,但是同样的sql使用连接工具可以查询出数据
  • ¥15 疾病的获得与年龄是否有关