一.概述:本次练习的是linux下的TAP/IP套接字多进程与多线程编程,功能只实现了对话。话不多说,直接上代码。
二.多进程:
server.c:
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
1
/****************************************
2 > File Name:server.c
3 > Author:xiaoxiaohui
4 > mail:1924224891@qq.com
5 > Created Time:2016年05月15日 星期日 16时06分03秒
6 ****************************************/
7
8 #include<stdio.h>
9 #include<stdlib.h>
10 #include<sys/types.h>
11 #include<sys/socket.h>
12 #include<unistd.h>
13 #include <arpa/inet.h>
14 #include<netinet/in.h>
15 #include<fcntl.h>
16 #include<string.h>
17 #include<fcntl.h>
18
19 #define LEN 1024
20
const
int
PORT = 8080;
21
int
listenSock, linkSock;
22
struct
sockaddr_in lockal;
23
struct
sockaddr_in client;
24
char
buf[LEN];
25
26
27
void
ListenSock()
//建立一个已链接套接字
28 {
29 listenSock = socket(AF_INET, SOCK_STREAM, 0);
//返回一个文件描述符
30
31 lockal.sin_family = AF_INET;
32 lockal.sin_addr.s_addr = htonl(INADDR_ANY);
//适合多网卡
33 lockal.sin_port = htons(PORT);
34
if
( bind(listenSock, (
struct
sockaddr*)&lockal,
sizeof
(lockal)) < 0)
//绑定本地地址
35 {
36
perror
(
"bind"
);
37
exit
(0);
38 }
39
40
if
( listen(listenSock, 5) < 0)
//进入监听状态
41 {
42
perror
(
"listen"
);
43
exit
(1);
44 }
45 }
46
47
void
LinkSock()
48 {
49
int
size =
sizeof
(lockal);
50 linkSock = accept(listenSock, (
struct
sockaddr*)&client, &size);
//创建一个已链接套接字
51
if
(linkSock < 0)
52 {
53
perror
(
"accept"
);
54 }
55
else
56 {
57
printf
(
"connect success ip is :%s port is : %d\n"
,inet_ntoa(client.sin_addr),ntohs(client.sin_port));
58 }
59 }
60
61
int
main()
62 {
63 ListenSock();
64
while
(1)
65 {
66 LinkSock();
67
68 pid_t pid = fork();
69
70
if
(pid == 0)
71 {
72 close(listenSock);
73
while
(1)
74 {
75
int
ret = 0;
76 ret = read(linkSock, buf, LEN);
77 buf[ret] =
'\0'
;
78
printf
(
"client->ip:%s port: %d #%s\n"
,inet_ntoa(client.sin_addr),\
79 ntohs(client.sin_port), buf);
80 }
81 }
82
else
if
(pid > 0)
83 {
84 close(linkSock);
85 }
86
else
87 {
88
perror
(
"fork"
);
89
continue
;
90 }
91 }
92
93
return
0;
94 }
95
96
|
client.c:
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
|
1
/****************************************
2 > File Name:client.c
3 > Author:xiaoxiaohui
4 > mail:1924224891@qq.com
5 > Created Time:2016年05月15日 星期日 16时48分21秒
6 ****************************************/
7
8
9 #include<stdio.h>
10 #include<stdlib.h>
11 #include<sys/types.h>
12 #include<sys/socket.h>
13 #include<unistd.h>
14 #include <arpa/inet.h>
15 #include<netinet/in.h>
16 #include<fcntl.h>
17 #include<string.h>
18 #include<fcntl.h>
19
const
int
PORT = 8080;
20
const
int
LEN = 1024;
21
int
clientSock;
22
struct
sockaddr_in server;
23
24
25
26
void
LinkSock()
//创建一个以链接套接字
27 {
28 clientSock = socket(AF_INET, SOCK_STREAM, 0);
29
30 server.sin_family = AF_INET;
31 server.sin_addr.s_addr = inet_addr(
"127.0.0.1"
);
32 server.sin_port = htons(PORT);
33
if
( connect(clientSock, (
struct
sockaddr*)&server,
sizeof
(server)) < 0)
34 {
35
perror
(
"connect"
);
36
exit
(0);
37 }
38
else
39 {
40
printf
(
"connect success! ip:%s port:%d\n"
, inet_ntoa(server.sin_addr), ntohs(server.sin_port));
41 }
42 }
43
44
int
main()
45 {
46
47
char
buf[LEN];
48 LinkSock();
//得到一个已链接套接字
49
50
while
(1)
51 {
52
int
ret = 0;
53
printf
(
"请选择->"
);
54
gets
(buf);
55
56 write(clientSock, buf,
strlen
(buf));
//把选择的命令发到服务器
57
58
//memset(buf, '\0', LEN);
59
//ret = read(clientSock, buf, LEN - 1 ); //接受服务器的信息
60
//buf[ret] = '\0';
61
//printf("%s", buf);
62
//printf("\n");
63 }
64
65
return
0;
66 }
67
|
执行结果:
三.多线程:
server.c:
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
82
83
84
85
86
87
88
89
90
|
1
/****************************************
2 > File Name:server.c
3 > Author:xiaoxiaohui
4 > mail:1924224891@qq.com
5 > Created Time:2016年05月15日 星期日 16时06分03秒
6 ****************************************/
7
8 #include<stdio.h>
9 #include<stdlib.h>
10 #include<sys/types.h>
11 #include<sys/socket.h>
12 #include<unistd.h>
13 #include <arpa/inet.h>
14 #include<netinet/in.h>
15 #include<fcntl.h>
16 #include<string.h>
17 #include<fcntl.h>
18 #include<pthread.h>
19
20 #define LEN 1024
21
const
int
PORT = 8080;
22
int
listenSock, linkSock;
23
struct
sockaddr_in lockal;
24
struct
sockaddr_in client;
25
char
buf[LEN];
26
27
28
void
ListenSock()
//建立一个已链接套接字
29 {
30 listenSock = socket(AF_INET, SOCK_STREAM, 0);
//返回一个文件描述符
31
32 lockal.sin_family = AF_INET;
33 lockal.sin_addr.s_addr = htonl(INADDR_ANY);
//适合多网卡
34 lockal.sin_port = htons(PORT);
35
if
( bind(listenSock, (
struct
sockaddr*)&lockal,
sizeof
(lockal)) < 0)
//绑定本地地址
36 {
37
perror
(
"bind"
);
38
exit
(0);
39 }
40
41
if
( listen(listenSock, 5) < 0)
//进入监听状态
42 {
43
perror
(
"listen"
);
44
exit
(1);
45 }
46 }
47
48
void
LinkSock()
49 {
50
int
size =
sizeof
(lockal);
51 linkSock = accept(listenSock, (
struct
sockaddr*)&client, &size);
//创建一个已链接套接字
52
if
(linkSock < 0)
53 {
54
perror
(
"accept"
);
55 }
56
else
57 {
58
printf
(
"connect success ip is :%s port is : %d\n"
,inet_ntoa(client.sin_addr),ntohs(client.sin_port));
59 }
60 }
61
62
void
* RecvMessage(
void
* ptr)
63 {
64
while
(1)
65 {
66
int
ret = 0;
67 ret = read(linkSock, buf, LEN);
68 buf[ret] =
'\0'
;
69
printf
(
"client->ip:%s port: %d #%s\n"
,inet_ntoa(client.sin_addr),\
70 ntohs(client.sin_port), buf);
71 }
72
return
NULL;
73 }
74
75
int
main()
76 {
77 ListenSock();
78
while
(1)
79 {
80 LinkSock();
81
82 pthread_t tid;
83
int
err = pthread_create(&tid, NULL, RecvMessage, NULL);
84 pthread_detach(tid);
85 }
86
87
return
0;
88 }
89
90
|
client.c:
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
|
1
/****************************************
2 > File Name:client.c
3 > Author:xiaoxiaohui
4 > mail:1924224891@qq.com
5 > Created Time:2016年05月15日 星期日 16时48分21秒
6 ****************************************/
7
8
9 #include<stdio.h>
10 #include<stdlib.h>
11 #include<sys/types.h>
12 #include<sys/socket.h>
13 #include<unistd.h>
14 #include <arpa/inet.h>
15 #include<netinet/in.h>
16 #include<fcntl.h>
17 #include<string.h>
18 #include<fcntl.h>
19
const
int
PORT = 8080;
20
const
int
LEN = 1024;
21
int
clientSock;
22
struct
sockaddr_in server;
23
24
25
26
void
LinkSock()
//创建一个以链接套接字
27 {
28 clientSock = socket(AF_INET, SOCK_STREAM, 0);
29
30 server.sin_family = AF_INET;
31 server.sin_addr.s_addr = inet_addr(
"127.0.0.1"
);
32 server.sin_port = htons(PORT);
33
if
( connect(clientSock, (
struct
sockaddr*)&server,
sizeof
(server)) < 0)
34 {
35
perror
(
"connect"
);
36
exit
(0);
37 }
38
else
39 {
40
printf
(
"connect success! ip:%s port:%d\n"
, inet_ntoa(server.sin_addr), ntohs(server.sin_port));
41 }
42 }
43
44
int
main()
45 {
46
47
char
buf[LEN];
48 LinkSock();
//得到一个已链接套接字
49
50
while
(1)
51 {
52
int
ret = 0;
53
printf
(
"请选择->"
);
54
gets
(buf);
55
56 write(clientSock, buf,
strlen
(buf));
//把选择的命令发到服务器
57
58
//memset(buf, '\0', LEN);
59
//ret = read(clientSock, buf, LEN - 1 ); //接受服务器的信息
60
//buf[ret] = '\0';
61
//printf("%s", buf);
62
//printf("\n");
63 }
64
65
return
0;
66 }
67
|
执行结果:
本文转自 ye小灰灰 51CTO博客,原文链接:http://blog.51cto.com/10704527/1775354,如需转载请自行联系原作者