TCP/IP Programming Manual

while (1) {
/* Accept a connection on this socket. The accept call places the
client's address in the sockaddr_in structure named clientaddr.*/
clientaddrlen = sizeof(clientaddr);
if( accept_nw(s, (struct sockaddr *)&clientaddr, &clientaddrlen, tag) <0) {
perror("accept");
exit(3);
}
if( fe = IOCheck(acceptWait) ) { /* initially, wait -1;
maybe change afterwards? */
if( fe == 40 ) {
printf( "Timed out after %ld secs wtg Client connect.
Terminating.\n",acceptWait/100 );
FILE_CLOSE_((short)s);
exit(0);
} else {
printf( "AWAITIO error %d from accept_nw\n",fe );
exit(3);
}
}
/* Need a new socket for the data transfer
Resembles the earlier call */
if ((new_s = socket_nw(AF_INET, SOCK_STREAM,0,2,0)) < 0) {
perror ("Socket 2 create failed.");
exit (4);
}
/* Make the connection */
if ( accept_nw2(new_s, (struct sockaddr *)&clientaddr, tag2) < 0) {
perror ("2nd Accept failed.");
exit (5);
}
if( fe = IOCheck(-1) ) {
printf( "AWAITIO error %d, tag %ld from 2nd
accept_nw\n",fe,tagBack );
exit(4);
}
/* Receive data from the client.
recv_nw() - awaitio() should be in a loop until a logical record
has been received. In this example, we expect the short messages
to be completed in a single recv_nw() */
if( recv_nw(new_s, databuf, sizeof(databuf), 0, tag2) < 0 ) {
if( errno == ESHUTDOWN || errno == ETIMEDOUT || errno ==
ECONNRESET ) {
FILE_CLOSE_((short)new_s);
continue;
} else {
perror( "recv_nw error" );
exit( 6 );
}
}
if( fe = IOCheck(timeout) ) {
if( fe == 40 ) { /* abandon and start over */
FILE_CLOSE_((short)new_s);
continue;
} else {
printf( "AWAITIO error %d from recv_nw\n",fe );
exit(6);
}
}
databuf[dcount] = '\0'; /* dcount set by IOCheck */
/* Retrieve the client name using the address in the sockaddr_in
structure named clientaddr. A call to gethostbyaddr expects an
IPv4 address as input. */
214 Sample Programs