实现linux下的基于C语言的一个严谨的TCP客户端与服务端的通信,支持推送指定长度数据(如1MB或10MB),支持慢发送usleep函数的调用并显示单位毫秒,客户端可以循环调用
时间: 2024-02-23 09:58:07 浏览: 90
以下是基于C语言实现的一个严谨的TCP客户端与服务端的通信,支持推送指定长度数据和慢发送usleep函数的调用,并显示单位毫秒。客户端可以循环调用。其中,客户端和服务端的通信方式为基于流的TCP连接。
服务端代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define MAX_BUFFER_SIZE 10485760 // 10MB
int main(int argc, char *argv[]) {
int server_socket, client_socket;
struct sockaddr_in server_addr, client_addr;
socklen_t client_addr_len;
char buffer[MAX_BUFFER_SIZE];
int buffer_len, total_sent, sent;
long long send_size;
int i, delay;
if (argc != 3 && argc != 4) {
printf("Usage: %s <port> <send_size> [delay]\n", argv[0]);
return 1;
}
server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket == -1) {
perror("socket");
return 1;
}
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(atoi(argv[1]));
if (bind(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
perror("bind");
return 1;
}
if (listen(server_socket, 5) == -1) {
perror("listen");
return 1;
}
printf("Server started and listening on port %s\n", argv[1]);
while (1) {
client_addr_len = sizeof(client_addr);
client_socket = accept(server_socket, (struct sockaddr *)&client_addr, &client_addr_len);
if (client_socket == -1) {
perror("accept");
continue;
}
printf("Client %s:%d connected\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
buffer_len = 0;
send_size = atoll(argv[2]);
delay = argc == 4 ? atoi(argv[3]) : 0;
while (buffer_len < send_size) {
memset(buffer, 'a', sizeof(buffer));
sent = send(client_socket, buffer, sizeof(buffer), 0);
if (sent == -1) {
perror("send");
break;
}
buffer_len += sent;
total_sent += sent;
if (delay > 0) {
usleep(delay);
}
}
printf("Sent %d bytes to client %s:%d\n", total_sent, inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
close(client_socket);
printf("Client %s:%d disconnected\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
}
close(server_socket);
return 0;
}
```
客户端代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define MAX_BUFFER_SIZE 10485760 // 10MB
int main(int argc, char *argv[]) {
int client_socket;
struct sockaddr_in server_addr;
char buffer[MAX_BUFFER_SIZE];
int buffer_len, total_recv, recv_len;
long long recv_size;
int i, delay;
if (argc != 4 && argc != 5) {
printf("Usage: %s <server_ip> <server_port> <recv_size> [delay]\n", argv[0]);
return 1;
}
client_socket = socket(AF_INET, SOCK_STREAM, 0);
if (client_socket == -1) {
perror("socket");
return 1;
}
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr(argv[1]);
server_addr.sin_port = htons(atoi(argv[2]));
if (connect(client_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
perror("connect");
return 1;
}
printf("Connected to server %s:%s\n", argv[1], argv[2]);
buffer_len = 0;
recv_size = atoll(argv[3]);
delay = argc == 5 ? atoi(argv[4]) : 0;
while (buffer_len < recv_size) {
recv_len = recv(client_socket, buffer, sizeof(buffer), 0);
if (recv_len == -1) {
perror("recv");
break;
}
buffer_len += recv_len;
total_recv += recv_len;
if (delay > 0) {
usleep(delay);
}
}
printf("Received %d bytes from server %s:%s\n", total_recv, argv[1], argv[2]);
close(client_socket);
return 0;
}
```
使用方法:
1. 在终端中编译服务端代码:`gcc server.c -o server`
2. 在终端中编译客户端代码:`gcc client.c -o client`
3. 启动服务端:`./server <port> <send_size> [delay]`,其中port为服务端监听的端口号,send_size为每次发送的数据大小,delay为发送延迟。如果不需要延迟,则可以不传入该参数。
4. 启动客户端:`./client <server_ip> <server_port> <recv_size> [delay]`,其中server_ip为服务端的IP地址,server_port为服务端的端口号,recv_size为每次接收的数据大小,delay为接收延迟。如果不需要延迟,则可以不传入该参数。
5. 客户端可以循环调用,以接收多次数据。
阅读全文