NP 3
NP 3
org
Network Programming – MC9241
1. The Client reads a line of text from its standard input and writes the line to the server.
2. The server reads the line from its network input and echoes the line back to the client.
3. The client reads the echoed line and prints it on its standard output.
voidstr_echo(intsockfd)
{
ssize_t n;
1 CCET
www.anuupdates.org
Network Programming – MC9241
char line[MAXLINE];
for ( ; ; ) {
if ( (n = Readline(sockfd, line, MAXLINE)) == 0)
return; /* connection closed by other end */
Writen(sockfd, line, n);
}
}
}
voidstr_cli(FILE *fp, intsockfd)
{
char sendline[MAXLINE], recvline[MAXLINE];
while (Fgets(sendline, MAXLINE, fp) != NULL) {
Writen(sockfd, sendline, strlen(sendline));
if (Readline(sockfd, recvline, MAXLINE) == 0)
err_quit("str_cli: server terminated
prematurely");
Fputs(recvline, stdout);
}
}
2 CCET
www.anuupdates.org
Network Programming – MC9241
Boundary Conditions
3 CCET
www.anuupdates.org
Network Programming – MC9241
I/O Multiplexing
I/O Models
Models
Blocking I/O
4 CCET
www.anuupdates.org
Network Programming – MC9241
Nonblocking I/O
I/O multiplexing(select and poll)
Signal driven I/O (SIGIO)
Asynchronous I/O
2) Nonblocking I/O
5 CCET
www.anuupdates.org
Network Programming – MC9241
5) Asynchronous I/O
6 CCET
www.anuupdates.org
Network Programming – MC9241
• Synchronous I/O
• Asynchronous I/O
Select Function
• Allows the process to instruct the kernel to wait for any one of multiple events to
occur and to wake up the process only when one or more of these events occurs or
when a specified amount of time has passed.
#include <sys/select.h>
#include <sys/time.h>
7 CCET
www.anuupdates.org
Network Programming – MC9241
int select (int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset, const
struct timeval *);
//Returns: +ve count of ready descriptors, 0 on timeout, -1 on error
struct timeval{
long tv_sec; /* seconds */
long tv_usec; /* microseconds */ }
• Wait forever : return only when descriptor (s) is ready (specify timeout argument
as NULL)
• wait up to a fixed amount of time
• Do not wait at all : return immediately after checking the descriptors. Polling
(specify timeout argument as pointing to a timeval structure where the timer value
is 0)
• The wait is normally interrupted if the process catches a signal and returns from the
signal handler
select might return an error of EINTR
Actual return value from function = -1
select function Descriptor Arguments
• readset descriptors for checking readable
• writeset descriptors for checking writable
• exceptset descriptors for checking exception conditions (2 exception conditions)
arrival of out of band data for a socket
the presence of control status information to be read from the master
side of a pseudo terminal (Ignore)
• If you pass the 3 arguments as NULL, you have a high precision timer than the sleep
function.
maxfdp1 argument to select function
8 CCET
www.anuupdates.org
Network Programming – MC9241
Shutdown Function
9 CCET
www.anuupdates.org
Network Programming – MC9241
•
#include<sys/socket.h>
int shutdown ( int sockfd, int howto );
/* return : 0 if OK, -1 on error */
howto argument
SHUT_RD
read-half of the connection closed
Any data in receive buffer is discarded
Any data received after this call is ACKed and then discarded
SHUT_WR
write-half of the connection closed (half-close)
Data in socket send buffer sent, followed by connection termination
SHUT_RDWR - both closed
Poll Function
poll
Provides functionality similar to select, but poll provides additional information
when dealing with stream devices.
#include <poll.h>
int poll(struct pollfd fds[], nfds_t nfds, int timeout);
Description
The poll() function provides applications with a mechanism for multiplexing input/output
over a set of file descriptors. For each member of the array pointed to by fds, poll() shall
10 CCET
www.anuupdates.org
Network Programming – MC9241
examine the given file descriptor for the event(s) specified in events. The number of pollfd
structures in the fds array is specified by nfds. The poll() function shall identify those file
descriptors on which an application can read or write data, or on which certain events have
occurred.
The fds argument specifies the file descriptors to be examined and the events of interest for
each file descriptor. It is a pointer to an array with one member for each open file
descriptor of interest. The array's members are pollfd structures within which fd specifies
an open file descriptor and events and revents are bitmasks constructed by OR'ing a
combination of the following event flags:
POLLIN
Data other than high-priority data may be read without blocking. For STREAMS, this
flag is set in revents even if the message is of zero length. This flag shall be
equivalent to POLLRDNORM | POLLRDBAND.
POLLRDNORM
Normal data may be read without blocking.For STREAMS, data on priority band 0
may be read without blocking. This flag is set in revents even if the message is of
zero length.
POLLRDBAND
Priority data may be read without blocking.For STREAMS, data on priority bands
greater than 0 may be read without blocking. This flag is set in revents even if the
message is of zero length.
POLLPRI
High-priority data may be read without blocking.For STREAMS, this flag is set in
revents even if the message is of zero length.
POLLOUT
Normal data may be written without blocking.For STREAMS, data on priority band 0
may be written without blocking.
POLLWRNORM
Equivalent to POLLOUT.
POLLWRBAND
Priority data may be written.For STREAMS, data on priority bands greater than 0
may be written without blocking. If any priority band has been written to on this
STREAM, this event only examines bands that have been written to at least once.
POLLERR
An error has occurred on the device or stream. This flag is only valid in the revents
bitmask; it shall be ignored in the events member.
11 CCET
www.anuupdates.org
Network Programming – MC9241
POLLHUP
The device has been disconnected. This event and POLLOUT are mutually-exclusive;
a stream can never be writable if a hangup has occurred. However, this event and
POLLIN, POLLRDNORM, POLLRDBAND, or POLLPRI are not mutually-exclusive. This
flag is only valid in the revents bitmask; it shall be ignored in the events member.
POLLNVAL
The specified fd value is invalid. This flag is only valid in the revents member; it shall
ignored in the events member.
• Rewrite the server as a single process that uses select to handle any number of clients,
instead of forking one child per client.
• Before first client has established a connection
12 CCET