用c语言编写TCP客户端及服务器通讯程序
时间: 2023-07-15 10:13:01 浏览: 93
下面是一个简单的基于TCP协议的客户端和服务器通讯程序示例,使用C语言编写。
服务器端代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define SERVER_PORT 8080
#define MAX_MSG_SIZE 1024
int main()
{
// 创建套接字
int server_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (server_fd < 0)
{
perror("socket");
exit(EXIT_FAILURE);
}
// 绑定地址和端口号
struct sockaddr_in server_addr;
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_port = htons(SERVER_PORT);
if (bind(server_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)
{
perror("bind");
exit(EXIT_FAILURE);
}
// 监听连接
if (listen(server_fd, 5) < 0)
{
perror("listen");
exit(EXIT_FAILURE);
}
printf("Server started, waiting for client...\n");
// 接收连接请求并处理
while (1)
{
struct sockaddr_in client_addr;
socklen_t client_addr_len = sizeof(client_addr);
int client_fd = accept(server_fd, (struct sockaddr *)&client_addr, &client_addr_len);
if (client_fd < 0)
{
perror("accept");
continue;
}
printf("Client connected: %s:%d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
// 接收客户端消息并打印
char recv_buf[MAX_MSG_SIZE];
int recv_len = recv(client_fd, recv_buf, MAX_MSG_SIZE, 0);
if (recv_len < 0)
{
perror("recv");
close(client_fd);
continue;
}
recv_buf[recv_len] = '\0';
printf("Received message from client: %s\n", recv_buf);
// 发送响应消息
char send_buf[MAX_MSG_SIZE] = "Hello, client!";
int send_len = send(client_fd, send_buf, strlen(send_buf), 0);
if (send_len < 0)
{
perror("send");
close(client_fd);
continue;
}
printf("Response message sent to client: %s\n", send_buf);
// 关闭套接字
close(client_fd);
}
close(server_fd);
return 0;
}
```
客户端代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define SERVER_ADDR "127.0.0.1"
#define SERVER_PORT 8080
#define MAX_MSG_SIZE 1024
int main()
{
// 创建套接字
int client_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (client_fd < 0)
{
perror("socket");
exit(EXIT_FAILURE);
}
// 连接服务器
struct sockaddr_in server_addr;
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr(SERVER_ADDR);
server_addr.sin_port = htons(SERVER_PORT);
if (connect(client_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)
{
perror("connect");
exit(EXIT_FAILURE);
}
printf("Connected to server: %s:%d\n", SERVER_ADDR, SERVER_PORT);
// 发送消息给服务器
char send_buf[MAX_MSG_SIZE] = "Hello, server!";
int send_len = send(client_fd, send_buf, strlen(send_buf), 0);
if (send_len < 0)
{
perror("send");
close(client_fd);
exit(EXIT_FAILURE);
}
printf("Message sent to server: %s\n", send_buf);
// 接收服务器响应消息并打印
char recv_buf[MAX_MSG_SIZE];
int recv_len = recv(client_fd, recv_buf, MAX_MSG_SIZE, 0);
if (recv_len < 0)
{
perror("recv");
close(client_fd);
exit(EXIT_FAILURE);
}
recv_buf[recv_len] = '\0';
printf("Received message from server: %s\n", recv_buf);
// 关闭套接字
close(client_fd);
return 0;
}
```
阅读全文