test07_srv.c

Go to the documentation of this file.
00001 /*
00002  * Test name: test07_srv.c
00003  *
00004  * Objective: Test an impatient TCP server with a timeout.
00005  *
00006  * Description: Implements an echo server using the TCP protocol. It is
00007  * based on test06_srv.c but has a timeout for receiving data from a client
00008  * and if the time is up it sends an angry message to the client requesting
00009  * for more information.
00010  */
00011 
00012 #include <sys/types.h>
00013 #include <sys/ioctl.h>
00014 #include <sys/select.h>
00015 #include <sys/wait.h>
00016 #include <sys/asynchio.h>
00017 #include <stdio.h>
00018 #include <string.h>
00019 #include <stdlib.h>
00020 #include <stdarg.h>
00021 #include <fcntl.h>
00022 #include <errno.h>
00023 #include <unistd.h>
00024 #include <net/netlib.h>
00025 #include <net/gen/netdb.h>
00026 #include <net/gen/in.h>
00027 #include <net/gen/tcp.h>
00028 #include <net/gen/tcp_io.h>
00029 #include <net/hton.h>
00030 #include <net/gen/inet.h>
00031 
00032 #define PORT 6060L
00033 
00034 int listen(long port) {
00035 
00036   char *tcp_device;
00037   int netfd;
00038   nwio_tcpconf_t tcpconf;
00039   nwio_tcpcl_t tcplistenopt;
00040                             
00041   /* Get default UDP device */
00042   if ((tcp_device = getenv("TCP_DEVICE")) == NULL) 
00043     tcp_device = TCP_DEVICE;
00044                                 
00045   /* Open TCP connection */ 
00046   if ((netfd = open(tcp_device, O_RDWR)) < 0) 
00047   {
00048     fprintf(stderr,"Error opening TCP connection\n");
00049     return -1;
00050   }               
00051                                 
00052   /* Configure TCP connection */ 
00053   tcpconf.nwtc_flags = NWTC_LP_SET | NWTC_UNSET_RA | NWTC_UNSET_RP;
00054   tcpconf.nwtc_locport = (tcpport_t) htons(port);
00055                                 
00056   if ((ioctl(netfd, NWIOSTCPCONF, &tcpconf))<0) 
00057   {
00058     fprintf(stderr,"Error configuring the connection\n");
00059     close(netfd);
00060     return -1;
00061   }               
00062 
00063   /* Get communication options */
00064   if ((ioctl(netfd, NWIOGTCPCONF, &tcpconf)) < 0) {
00065     fprintf(stderr, "Error getting configuration\n");
00066     close(netfd);
00067     return -1;
00068    }                
00069                                 
00070   /* Set conf options */
00071   tcplistenopt.nwtcl_flags = 0;
00072   printf("Waiting for connections...\n");
00073   while ((ioctl(netfd, NWIOTCPLISTEN, &tcplistenopt)) == -1) 
00074   {
00075     if (errno != EAGAIN)
00076     {
00077         fprintf(stderr,"Unable to listen for connections\n");
00078         close(netfd);
00079     }
00080     sleep(-1);
00081   }               
00082   return netfd;
00083 }
00084 
00085 int main(int argc,char *argv[]) {
00086   int fd;
00087   ssize_t data_read;
00088   char buffer[1024];
00089   int ret;
00090   fd_set fds_read;
00091   struct timeval timeout;
00092 
00093   if ((fd = listen(PORT)) < 0) {
00094         exit(-1);       
00095   }
00096   printf("Waiting for messages on port: %ld\n", PORT);
00097   fflush(stdout);
00098   while (1) 
00099   {
00100 
00101     /* Initialize fd_set */
00102     FD_ZERO(&fds_read);
00103     FD_SET(fd, &fds_read);
00104     /* set timeout */
00105     timeout.tv_sec = 3;
00106     timeout.tv_usec = 0;
00107     /* Wait for data available to be read (no timeout) */
00108     ret = select(4, &fds_read, NULL, NULL, &timeout);
00109     if (ret < 0) {
00110         fprintf(stderr, "Error on select: %d\n", errno);
00111         exit(-1);
00112     }
00113     /* timeout */
00114     if (ret == 0) {
00115         strcpy(buffer, "I want to get some data!!\n");
00116         write(fd, &buffer, 1024);
00117         continue;
00118     }
00119 
00120     /* data received from client */
00121     if (FD_ISSET(fd, &fds_read)) {
00122         printf("Ready to receive...\n");
00123         /* Read received data */
00124         data_read = read(fd, &buffer, 1024);
00125         printf("Received data: %s\n", buffer);
00126                                 
00127         /* Can exit if the received string == exit */
00128         if (!strcmp(buffer,"exit")) 
00129            break; 
00130 
00131         /* Write the same back  */
00132         write(fd, &buffer, data_read);             
00133      }               
00134   }                       
00135   printf("Connection finished\n");
00136   close(fd);
00137 }

Generated on Fri Apr 14 22:57:32 2006 for minix by  doxygen 1.4.6