1 | /* |
---|
2 | * Test RTEMS networking |
---|
3 | * |
---|
4 | * This program may be distributed and used for any purpose. |
---|
5 | * I ask only that you: |
---|
6 | * 1. Leave this author information intact. |
---|
7 | * 2. Document any changes you make. |
---|
8 | * |
---|
9 | * W. Eric Norum |
---|
10 | * Saskatchewan Accelerator Laboratory |
---|
11 | * University of Saskatchewan |
---|
12 | * Saskatoon, Saskatchewan, CANADA |
---|
13 | * eric@skatter.usask.ca |
---|
14 | * |
---|
15 | * $Id$ |
---|
16 | */ |
---|
17 | |
---|
18 | #include <stdio.h> |
---|
19 | #include <string.h> |
---|
20 | #include <errno.h> |
---|
21 | #include <unistd.h> |
---|
22 | #include <rtems.h> |
---|
23 | #include <rtems/rtems_bsdnet.h> |
---|
24 | #include <rtems/error.h> |
---|
25 | #include <sys/types.h> |
---|
26 | #include <sys/socket.h> |
---|
27 | #include <sys/select.h> |
---|
28 | #include <netinet/in.h> |
---|
29 | |
---|
30 | #define BASE_PORT 24742 |
---|
31 | #define CLIENT_COUNT 2 |
---|
32 | |
---|
33 | static int clientfd[CLIENT_COUNT]; |
---|
34 | |
---|
35 | static void |
---|
36 | getClients (unsigned short port) |
---|
37 | { |
---|
38 | int s, s1; |
---|
39 | struct sockaddr_in myAddr, farAddr; |
---|
40 | socklen_t addrlen; |
---|
41 | int clientCount; |
---|
42 | |
---|
43 | printf ("Create socket.\n"); |
---|
44 | s = socket (AF_INET, SOCK_STREAM, 0); |
---|
45 | if (s < 0) |
---|
46 | rtems_panic ("Can't create socket: %s", strerror (errno)); |
---|
47 | myAddr.sin_family = AF_INET; |
---|
48 | myAddr.sin_port = htons (port); |
---|
49 | myAddr.sin_addr.s_addr = INADDR_ANY; |
---|
50 | memset (myAddr.sin_zero, '\0', sizeof myAddr.sin_zero); |
---|
51 | printf ("Bind socket.\n"); |
---|
52 | if (bind (s, (struct sockaddr *)&myAddr, sizeof myAddr) < 0) |
---|
53 | rtems_panic ("Can't bind socket: %s", strerror (errno)); |
---|
54 | printf ("Listen.\n"); |
---|
55 | if (listen (s, 2) < 0) |
---|
56 | rtems_panic ("Can't listen on socket: %s", strerror (errno)); |
---|
57 | |
---|
58 | /* |
---|
59 | * Accumulate clients |
---|
60 | */ |
---|
61 | for (clientCount = 0 ; clientCount < CLIENT_COUNT ; clientCount++) { |
---|
62 | printf ("Accept.\n"); |
---|
63 | addrlen = sizeof farAddr; |
---|
64 | s1 = accept (s, (struct sockaddr *)&farAddr, &addrlen); |
---|
65 | if (s1 < 0) |
---|
66 | rtems_panic ("Can't accept connection: %s", strerror (errno)); |
---|
67 | else |
---|
68 | printf ("ACCEPTED:%lX\n", ntohl (farAddr.sin_addr.s_addr)); |
---|
69 | clientfd[clientCount] = s1; |
---|
70 | } |
---|
71 | |
---|
72 | close (s); |
---|
73 | } |
---|
74 | |
---|
75 | static void |
---|
76 | echoServer (unsigned short port) |
---|
77 | { |
---|
78 | fd_set clientfdset; |
---|
79 | int clientCount; |
---|
80 | int topfd = 0; |
---|
81 | |
---|
82 | getClients (port); |
---|
83 | |
---|
84 | FD_ZERO (&clientfdset); |
---|
85 | |
---|
86 | for (clientCount = 0 ; clientCount < CLIENT_COUNT ; clientCount++) { |
---|
87 | int s1; |
---|
88 | |
---|
89 | s1 = clientfd[clientCount]; |
---|
90 | FD_SET (s1, &clientfdset); |
---|
91 | if (s1 > topfd) |
---|
92 | topfd = s1; |
---|
93 | } |
---|
94 | |
---|
95 | /* |
---|
96 | * Run clients |
---|
97 | */ |
---|
98 | for (;;) { |
---|
99 | fd_set readfdset; |
---|
100 | struct timeval tv; |
---|
101 | int n; |
---|
102 | int i; |
---|
103 | |
---|
104 | tv.tv_sec = 5; |
---|
105 | tv.tv_usec = 0; |
---|
106 | readfdset = clientfdset; |
---|
107 | n = select (topfd + 1, &readfdset, NULL, NULL, &tv); |
---|
108 | if (n < 0) { |
---|
109 | printf ("Select() error: %s\n", strerror (errno)); |
---|
110 | return; |
---|
111 | } |
---|
112 | if (n == 0) { |
---|
113 | printf ("Timeout\n"); |
---|
114 | continue; |
---|
115 | } |
---|
116 | |
---|
117 | printf ("Activity on %d file descriptor%s.\n", n, n == 1 ? "" : "s"); |
---|
118 | for (i = 0 ; n && (i < CLIENT_COUNT) ; i++) { |
---|
119 | int fd = clientfd[i]; |
---|
120 | if (FD_ISSET (fd, &readfdset)) { |
---|
121 | char buf[200]; |
---|
122 | int nread; |
---|
123 | |
---|
124 | printf ("Activity on file descriptor %d.\n", fd); |
---|
125 | n--; |
---|
126 | nread = read (fd, buf, sizeof buf); |
---|
127 | if (nread < 0) { |
---|
128 | printf ("Read error %s.\n", strerror (errno)); |
---|
129 | return; |
---|
130 | } |
---|
131 | if (nread == 0) { |
---|
132 | printf ("EOF\n"); |
---|
133 | FD_CLR (fd, &clientfdset); |
---|
134 | close (fd); |
---|
135 | if (--clientCount == 0) |
---|
136 | return; |
---|
137 | } |
---|
138 | printf ("Read %d.\n", nread); |
---|
139 | } |
---|
140 | } |
---|
141 | } |
---|
142 | } |
---|
143 | |
---|
144 | void |
---|
145 | doSocket (void) |
---|
146 | { |
---|
147 | echoServer (BASE_PORT); |
---|
148 | } |
---|