-
Notifications
You must be signed in to change notification settings - Fork 4
/
select-server.c
112 lines (100 loc) · 2.81 KB
/
select-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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <sys/select.h>
#include <sys/time.h>
#define PORT 9990
#define SIZE 1024
#define MAX_CLIENTS 10
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));
if (bindResult == -1)
{
perror("bindResult");
}
int listenResult = listen(server_socket, 5);
if (listenResult == -1)
{
perror("listenResult");
}
printf("server start\n");
return server_socket;
}
int wait_client(int server_socket)
{
fd_set *readfds;
int client_sockets[MAX_CLIENTS];
for (int i = 0; i < MAX_CLIENTS; i++)
{
client_sockets[i] = 0;
}
int max_fd = server_socket;
while (1)
{
printf("Loop ~~~\n");
FD_ZERO(readfds);
FD_SET(server_socket, readfds);
for (int i = 0; i < MAX_CLIENTS; i++)
{
if (client_sockets[i] > 0)
{
FD_SET(client_sockets[i], readfds);
max_fd = max_fd + client_sockets[i];
}
}
int selectResult = select(max_fd + 1, readfds, NULL, NULL, NULL);
struct sockaddr_in cliaddr;
int addrlen = sizeof(cliaddr);
if (FD_ISSET(server_socket, readfds))
{
for (int i = 0; i < MAX_CLIENTS; i++)
{
if (client_sockets[i] == 0)
{
client_sockets[i] = accept(server_socket, (struct sockaddr *)&cliaddr, &addrlen);
printf("socketId==>%d client_sockets [%d] accept success, IP => %s\n", client_sockets[i], i, inet_ntoa(cliaddr.sin_addr));
break;
}
}
}
for (int i = 0; i < MAX_CLIENTS; i++)
{
if (FD_ISSET(client_sockets[i], readfds))
{
char buf[SIZE];
int bufSize = read(client_sockets[i], buf, SIZE - 1);
if (bufSize == -1)
{
client_sockets[i] = 0;
close(client_sockets[i]);
}
if (bufSize == 0)
{
client_sockets[i] = 0;
close(client_sockets[i]);
}
buf[bufSize] = '\0';
printf("From client: %s\n", buf);
}
}
}
}
int main()
{
int server_socket = creat_socket();
int client_socket = wait_client(server_socket);
printf("end\n");
close(client_socket);
close(server_socket);
return 0;
}