-
Notifications
You must be signed in to change notification settings - Fork 4
/
single-server.c
83 lines (68 loc) · 1.74 KB
/
single-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
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#define PORT 9990
#define SIZE 1024
int creat_socket()
{
int server_socket = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(PORT);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
int bindResult = bind(server_socket, (struct sockaddr *)&addr, sizeof(addr));
printf("bindResult ==> %d\n", bindResult);
if (bindResult == -1)
{
perror("bindResult");
}
int listenResult = listen(server_socket, 5);
printf("listenResult ==> %d\n", listenResult);
if (bindResult == -1)
{
perror("listenResult");
}
return server_socket;
}
int wait_client(int server_socket)
{
struct sockaddr_in cliaddr;
int addrlen = sizeof(cliaddr);
printf("waiting connect .. \n");
int client_socket = accept(server_socket, (struct sockaddr *)&cliaddr, &addrlen);
printf("accept success %s\n", inet_ntoa(cliaddr.sin_addr));
return client_socket;
}
int main()
{
int server_socket = creat_socket();
int client_socket = wait_client(server_socket);
char buf[SIZE];
while (1)
{
int bufSize = read(client_socket, buf, SIZE - 1);
if (bufSize == -1)
{
break;
}
if (bufSize == 0)
{
break;
}
printf("bufSize: ==> %d\n", bufSize);
buf[bufSize] = '\0';
printf("From client: %s\n", buf);
write(client_socket, buf, bufSize);
if (strncmp(buf, "end", 3) == 0)
{
break;
}
}
close(client_socket);
close(server_socket);
return 0;
}