2 * Test name: test08_srv.c
4 * Objective: Test a simple TCP server waiting for urgent data.
6 * Description: Implements a echo TCP server as in test06_srv but waits
7 * for urgent data, using select on exception.
11 #include <sys/types.h>
12 #include <sys/ioctl.h>
13 #include <sys/select.h>
22 #include <net/netlib.h>
23 #include <net/gen/netdb.h>
24 #include <net/gen/in.h>
25 #include <net/gen/tcp.h>
26 #include <net/gen/tcp_io.h>
28 #include <net/gen/inet.h>
32 int my_listen(long port
) {
36 nwio_tcpconf_t tcpconf
;
40 /* Get default UDP device */
41 if ((tcp_device
= getenv("TCP_DEVICE")) == NULL
)
42 tcp_device
= TCP_DEVICE
;
44 /* Open TCP connection */
45 if ((netfd
= open(tcp_device
, O_RDWR
)) < 0)
47 fprintf(stderr
,"Error opening TCP connection\n");
51 /* Configure TCP connection */
52 tcpconf
.nwtc_flags
= NWTC_LP_SET
| NWTC_UNSET_RA
| NWTC_UNSET_RP
;
53 tcpconf
.nwtc_locport
= (tcpport_t
) htons(port
);
55 if ((ioctl(netfd
, NWIOSTCPCONF
, &tcpconf
))<0)
57 fprintf(stderr
,"Error configuring the connection\n");
62 /* Get communication conf*/
63 if ((ioctl(netfd
, NWIOGTCPCONF
, &tcpconf
)) < 0) {
64 fprintf(stderr
, "Error getting configuration\n");
69 /* Set comm options */
70 tcpopt
.nwto_flags
= NWTO_RCV_URG
;
72 if ((ioctl(netfd
, NWIOSTCPOPT
, &tcpopt
))<0)
74 fprintf(stderr
,"Error configuring the connection\n");
79 /* Get communication opt*/
80 if ((ioctl(netfd
, NWIOGTCPOPT
, &tcpopt
)) < 0) {
81 fprintf(stderr
, "Error getting options\n");
86 /* Set conn options */
87 tcpcl
.nwtcl_flags
= 0;
88 printf("Waiting for connections...\n");
89 while ((ioctl(netfd
, NWIOTCPLISTEN
, &tcpcl
)) == -1)
93 fprintf(stderr
,"Unable to listen for connections\n");
101 int main(int argc
,char *argv
[]) {
108 if ((fd
= my_listen(PORT
)) < 0) {
111 printf("Waiting for messages on port: %ld\n", PORT
);
113 /* Initialize fd_set */
115 FD_SET(fd
, &fds_excep
);
119 /* Wait for data available to be read (no timeout) */
121 ret
= select(4, NULL
, NULL
, &fds_excep
, NULL
);
123 fprintf(stderr
, "Error on select: %d\n", errno
);
126 if (!FD_ISSET(fd
, &fds_excep
)) {
127 printf("Error: no URG data received (?)\n");
131 printf("Ready to receive...\n");
132 /* Read received data */
133 data_read
= read(fd
, &buffer
, 1024);
134 printf("Received data: %s\n", buffer
);
136 /* Can exit if the received string == exit */
137 if (!strcmp(buffer
,"exit"))
140 /* Write the same back */
141 write(fd
, &buffer
, data_read
);
143 printf("Connection finished\n");