Fork me on GitHub

Linux Socket编程:基于TCP/IP的客户端与服务端通讯实例

Linux Socket编程:基于TCP/IP的客户端与服务端通讯实例

服务端代码

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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
int main(int argc, char *argv[])
{
int serv_sock, clnt_sock;
struct sockaddr_in serv_addr, clnt_addr;
socklen_t clnt_addr_size;
if (argc != 2) {
printf("Usage: %s <port>\n", argv[0]);
exit(1);
}
serv_sock = socket(AF_INET, SOCK_STREAM, 0);
if (serv_sock < 0) {
printf("sock() error\n");
exit(1);
}
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(atoi(argv[1]));
int optval = 1;
// setting address and port reusable
setsockopt(serv_sock, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
if (bind(serv_sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
printf("bind() error\n");
exit(1);
}
if (listen(serv_sock, 5) < 0) {
printf("listen() error\n");
exit(1);
}
clnt_addr_size = sizeof(clnt_addr);
clnt_sock = accept(serv_sock, (struct sockaddr*)&clnt_addr,
&clnt_addr_size);
if (clnt_sock < 0) {
printf("accept() error\n");
exit(1);
}
char buf[1024];
printf("connected with ip: %s and port: %d\n",
inet_ntop(AF_INET, &clnt_addr.sin_addr, buf, 1024),
ntohs(clnt_addr.sin_port));
while (1) {
memset(buf, 0, sizeof(buf));
ssize_t size = read(clnt_sock, buf, sizeof(buf) - 1);
if (size < 0) {
printf("read() error\n");
break;
} else {
printf("client#: %s\n", buf);
}
}
close(clnt_sock);
close(serv_sock);
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
int main(int argc, char *argv[])
{
int clnt_sock;
struct sockaddr_in serv_addr;
int str_len;
if (argc != 3) {
printf("Usage: %s <IP> <port>\n", argv[0]);
exit(1);
}
clnt_sock = socket(AF_INET, SOCK_STREAM, 0);
if (clnt_sock < 0) {
printf("sock() error\n");
exit(1);
}
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr(argv[1]);
serv_addr.sin_port = htons(atoi(argv[2]));
if (connect(clnt_sock,
(struct sockaddr*)&serv_addr,
sizeof(serv_addr)) == -1) {
printf("connect() error\n");
exit(1);
}
char buf[1024];
while (1) {
memset(buf, 0, sizeof(buf));
printf("client enter: ");
fflush(stdout);
ssize_t size = read(0, buf, sizeof(buf) - 1);
if (size < 0) {
printf("read() error\n");
break;
} else {
buf[size - 1] = '\0';
}
write(clnt_sock, buf, strlen(buf));
}
close(clnt_sock);
return 0;
}

使用方法

编译:

1
2
$ gcc hello_server.c -o hello_server
$ gcc hello_client.c -o hello_client

打开终端:

1
$ ./hello_server 8888

打开另一个终端:

1
$ ./hello_client 127.0.0.1 8888

运行结果

客户端:

1
2
3
4
$ ./hello_client 127.0.0.1 8888
client please enter: 123
client please enter: 456
client please enter:

服务端:

1
2
3
4
$ ./hello_server 8888
connected with ip: 127.0.0.1 and port: xxxx
client#: 123
client#: 456