在linux操作系统下,进行tcp通信示例

以下是一个简单的cs通信模型:

服务器:

// TCPServer.cpp  
#include <iostream>  
#include <thread>  
#include <cstring>  
#include <sys/socket.h>  
#include <netinet/in.h>  
#include <unistd.h>  
  
const int PORT = 8080;  
const int BUFFER_SIZE = 1024;  
  
void receive_data(int server_socket) {  
    char buffer[BUFFER_SIZE];  
    while (true) {  
        int valread = read(server_socket, buffer, BUFFER_SIZE);  
        if (valread == 0) {  
            std::cout << "Client disconnected." << std::endl;  
            break;  
        } else if (valread > 0) {  
            std::cout << "Received: " << buffer << std::endl;  
        } else {  
            perror("read failed");  
            break;  
        }  
    }  
}  
  
int main() {  
    int server_socket, client_socket;  
    struct sockaddr_in server_addr, client_addr;  
    socklen_t client_len = sizeof(client_addr);  
  
    server_socket = socket(AF_INET, SOCK_STREAM, 0);  
    if (server_socket < 0) {  
        perror("socket failed");  
        return 1;  
    }  
  
    memset(&server_addr, 0, sizeof(server_addr));  
    server_addr.sin_family = AF_INET;  
    server_addr.sin_port = htons(PORT);  
    server_addr.sin_addr.s_addr = INADDR_ANY;  
  
    if (bind(server_socket, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) {  
        perror("bind failed");  
        return 1;  
    }  
  
    if (listen(server_socket, 5) < 0) {  
        perror("listen failed");  
        return 1;  
    }  
  
    std::cout << "Server listening on port " << PORT << "..." << std::endl;  
  
    client_socket = accept(server_socket, (struct sockaddr*)&client_addr, &client_len);  
    if (client_socket < 0) {  
        perror("accept failed");  
        return 1;  
    }  
  
    std::thread receiver_thread(receive_data, client_socket);  
  
    // Send data in the main thread  
    char send_buffer[BUFFER_SIZE] = "Hello from server!";  
    while (true) {  
        int valwrite = write(client_socket, send_buffer, strlen(send_buffer));  
        if (valwrite < 0) {  
            perror("write failed");  
            break;  
        }  
        sleep(1); // Wait for 1 second before sending another message  
    }  
  
    receiver_thread.join();  
    close(client_socket);  
    close(server_socket);  
  
    return 0;  
}

客户端:

// TCPClient.cpp  
#include <iostream>  
#include <thread>  
#include <cstring>  
#include <sys/socket.h>  
#include <netinet/in.h>  
#include <unistd.h>  
#include <arpa/inet.h>
const char* SERVER_IP = "127.0.0.1";  
const int PORT = 8080;  
const int BUFFER_SIZE = 1024;  
  
void send_data(int client_socket) {  
    char send_buffer[BUFFER_SIZE] = "Hello from client!";  
    while (true) {  
        int valwrite = write(client_socket, send_buffer, strlen(send_buffer));  
        if (valwrite < 0) {  
            perror("write failed");  
            break;  
        }  
        sleep(1); // Wait for 1 second before sending another message  
    }  
}  
  
void receive_data(int client_socket) {  
    char buffer[BUFFER_SIZE];  
    while (true) {  
        int valread = read(client_socket, buffer, BUFFER_SIZE);  
        if (valread == 0) {  
            std::cout << "Server disconnected." << std::endl;  
            break;  
        } else if (valread > 0) {  
            std::cout << "Received: " << buffer << std::endl;  
        } else {  
            perror("read failed");  
            break;  
        }  
    }  
}  
  
int main() {  
    int client_socket;  
    struct sockaddr_in server_addr;  
  
    client_socket = socket(AF_INET, SOCK_STREAM, 0);  
    if (client_socket < 0) {  
        perror("socket failed");  
        return 1;  
    }  
  
    memset(&server_addr, 0, sizeof(server_addr));  
    server_addr.sin_family = AF_INET;  
    server_addr.sin_port = htons(PORT);  
    inet_pton(AF_INET, SERVER_IP, &server_addr.sin_addr);  
  
    if (connect(client_socket, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) {  
        perror("connect failed");  
        return 1;  
    }  
  
    std::thread sender_thread(send_data, client_socket);  
    std::thread receiver_thread(receive_data, client_socket);  
  
    sender_thread.join();  
    receiver_thread.join();  
  
    close(client_socket);  
  
    return 0;  
}

g++ -o TCPServer TCPServer.cpp
g++ -o TCPClient TCPClient.cpp

# 在一个终端中运行服务器 ./TCPServer

# 在另一个终端中运行客户端 ./TCPClient

运行结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黑暗森林里的葱

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值