summaryrefslogtreecommitdiffstats
path: root/select/test.c
blob: 1dbe284b74639935ce1616c6abce740ad275ae30 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
 * Test RTEMS networking
 *
 * This program may be distributed and used for any purpose.
 * I ask only that you:
 *      1. Leave this author information intact.
 *      2. Document any changes you make.
 *
 * W. Eric Norum
 * Saskatchewan Accelerator Laboratory
 * University of Saskatchewan
 * Saskatoon, Saskatchewan, CANADA
 * eric@skatter.usask.ca
 */

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <rtems.h>
#include <rtems/rtems_bsdnet.h>
#include <rtems/error.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <netinet/in.h>

#define BASE_PORT	24742
#define CLIENT_COUNT	2

static int clientfd[CLIENT_COUNT];

static void
getClients (unsigned short port)
{
	int s, s1;
	struct sockaddr_in myAddr, farAddr;
	socklen_t addrlen;
	int clientCount;

	printf ("Create socket.\n");
	s = socket (AF_INET, SOCK_STREAM, 0);
	if (s < 0)
		rtems_panic ("Can't create socket: %s", strerror (errno));
	myAddr.sin_family = AF_INET;
	myAddr.sin_port = htons (port);
	myAddr.sin_addr.s_addr = INADDR_ANY;
	memset (myAddr.sin_zero, '\0', sizeof myAddr.sin_zero);
	printf ("Bind socket.\n");
	if (bind (s, (struct sockaddr *)&myAddr, sizeof myAddr) < 0)
		rtems_panic ("Can't bind socket: %s", strerror (errno));
	printf ("Listen.\n");
	if (listen (s, 2) < 0)
		rtems_panic ("Can't listen on socket: %s", strerror (errno));

	/*
	 * Accumulate clients
	 */
	for (clientCount = 0 ; clientCount < CLIENT_COUNT ; clientCount++) {
		printf ("Accept.\n");
		addrlen = sizeof farAddr;
		s1 = accept (s, (struct sockaddr *)&farAddr, &addrlen);
		if (s1 < 0)
			rtems_panic ("Can't accept connection: %s", strerror (errno));
		else
			printf ("ACCEPTED:%lX\n", ntohl (farAddr.sin_addr.s_addr));
		clientfd[clientCount] = s1;
	}

	close (s);
}

static void
echoServer (unsigned short port)
{
	fd_set clientfdset;
	int clientCount;
	int topfd = 0;

	getClients (port);

	FD_ZERO (&clientfdset);

	for (clientCount = 0 ; clientCount < CLIENT_COUNT ; clientCount++) {
		int s1;

		s1 = clientfd[clientCount];
		FD_SET (s1, &clientfdset);
		if (s1 > topfd)
			topfd = s1;
	}

	/*
	 * Run clients
	 */
	for (;;) {
		fd_set readfdset;
		struct timeval tv;
		int n;
		int i;

		tv.tv_sec = 5;
		tv.tv_usec = 0;
		readfdset = clientfdset;
		n = select (topfd + 1, &readfdset, NULL, NULL, &tv);
		if (n < 0) {
			printf ("Select() error: %s\n", strerror (errno));
			return;
		}
		if (n == 0) {
			printf ("Timeout\n");
			continue;
		}
	
		printf ("Activity on %d file descriptor%s.\n", n, n == 1 ? "" : "s");
		for (i = 0 ; n && (i < CLIENT_COUNT) ; i++) {
			int fd = clientfd[i];
			if (FD_ISSET (fd, &readfdset)) {
				char buf[200];
				int nread;

				printf ("Activity on file descriptor %d.\n", fd);
				n--;
				nread = read (fd, buf, sizeof buf);
				if (nread < 0) {
					printf ("Read error %s.\n", strerror (errno));
					return;
				}
				if (nread == 0) {
					printf ("EOF\n");
					FD_CLR (fd, &clientfdset);
					close (fd);
					if (--clientCount == 0)
						return;
				}
				printf ("Read %d.\n", nread);
			}
		}
	}
}

void
doSocket (void)
{
	echoServer (BASE_PORT);
}