您的位置:首页 > 理论基础 > 计算机网络

Linux c 通过TCP协议实现多个客户端连接服务器

2017-09-13 15:47 357 查看
通过父子进程实现TCP的多个客户端连接服务器 

tcp_sever_fork.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <arpa/inet.h>
4000

#define PORT 3333

int main()
{
int sockfd;
int sock_fd;
struct sockaddr_in server_addr;
struct sockaddr_in client_addr;
int addrlen = sizeof(struct sockaddr);
int addrinlen = sizeof(struct sockaddr_in);
int recvbytes;
char buf[1024];

//服务器创建套接字
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
fprintf(stderr, "socket error:%s\n", strerror(errno));
exit(1);
}
//服务器填充sockaddr结构
bzero(&server_addr, addrinlen);
server_addr.sin_family = AF_INET;
//server_addr.sin_addr.s_addr = htonl(INADDR_ANY);//INADDR_ANY表示可以接收任意IP地址的数据,即绑定所有的IP
server_addr.sin_addr.s_addr = inet_addr("192.168.1.222");//用于绑定到一个固定的IP,inet_addr用于把数字加格式的IP转化为整形IP
server_addr.sin_port = htons(PORT);

//绑定socket到IP地址
if(bind(sockfd, (struct sockaddr *)&server_addr, addrlen) < 0)
{
fprintf(stderr, "bind error:%s\n", strerror(errno));
exit(1);
}

//设置允许连接的最大客户端数目
if(listen(sockfd, 5) < 0)
{
fprintf(stderr, "listen error:%s\n", strerror(errno));
exit(1);
}

while(1)
{
//建立连接
if((sock_fd = accept(sockfd, (struct sockaddr *)&client_addr, &addrinlen)) < 0)
{
fprintf(stderr, "accept error:%s\n", strerror(errno));
exit(1);
}
if(fork() == 0)
{
if((recvbytes = recv(sock_fd, buf, 1024, 0)) < 0)
{
fprintf(stderr, "recv error:%s\n", strerror(errno));
exit(1);
}
buf[recvbytes] = '\0';
printf("buf is %s\n", buf);
close(sockfd);
close(sock_fd);
exit(0);
}
else
{
//通讯结束
close(sock_fd);
}
}

//关闭
close(sockfd);

return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81

tcp_client_fork.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <arpa/inet.h>

#define PORT 3333

int main()
{
int sockfd;
struct sockaddr_in server_addr;
int addrlen = sizeof(struct sockaddr);
int addrinlen = sizeof(struct sockaddr_in);
char buf[512];
char buf1[1024];

//客户端程序创建套接字
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
fprintf(stderr, "socket error:%s\n", strerror(errno));
exit(1);
}
//客户端程序填充服务器的资料
bzero(&server_addr, addrlen);//初始化,置0
server_addr.sin_family = AF_INET;//IPv4
//server_addr.sin_addr.s_addr = htonl(INADDR_ANY);//
server_addr.sin_addr.s_addr = inet_addr("192.168.1.222");
server_addr.sin_port = htons(PORT);//将本机的short数据转化为网络上的short数据——端口号

//服务器发起连接请求
if(connect(sockfd, (struct sockaddr *)&server_addr, addrlen) < 0)
{
fprintf(stderr, "bind error:%s\n", strerror(errno));
exit(1);
}

//while(1)
//{
//连接成功
printf("Please input:\n");
fgets(buf, 512, stdin);

//发送数据
if(send(sockfd, buf, 512, 0) < 0)
{
fprintf(stderr, "listen error:%s\n", strerror(errno));
close(sockfd);
exit(1);
}

//write(sockfd, buf, strlen(buf));
#if 0
memset(buf1, 0, sizeof(buf1));
if(read(sockfd, buf1, sizeof(buf1)) < 0)
{
fprintf(stderr, "listen error:%s\n", strerror(errno));
exit(1);
}
printf("read is %s\n", buf1);
#endif
// }
//关闭套接字
close(sockfd);

return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69

运行结果: 





内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐