]> git.karo-electronics.de Git - karo-tx-redboot.git/blob - packages/net/lwip_tcpip/v2_0/tests/socket.c
ff0b917ac826fbf55ae96e2fdd0d153dec802f07
[karo-tx-redboot.git] / packages / net / lwip_tcpip / v2_0 / tests / socket.c
1 //==========================================================================
2 //####ECOSGPLCOPYRIGHTBEGIN####
3 // -------------------------------------------
4 // This file is part of eCos, the Embedded Configurable Operating System.
5 // Copyright (C) 2004 eCosCentric 
6 //
7 // eCos is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU General Public License as published by the Free
9 // Software Foundation; either version 2 or (at your option) any later version.
10 //
11 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
12 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 // for more details.
15 //
16 // You should have received a copy of the GNU General Public License along
17 // with eCos; if not, write to the Free Software Foundation, Inc.,
18 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 //
20 // As a special exception, if other files instantiate templates or use macros
21 // or inline functions from this file, or you compile this file and link it
22 // with other works to produce a work based on this file, this file does not
23 // by itself cause the resulting work to be covered by the GNU General Public
24 // License. However the source code for this file must still be made available
25 // in accordance with section (3) of the GNU General Public License.
26 //
27 // This exception does not invalidate any other reasons why a work based on
28 // this file might be covered by the GNU General Public License.
29 // -------------------------------------------
30 //####ECOSGPLCOPYRIGHTEND####
31 //==========================================================================
32
33 /* Simple test-case for the BSD socket API  : echo on TCP port 7 */
34
35 #include "lwip/sys.h"
36 #define LWIP_COMPAT_SOCKETS 1
37 #include "lwip/sockets.h"
38
39 char buf[400];
40 static void
41 socket_thread(void *arg)
42 {
43         int sock,s;
44         int len;
45         struct sockaddr_in addr,rem;
46         sock = socket(AF_INET,SOCK_STREAM,0);   
47         addr.sin_family = AF_INET;
48         addr.sin_port = htons(7);
49         addr.sin_addr.s_addr = INADDR_ANY;
50
51         bind(sock,(struct sockaddr *)&addr,sizeof(addr));
52                         
53         listen(sock,5);
54         while(1) {      
55                 len = sizeof(rem);
56                 s = accept(sock,(struct sockaddr*)&rem,&len);
57                 while((len = read(s,buf,400)) > 0)
58                         write(s,buf,len);
59                 close(s);
60         }       
61 }
62
63 void
64 tmain(cyg_addrword_t p)
65 {
66   lwip_init();  
67   sys_thread_new(socket_thread, (void*)"socket",7);
68 }
69
70 #define STACK_SIZE 0x1000
71 static char stack[STACK_SIZE];
72 static cyg_thread thread_data;
73 static cyg_handle_t thread_handle;
74
75 void
76 cyg_user_start(void)
77 {
78     // Create a main thread, so we can run the scheduler and have time 'pass'
79     cyg_thread_create(10,                // Priority - just a number
80                       tmain,          // entry
81                       0,                 // entry parameter
82                       "socket echo test",        // Name
83                       &stack[0],         // Stack
84                       STACK_SIZE,        // Size
85                       &thread_handle,    // Handle
86                       &thread_data       // Thread data structure
87             );
88     cyg_thread_resume(thread_handle);  // Start it
89 }
90