accept?
daytime server
so that the server sleeps for a sufficiently long period of time
before calling accept, and then observe the result.
Section 15.6 might also be helpful to understand this problem.
connect
operation. Explain briefly why such a non-blocking connection operation
is necessary.
TIME_WAIT state, the port
number can not be reused except that the SO_REUSEADDR
option is set. Answer the following questions:
2*MSL to restart the server, while
you can restart the client immediately.
IP1
and IP2, whether can the server program
bind one linstening socket to the address IP1:port
bind another linstening socket to address IP2:port.
man 2 bind
and man 7 socket.
listensock = socket (...);
bind (listensock, ...);
listen (listensock);
flags = fcntl (listensock, F_GETFL, 0);
fcntl (listensock, F_SETFL, flags | O_NONBLOCK);
for (i = 0; i < MAXCLIENTS; i++)
clients[i] = -1;
while (1) {
datasock = accept (listensock, ...);
if (datasock != -1) {
for (i = 0; i < MAXCLIENTS; i++)
if (clients[i] == -1) {
clients[i] = datasock;
break;
}
if (i == MAXCLIENTS)
close (datasock); // too many clients
}
else if (errno != E_WOULDBLOCK && errno != EINTR)
// process some real error, may terminate the server
}
for (i = 0; i < MAXCLIENTS; i++)
if (clients[i] != -1) {
n = read (clients[i], buf, ...);
if (n > 0) {
// process the request in buf, and send back the response
}
else if (n == 0) {
close (clients[i]);
clients[i] = -1;
}
else if (errno != E_WOULDBLOCK && errno != EINTR)
// process some real error, may terminate the server
}
}
}
Read the code and answer the following questions:
man 7 socket.
if statement for
checking the value of errno.
select to implement the same
server behavior, which implement is more efficient?
realsize and
size may not be equal after read/write
and read returns.
You can assume the socket is a blocking socket.
// size is the size of the expected request
realsize = read (datasock, request, size);
// size is the size of the response
realsize = write (datasock, response, size);
handin program is 4.
To submit your assignment with the handin program, type:
~cs742/bin/handin 4 ans.txt