source: network-demos/select/test.c @ 557c93a

4.11network-demos-4-10-branchnetwork-demos-4-8-branchnetwork-demos-4-9-branch
Last change on this file since 557c93a was 557c93a, checked in by Joel Sherrill <joel.sherrill@…>, on 06/21/07 at 18:26:28

2007-06-21 Joel Sherrill <joel.sherrill@…>

  • init.c, test.c: More warnings removed.
  • Property mode set to 100644
File size: 5.1 KB
Line 
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
33static int clientfd[CLIENT_COUNT];
34
35static void
36getClients (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
75static void
76echoServer (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
144static rtems_id tid;
145
146static void
147wakeup (struct socket *so, caddr_t arg)
148{
149        rtems_event_send (tid, RTEMS_EVENT_0 + (int) arg);
150}
151
152#if 0
153static void
154echoServer2 (port)
155{
156        struct sockwakeup sw, sw2;
157        socklen_t swlen;
158        int clientCount;
159        rtems_event_set clientEvents;
160
161        getClients (port);
162
163        sw.sw_pfn = &wakeup;
164        clientEvents = 0;
165        for (clientCount = 0 ; clientCount < CLIENT_COUNT ; clientCount++) {
166                sw.sw_arg = (caddr_t) clientCount;
167                if (setsockopt (clientfd[clientCount], SOL_SOCKET,
168                                SO_RCVWAKEUP, &sw, sizeof sw) < 0)
169                        rtems_panic ("setsockopt failed: %s",
170                                     strerror (errno));
171                swlen = sizeof sw2;
172                if (getsockopt (clientfd[clientCount], SOL_SOCKET,
173                                SO_RCVWAKEUP, &sw2, &swlen) < 0)
174                        rtems_panic ("getsockopt failed: %s",
175                                     strerror (errno));
176                if (swlen != sizeof sw2
177                    || sw2.sw_pfn != &wakeup
178                    || (int) sw2.sw_arg != clientCount)
179                        rtems_panic ("getsockopt mismatch");
180
181                clientEvents |= RTEMS_EVENT_0 + clientCount;
182        }
183
184        if (rtems_task_ident (RTEMS_SELF, RTEMS_SEARCH_LOCAL_NODE, &tid)
185            != RTEMS_SUCCESSFUL)
186          rtems_panic ("rtems_task_ident failed");
187
188        for (;;) {
189                rtems_event_set events;
190                rtems_status_code status;
191                int i;
192
193                status = rtems_event_receive (clientEvents,
194                                              RTEMS_WAIT | RTEMS_EVENT_ANY,
195                                              RTEMS_MILLISECONDS_TO_TICKS (5000),
196                                              &events);
197
198                if (status == RTEMS_TIMEOUT) {
199                        printf ("Timeout\n");
200                        continue;
201                }
202
203                for (i = 0; i < CLIENT_COUNT; ++i) {
204                        if (events == 0)
205                                break;
206                        if (events & (i + RTEMS_EVENT_0)) {
207                                int fd;
208                                char buf[200];
209                                int nread;
210
211                                fd = clientfd[i];
212                                printf ("Activity on file descriptor %d.\n", fd);
213                                events &= ~ (i + RTEMS_EVENT_0);
214                                nread = read (fd, buf, sizeof buf);
215                                if (nread < 0) {
216                                        printf ("Read error %s.\n", strerror (errno));
217                                        return;
218                                }
219                                if (nread == 0) {
220                                        printf ("EOF\n");
221                                        clientEvents &= ~ (i + RTEMS_EVENT_0);
222                                        close (fd);
223                                        if (--clientCount == 0)
224                                                return;
225                                }
226                                printf ("Read %d.\n", nread);
227                        }
228                }
229        }
230}
231#endif
232
233void
234doSocket (void)
235{
236        echoServer (BASE_PORT);
237}
Note: See TracBrowser for help on using the repository browser.