使用C++搭建TCP服务器和客户端
时间: 2024-05-02 22:23:28 浏览: 17
下面是一个简单的TCP服务器和客户端的实现示例:
```c
// 服务器端代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define PORT 8080
#define BUFFER_SIZE 1024
int main(int argc, char const *argv[])
{
int sockfd, newsockfd, valread;
struct sockaddr_in serv_addr, cli_addr;
socklen_t cli_addr_len;
char buffer[BUFFER_SIZE] = {0};
// 创建套接字
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1) {
perror("socket");
exit(EXIT_FAILURE);
}
// 设置服务器地址
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(PORT);
// 绑定套接字到指定端口
if (bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) == -1) {
perror("bind");
exit(EXIT_FAILURE);
}
// 监听套接字
if (listen(sockfd, 5) == -1) {
perror("listen");
exit(EXIT_FAILURE);
}
printf("Server listening on port %d...\n", PORT);
// 接收连接请求
cli_addr_len = sizeof(cli_addr);
newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, &cli_addr_len);
if (newsockfd == -1) {
perror("accept");
exit(EXIT_FAILURE);
}
printf("Client connected!\n");
// 读取客户端发送的数据
valread = read(newsockfd, buffer, BUFFER_SIZE);
printf("Received message from client: %s\n", buffer);
// 发送响应数据给客户端
const char *response = "Hello, client!";
if (send(newsockfd, response, strlen(response), 0) == -1) {
perror("send");
exit(EXIT_FAILURE);
}
printf("Response sent to client!\n");
// 关闭套接字
close(newsockfd);
close(sockfd);
return 0;
}
```
```c
// 客户端代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define PORT 8080
#define BUFFER_SIZE 1024
int main(int argc, char const *argv[])
{
int sockfd, valread;
struct sockaddr_in serv_addr;
char buffer[BUFFER_SIZE] = {0};
// 创建套接字
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1) {
perror("socket");
exit(EXIT_FAILURE);
}
// 设置服务器地址
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
if (inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr) == -1) {
perror("inet_pton");
exit(EXIT_FAILURE);
}
// 连接服务器
if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) == -1) {
perror("connect");
exit(EXIT_FAILURE);
}
printf("Connected to server!\n");
// 发送数据到服务器
const char *message = "Hello, server!";
if (send(sockfd, message, strlen(message), 0) == -1) {
perror("send");
exit(EXIT_FAILURE);
}
printf("Message sent to server!\n");
// 读取服务器响应数据
valread = read(sockfd, buffer, BUFFER_SIZE);
printf("Received message from server: %s\n", buffer);
// 关闭套接字
close(sockfd);
return 0;
}
```
以上代码演示了客户端向服务器发送一条消息,服务器接收到消息后回复一条消息。可以根据实际需求进行修改。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)