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)
58 int s, client, client_len;
59 struct sockaddr_in client_addr, local;
66 s = socket(AF_INET, SOCK_STREAM, 0);
68 pexit("stream socket");
70 if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one))) {
71 pexit("setsockopt SO_REUSEADDR");
73 if (setsockopt(s, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one))) {
74 pexit("setsockopt SO_REUSEPORT");
76 memset(&local, 0, sizeof(local));
77 local.sin_family = AF_INET;
78 local.sin_len = sizeof(local);
79 local.sin_port = htons(7734);
80 local.sin_addr.s_addr = INADDR_ANY;
81 if(bind(s, (struct sockaddr *) &local, sizeof(local)) < 0) {
86 client_len = sizeof(client_addr);
87 if ((client = accept(s, (struct sockaddr *)&client_addr, &client_len)) < 0) {
90 client_len = sizeof(client_addr);
91 getpeername(client, (struct sockaddr *)&client_addr, &client_len);
92 diag_printf("connection from %s:%d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
94 #ifdef CYGPKG_LIBC_STDIO
95 sprintf(buf, "Hello %s:%d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
97 strcpy(buf, "Hello ");
98 strcat(buf, inet_ntoa(client_addr.sin_addr));
100 strcat(buf, atoi(ntohs(client_addr.sin_port)));
104 write(client, buf, strlen(buf));
108 FD_SET(client, &in_fds);
109 num = select(client+1, &in_fds, 0, 0, &tv);
111 len = read(client, buf, sizeof(buf)-1);
113 diag_printf("buf = '%s'\n", buf);
114 } else if (num == 0) {
115 diag_printf("No reply - timed out\n");
125 net_test(cyg_addrword_t param)
127 diag_printf("Start SERVER test\n");
128 init_all_network_interfaces();
129 #ifdef CYGHWR_NET_DRIVER_ETH0
131 server_test(ð0_bootp_data);
140 // Create a main thread, so we can run the scheduler and have time 'pass'
141 cyg_thread_create(10, // Priority - just a number
143 0, // entry parameter
144 "Network test", // Name
147 &thread_handle, // Handle
148 &thread_data // Thread data structure
150 cyg_thread_resume(thread_handle); // Start it
151 cyg_scheduler_start();