1 //==========================================================================
5 // Simple TCP 'server' test
7 //==========================================================================
8 //####BSDCOPYRIGHTBEGIN####
10 // -------------------------------------------
12 // Portions of this software may have been derived from OpenBSD or other sources,
13 // and are covered by the appropriate copyright disclaimers included herein.
15 // -------------------------------------------
17 //####BSDCOPYRIGHTEND####
18 //==========================================================================
19 //#####DESCRIPTIONBEGIN####
22 // Contributors: gthomas
28 //####DESCRIPTIONEND####
30 //==========================================================================
31 // Network server test code
36 #ifndef CYGPKG_LIBC_STDIO
37 #define perror(s) diag_printf(#s ": %s\n", strerror(errno))
40 #define STACK_SIZE (CYGNUM_HAL_STACK_SIZE_TYPICAL + 0x1000)
41 static char stack[STACK_SIZE];
42 static cyg_thread thread_data;
43 static cyg_handle_t thread_handle;
56 server_test(struct bootp *bp)
60 struct sockaddr_in client_addr, local;
67 s = socket(AF_INET, SOCK_STREAM, 0);
69 pexit("stream socket");
71 if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one))) {
72 pexit("setsockopt SO_REUSEADDR");
74 if (setsockopt(s, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one))) {
75 pexit("setsockopt SO_REUSEPORT");
77 memset(&local, 0, sizeof(local));
78 local.sin_family = AF_INET;
79 local.sin_len = sizeof(local);
80 local.sin_port = htons(7734);
81 local.sin_addr.s_addr = INADDR_ANY;
82 if(bind(s, (struct sockaddr *) &local, sizeof(local)) < 0) {
87 client_len = sizeof(client_addr);
88 if ((client = accept(s, (struct sockaddr *)&client_addr, &client_len)) < 0) {
91 client_len = sizeof(client_addr);
92 getpeername(client, (struct sockaddr *)&client_addr, &client_len);
93 diag_printf("connection from %s:%d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
95 diag_sprintf(buf, "Hello %s:%d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
97 write(client, buf, strlen(buf));
101 FD_SET(client, &in_fds);
102 num = select(client+1, &in_fds, 0, 0, &tv);
104 len = read(client, buf, sizeof(buf)-1);
106 diag_printf("buf = '%s'\n", buf);
107 } else if (num == 0) {
108 diag_printf("No reply - timed out\n");
118 net_test(cyg_addrword_t param)
120 diag_printf("Start SERVER test\n");
121 init_all_network_interfaces();
122 #ifdef CYGHWR_NET_DRIVER_ETH0
124 server_test(ð0_bootp_data);
133 // Create a main thread, so we can run the scheduler and have time 'pass'
134 cyg_thread_create(10, // Priority - just a number
136 0, // entry parameter
137 "Network test", // Name
140 &thread_handle, // Handle
141 &thread_data // Thread data structure
143 cyg_thread_resume(thread_handle); // Start it
144 cyg_scheduler_start();