-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdaytime-server.cc
179 lines (145 loc) · 4.94 KB
/
daytime-server.cc
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
const char * usage =
" \n"
"daytime-server: \n"
" \n"
"Simple server program that shows how to use socket calls \n"
"in the server side. \n"
" \n"
"To use it in one window type: \n"
" \n"
" daytime-server <port> \n"
" \n"
"Where 1024 < port < 65536. \n"
" \n"
"In another window type: \n"
" \n"
" telnet <host> <port> \n"
" \n"
"where <host> is the name of the machine where daytime-server \n"
"is running. <port> is the port number you used when you run \n"
"daytime-server. \n"
" \n"
"Then type your name and return. You will get a greeting and \n"
"the time of the day. \n"
" \n";
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <time.h>
int QueueLength = 5;
// Processes time request
void processTimeRequest( int socket );
int
main( int argc, char ** argv )
{
// Print usage if not enough arguments
if ( argc < 2 ) {
fprintf( stderr, "%s", usage );
exit( -1 );
}
// Get the port from the arguments
int port = atoi( argv[1] );
// Set the IP address and port for this server
struct sockaddr_in serverIPAddress;
memset( &serverIPAddress, 0, sizeof(serverIPAddress) );
serverIPAddress.sin_family = AF_INET;
serverIPAddress.sin_addr.s_addr = INADDR_ANY;
serverIPAddress.sin_port = htons((u_short) port);
// Allocate a socket
int masterSocket = socket(PF_INET, SOCK_STREAM, 0);
if ( masterSocket < 0) {
perror("socket");
exit( -1 );
}
// Set socket options to reuse port. Otherwise we will
// have to wait about 2 minutes before reusing the sae port number
int optval = 1;
int err = setsockopt(masterSocket, SOL_SOCKET, SO_REUSEADDR,
(char *) &optval, sizeof( int ) );
// Bind the socket to the IP address and port
int error = bind( masterSocket,
(struct sockaddr *)&serverIPAddress,
sizeof(serverIPAddress) );
if ( error ) {
perror("bind");
exit( -1 );
}
// Put socket in listening mode and set the
// size of the queue of unprocessed connections
error = listen( masterSocket, QueueLength);
if ( error ) {
perror("listen");
exit( -1 );
}
while ( 1 ) {
// Accept incoming connections
struct sockaddr_in clientIPAddress;
int alen = sizeof( clientIPAddress );
int slaveSocket = accept( masterSocket,
(struct sockaddr *)&clientIPAddress,
(socklen_t*)&alen);
if ( slaveSocket < 0 ) {
perror( "accept" );
exit( -1 );
}
// Process request.
processTimeRequest( slaveSocket );
// Close socket
close( slaveSocket );
}
}
void
processTimeRequest( int fd )
{
// Buffer used to store the name received from the client
const int MaxName = 1024;
char name[ MaxName + 1 ];
int nameLength = 0;
int n;
// Send prompt
const char * prompt = "\nType your name:";
write( fd, prompt, strlen( prompt ) );
// Currently character read
unsigned char newChar;
// Last character read
unsigned char lastChar = 0;
//
// The client should send <name><cr><lf>
// Read the name of the client character by character until a
// <CR><LF> is found.
//
while ( nameLength < MaxName &&
( n = read( fd, &newChar, sizeof(newChar) ) ) > 0 ) {
if ( lastChar == '\015' && newChar == '\012' ) {
// Discard previous <CR> from name
nameLength--;
break;
}
name[ nameLength ] = newChar;
nameLength++;
lastChar = newChar;
}
// Add null character at the end of the string
name[ nameLength ] = 0;
printf( "name=%s\n", name );
// Get time of day
time_t now;
time(&now);
char *timeString = ctime(&now);
// Send name and greetings
const char * hi = "\nHi ";
const char * timeIs = " the time is:\n";
write( fd, hi, strlen( hi ) );
write( fd, name, strlen( name ) );
write( fd, timeIs, strlen( timeIs ) );
// Send the time of day
write(fd, timeString, strlen(timeString));
// Send last newline
const char * newline="\n";
write(fd, newline, strlen(newline));
}