source: trunk/minix/test/select/test07_srv.c@ 10

Last change on this file since 10 was 9, checked in by Mattia Monga, 13 years ago

Minix 3.1.2a

  • Property svn:executable set to *
File size: 3.4 KB
Line 
1/*
2 * Test name: test07_srv.c
3 *
4 * Objective: Test an impatient TCP server with a timeout.
5 *
6 * Description: Implements an echo server using the TCP protocol. It is
7 * based on test06_srv.c but has a timeout for receiving data from a client
8 * and if the time is up it sends an angry message to the client requesting
9 * for more information.
10 */
11
12#include <sys/types.h>
13#include <sys/ioctl.h>
14#include <sys/select.h>
15#include <sys/wait.h>
16#include <sys/asynchio.h>
17#include <stdio.h>
18#include <string.h>
19#include <stdlib.h>
20#include <stdarg.h>
21#include <fcntl.h>
22#include <errno.h>
23#include <unistd.h>
24#include <net/netlib.h>
25#include <net/gen/netdb.h>
26#include <net/gen/in.h>
27#include <net/gen/tcp.h>
28#include <net/gen/tcp_io.h>
29#include <net/hton.h>
30#include <net/gen/inet.h>
31
32#define PORT 6060L
33
34int listen(long port) {
35
36 char *tcp_device;
37 int netfd;
38 nwio_tcpconf_t tcpconf;
39 nwio_tcpcl_t tcplistenopt;
40
41 /* Get default UDP device */
42 if ((tcp_device = getenv("TCP_DEVICE")) == NULL)
43 tcp_device = TCP_DEVICE;
44
45 /* Open TCP connection */
46 if ((netfd = open(tcp_device, O_RDWR)) < 0)
47 {
48 fprintf(stderr,"Error opening TCP connection\n");
49 return -1;
50 }
51
52 /* Configure TCP connection */
53 tcpconf.nwtc_flags = NWTC_LP_SET | NWTC_UNSET_RA | NWTC_UNSET_RP;
54 tcpconf.nwtc_locport = (tcpport_t) htons(port);
55
56 if ((ioctl(netfd, NWIOSTCPCONF, &tcpconf))<0)
57 {
58 fprintf(stderr,"Error configuring the connection\n");
59 close(netfd);
60 return -1;
61 }
62
63 /* Get communication options */
64 if ((ioctl(netfd, NWIOGTCPCONF, &tcpconf)) < 0) {
65 fprintf(stderr, "Error getting configuration\n");
66 close(netfd);
67 return -1;
68 }
69
70 /* Set conf options */
71 tcplistenopt.nwtcl_flags = 0;
72 printf("Waiting for connections...\n");
73 while ((ioctl(netfd, NWIOTCPLISTEN, &tcplistenopt)) == -1)
74 {
75 if (errno != EAGAIN)
76 {
77 fprintf(stderr,"Unable to listen for connections\n");
78 close(netfd);
79 }
80 sleep(-1);
81 }
82 return netfd;
83}
84
85int main(int argc,char *argv[]) {
86 int fd;
87 ssize_t data_read;
88 char buffer[1024];
89 int ret;
90 fd_set fds_read;
91 struct timeval timeout;
92
93 if ((fd = listen(PORT)) < 0) {
94 exit(-1);
95 }
96 printf("Waiting for messages on port: %ld\n", PORT);
97 fflush(stdout);
98 while (1)
99 {
100
101 /* Initialize fd_set */
102 FD_ZERO(&fds_read);
103 FD_SET(fd, &fds_read);
104 /* set timeout */
105 timeout.tv_sec = 3;
106 timeout.tv_usec = 0;
107 /* Wait for data available to be read (no timeout) */
108 ret = select(4, &fds_read, NULL, NULL, &timeout);
109 if (ret < 0) {
110 fprintf(stderr, "Error on select: %d\n", errno);
111 exit(-1);
112 }
113 /* timeout */
114 if (ret == 0) {
115 strcpy(buffer, "I want to get some data!!\n");
116 write(fd, &buffer, 1024);
117 continue;
118 }
119
120 /* data received from client */
121 if (FD_ISSET(fd, &fds_read)) {
122 printf("Ready to receive...\n");
123 /* Read received data */
124 data_read = read(fd, &buffer, 1024);
125 printf("Received data: %s\n", buffer);
126
127 /* Can exit if the received string == exit */
128 if (!strcmp(buffer,"exit"))
129 break;
130
131 /* Write the same back */
132 write(fd, &buffer, data_read);
133 }
134 }
135 printf("Connection finished\n");
136 close(fd);
137}
Note: See TracBrowser for help on using the repository browser.