TCP并发服务器

client.c
 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <sys/types.h>
5 #include <sys/socket.h>
6 #include <arpa/inet.h>
7 #include <netinet/in.h>
8 #include <string.h>
9
10 #define N 128
11 #define ERR_LOG(errmsg) do{\
12 perror(errmsg);\
13 exit(1);\
14 }while(0)
15
16 int main(int argc, char const *argv[])
17 {
18 if(argc < 3)
19 {
20 fprintf(stderr, "Usage: %s <server_ip> <server_port>\n", argv[0]);
21 exit(1);
22 }
23
24 int sockfd;
25 struct sockaddr_in serveraddr;
26
27 //第一步:创建套接字
28 if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
29 {
30 ERR_LOG("fail to socket");
31 }
32
33 //第二步:填充服务器网络信息结构体
34 serveraddr.sin_family = AF_INET;
35 serveraddr.sin_addr.s_addr = inet_addr(argv[1]);
36 serveraddr.sin_port = htons(atoi(argv[2]));
37
38 //第三步:发送客户端连接请求
39 if(connect(sockfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr))
40 {
41 ERR_LOG("fail to connect");
42 }
43
44 //第四步:进行通信
45 char buf[N] = "";
46 while(1)
47 {
48 fgets(buf, N, stdin);
49 buf[strlen(buf) ‐ 1] = '\0';
50
51 if(send(sockfd, buf, N, 0) < 0)
52 {
53 ERR_LOG("fail to send");
54 }
55
56 if(strncmp(buf, "quit", 4) == 0)
57 {
58 exit(0);
59 }
60
61 if(recv(sockfd, buf, N, 0) < 0)
62 {
63 ERR_LOG("fail to recv");
64 }
65
66 printf("from server: %s\n", buf);
67 }
68
69 //第四步:关闭套接字
70 close(sockfd);
71
72 return 0;
73 }
server.c
//tcp服务器的实现
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <sys/types.h>
6 #include <sys/socket.h>
7 #include <arpa/inet.h>
8 #include <netinet/in.h>
9 #include <string.h>
10 #include <signal.h>
11
12 #define N 128
13 #define ERR_LOG(errmsg) do{\
14 perror(errmsg);\
15 exit(1);\
16 }while(0)
17
18 int main(int argc, char const *argv[])
19 {
20 if(argc < 3)
21 {
22 fprintf(stderr, "Usage: %s <server_ip> <server_port>\n", argv[0]);
23 exit(1);
24 }
25
26 int sockfd, acceptfd;
27 struct sockaddr_in serveraddr, clientaddr;
28 socklen_t addrlen = sizeof(serveraddr);
29
30 //第一步:创建套接字
31 if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
32 {
33 ERR_LOG("fail to socket");
34 }
35
36 //将套接字设置为允许重复使用本机地址或者设置为端口复用
37 int on = 1;
38 if(setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
39 {
40 ERR_LOG("fail to setsockopt");
41 }
42
43 //第二步:填充服务器网络信息结构体
44 serveraddr.sin_family = AF_INET;
45 serveraddr.sin_addr.s_addr = inet_addr(argv[1]);
46 serveraddr.sin_port = htons(atoi(argv[2]));
47
48 //第三步:将套接字与服务器网络信息结构体绑定
49 if(bind(sockfd, (struct sockaddr *)&serveraddr, addrlen) < 0)
50 {
51 ERR_LOG("fail to bind");
52 }
53
54 //第四步:将套接字设置为被动监听状态
55 if(listen(sockfd, 5) < 0)
56 {
57 ERR_LOG("fail to listen");
58 }
59
60 //第五步:阻塞等待客户端的连接请求
61 if((acceptfd = accept(sockfd, (struct sockaddr *)&clientaddr, &addrl
en)) < 0)
62 {
63 ERR_LOG("fail to accept");
64 }
65
66 //打印客户端的信息
67 printf("%s ‐‐ %d\n", inet_ntoa(clientaddr.sin_addr), ntohs(clientadd
r.sin_port));
68
69 //第六步:进行通信
70 char buf[N] = "";
71 ssize_t bytes;
72 while (1)
73 {
74 //注意:tcp中服务器与客户端通信时,一定要用accept函数的返回值来通信
75 if((bytes = recv(acceptfd, buf, N, 0)) < 0)
76 {
77 ERR_LOG("fail to recv");
78 }
79 else if(bytes == 0)
80 {
81 printf("The client quited\n");
82 exit(1);
83 }
84
85 if(strncmp(buf, "quit", 4) == 0)
86 {
87 exit(0);
88 }
89
90 printf("from client: %s\n", buf);
91
92 strcat(buf, " ^_^");
93 if(send(acceptfd, buf, N, 0) < 0)
94 {
95 ERR_LOG("fail to send");
96 }
97 }
98
99 //第七步:关闭套接字
100 close(acceptfd);
101 close(sockfd);
102
103 return 0;
104 }

执行结果

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值