xref: /haiku/src/tests/system/network/test3.c (revision 5e54f6d4f9dd607ae2afcea4fe72f2f1763e4b5e)
1 #include <stdio.h>
2 #include <kernel/OS.h>
3 #include <string.h>
4 #include <sys/time.h>
5 #include <errno.h>
6 
7 #include "sys/socket.h"
8 #include "netinet/in.h"
9 #include "arpa/inet.h"
10 #include "sys/select.h"
11 
12 #include "ufunc.h"
13 
14 #define THREADS	2
15 #define MIN_SOCK	10
16 #define MAX_SOCK	100
17 #define TIME	10
18 
test_thread(void * data)19 int32 test_thread(void *data)
20 {
21 	int tnum = *(int*)data;
22 	int sock[MAX_SOCK];
23 	int qty = MIN_SOCK;
24 	struct sockaddr_in sa;
25 	int i, rv, totsock = 0;
26 
27 	sa.sin_family = AF_INET;
28 	sa.sin_port = 0;
29 	sa.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
30 	sa.sin_len = sizeof(sa);
31 	memset(&sa.sin_zero, 0, sizeof(sa.sin_zero));
32 
33 	printf("Thread %d, starting test\n", tnum + 1);
34 
35 	while (qty <= MAX_SOCK) {
36 		for (i=0;i < qty;i++) {
37 			if (i >= MAX_SOCK)
38 				break;
39 			sock[i] = socket(AF_INET, SOCK_DGRAM , 0);
40 			totsock++;
41 			if (sock[i] < 0) {
42 				printf("Total of %d sockets created\n", totsock);
43 				err(errno, "Socket creation failed");
44 			}
45 		}
46 		printf("Thread %d: completed creating %d sockets...\n", tnum+1, qty);
47 		for (i=0;i < qty;i++) {
48 			if (i >= MAX_SOCK)
49 				break;
50 			rv = bind(sock[i],	(struct sockaddr*)&sa, sizeof(sa));
51 			if (rv < 0)
52 				err(errno, "Failed to bind!");
53 		}
54 		for (i=0;i < qty;i++) {
55 			if (i >= MAX_SOCK)
56 				break;
57 			rv = close(sock[i]);
58 			if (rv < 0)
59 				err(errno, "Failed to close socket!");
60 		}
61 		qty += (MAX_SOCK - MIN_SOCK) / 10;
62 	}
63 
64 	printf( "Thread %d complete\n", tnum);
65 }
66 
main(int argc,char ** argv)67 int main(int argc, char **argv)
68 {
69 	thread_id t[THREADS];
70 	int i;
71 	status_t retval;
72 
73 	test_banner("Simultaneous socket creation test");
74 
75 	for (i=0;i<THREADS;i++) {
76 		t[i] = spawn_thread(test_thread, "socket test thread",
77 			B_NORMAL_PRIORITY, &i);
78 		if (t[i] >= 0)
79 			resume_thread(t[i]);
80 	}
81 
82 	for (i=0;i<THREADS;i++) {
83 		wait_for_thread(t[i], &retval);
84 	}
85 
86 	return (0);
87 }
88 
89 
90