-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.c
155 lines (102 loc) · 3.2 KB
/
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/* server.c - Example of an Internet TPC server */
/* Compile with gcc -D_GNU_SOURCE server.c -o server */
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include "utils.h"
#define PORT 5555 /* The well-nown port. */
#define BUFFER_SIZE 512
int
main (int argc, char **argv)
{
int socketfd; /* Socket file descriptor */
struct sockaddr_in server_name; /* server socket's address */
struct sockaddr_in client_name; /* client socket's address */
fd_set active_fd_set, read_fd_set; /* Array of file descriptors */
int nbytes;
char buffer[BUFFER_SIZE];
/* Create the socket.
socket (NAMESPACE, STYLE, PROTOCOL)
- namespace: local, other, internet: PF_INET || PF_INET6
- style: SOCK_STREAM, SOCK_DGRAM
- protocol: 0 means 'auto' */
socketfd = socket (PF_INET, SOCK_STREAM, 0);
if (socketfd < 0)
fatal ();
/* Give the socket a name. */
server_name.sin_family = AF_INET; /* Ineternet family */
server_name.sin_port = htons (PORT); /* Well-known port */
server_name.sin_addr.s_addr = htonl (INADDR_ANY); /* Any internetface */
/* Bind the socket to the port. */
if (bind (socketfd, (struct sockaddr *)
&server_name, sizeof (server_name)) < 0)
fatal ();
/* Tell the OS to start to listen to the socket. */
if (listen (socketfd, 1) < 0)
fatal ();
/* Initialize the set of active sockets. */
FD_ZERO (&active_fd_set);
FD_SET (socketfd, &active_fd_set);
for (;;)
{
int i;
/* Block until there is activity in the socket. */
read_fd_set = active_fd_set;
if (select (FD_SETSIZE, &read_fd_set, NULL, NULL, NULL) < 0)
fatal ();
/* Service all the sockets with input pending. */
for (i = 0; i < FD_SETSIZE; ++i)
{
if (FD_ISSET (i, &read_fd_set))
{
if (i == socketfd)
{
/* Connection request on original socket. */
int new;
size_t size;
/* Accept the the connection and associate it
with a new socket. */
size = sizeof (client_name);
new = accept (socketfd,
(struct sockaddr *) &client_name,
&size);
if (new < 0)
fatal ();
fprintf (stderr,
"Server: connect from host %s, port %hd.\n",
inet_ntoa (client_name.sin_addr),
ntohs (client_name.sin_port));
/* Add the new socket to the set of active fd */
FD_SET (new, &active_fd_set);
}
else
{
/* Data arriving on an already-connected socket. */
nbytes = read (i, buffer, BUFFER_SIZE);
buffer[nbytes-1] = 0;
if (nbytes ==0) /* Cient ended the transmission */
{
printf ("End of transmission\n");
close (i);
FD_CLR (i, &active_fd_set);
}
else if (nbytes < 0) /* Read error */
{
fprintf (stderr, "Read error.\n");
close (i);
FD_CLR (i, &active_fd_set);
}
else
printf ("Client on socket %d said %s\n", i, buffer);
}
}
}
}
}