tcp客户端,tcp服务端测试

This commit is contained in:
flykhan 2023-09-06 18:06:03 +08:00
parent a6ab78e97a
commit f343b5a0a8
2 changed files with 160 additions and 0 deletions

74
day3/tcp_c1.c Normal file
View File

@ -0,0 +1,74 @@
// tcp_s1 的客户端
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h> // bzero
#include <stdlib.h> // atoi
int main(int argc, char const *argv[])
{
if (argc < 3)
{
printf("usage: %s server_ip port\n", argv[0]);
return 1;
}
// TCP 的 socket 套接字类型 SOCK_STREAM
int sock_fd = socket(AF_INET, SOCK_STREAM, 0);
if (sock_fd < 0)
{
perror("socket");
return 1;
}
// 连接服务端
struct sockaddr_in server_addr;
bzero(&server_addr, 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(sock_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) != 0)
{
perror("connect");
close(sock_fd);
return 1;
}
printf("connect ok\n");
while (1)
{
char buf[128] = "";
scanf("%s", buf); // 从键盘取数据
int send_len = send(sock_fd, buf, strlen(buf), 0);
if (send_len == -1)
{
perror("send");
break;
}
if (strncmp(buf, "bye", 3) == 0)
{
break;
}
if (strncmp(buf, "clear", 5) == 0)
{
system("clear");
}
memset(buf, 0, 128);
send_len = recv(sock_fd, buf, 128, 0);
if (send_len > 0)
{
printf("%s(%s): %s\n", argv[1], argv[2], buf);
}
}
close(sock_fd);
return 0;
}

86
day3/tcp_s1.c Normal file
View File

@ -0,0 +1,86 @@
// tcp_c1 的服务端
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h> // bzero
#include <stdlib.h> // atoi
int main(int argc, char const *argv[])
{
if (argc != 2)
{
printf("usage: %s port\n", argv[0]);
return 1;
}
// TCP 的 socket 套接字类型 SOCK_STREAM
int sock_fd = socket(AF_INET, SOCK_STREAM, 0);
if (sock_fd < 0)
{
perror("socket");
return 1;
}
// 设置绑定绑定服务器的IP和端口号
struct sockaddr_in server_addr;
bzero(&server_addr, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_port = htons(atoi(argv[1]));
bind(sock_fd, (struct sockaddr *)&server_addr, sizeof(server_addr));
// 创建监听队列
if (listen(sock_fd, 50) == -1)
{
perror("listen");
}
// 接收客户端的连接
struct sockaddr_in client_addr;
bzero(&client_addr, sizeof(client_addr));
socklen_t client_addr_len = sizeof(client_addr);
// 阻塞到有客户端连接为止
int client_fd = accept(sock_fd, (struct sockaddr *)&client_addr, &client_addr_len);
// 客户端 ip
char client_ip[16] = "";
inet_ntop(AF_INET, &client_addr.sin_addr.s_addr, client_ip, 16);
printf("%s(%d) 连接成功\n", client_ip, ntohs(client_addr.sin_port));
while (1)
{
// 接受(读取客户端数据)
char buf[128] = "";
ssize_t recv_len = read(client_fd, buf, 128); // 用 recv 也可以
// ssize_t recv_len = recv(client_fd, buf, 128, 0);
if (recv_len > 0)
{
printf("%s say: %s\n", client_ip, buf);
}
if (strncmp(buf, "bye", 3) == 0)
{
break;
}
memset(buf, 0, 128);
scanf("%s", buf);
// if (send(client_fd, buf, strlen(buf) + 1, 0) != -1) // 用 send 也可以
if (write(client_fd, buf, strlen(buf) + 1) != -1) // 用 send 也可以
{
printf("发送数据 OK\n");
}
}
close(client_fd);
close(sock_fd);
printf("----over----\n");
return 0;
}