In contrast to Cygwin 1.5 Cygwin 1.7 doesn't allow to get __argc out of thin air...
[contiki-2.x.git] / doc / example-psock-server.c
blobf297a43e0fcad630217b907a2571676bee32dd79
1 /*
2 * This is a small example of how to write a TCP server using
3 * Contiki's protosockets. It is a simple server that accepts one line
4 * of text from the TCP connection, and echoes back the first 10 bytes
5 * of the string, and then closes the connection.
7 * The server only handles one connection at a time.
9 */
11 #include <string.h>
14 * We include "contiki-net.h" to get all network definitions and
15 * declarations.
17 #include "contiki-net.h"
20 * We define one protosocket since we've decided to only handle one
21 * connection at a time. If we want to be able to handle more than one
22 * connection at a time, each parallell connection needs its own
23 * protosocket.
25 static struct psock ps;
28 * We must have somewhere to put incoming data, and we use a 10 byte
29 * buffer for this purpose.
31 static char buffer[10];
33 /*---------------------------------------------------------------------------*/
35 * A protosocket always requires a protothread. The protothread
36 * contains the code that uses the protosocket. We define the
37 * protothread here.
39 static
40 PT_THREAD(handle_connection(struct psock *p))
43 * A protosocket's protothread must start with a PSOCK_BEGIN(), with
44 * the protosocket as argument.
46 * Remember that the same rules as for protothreads apply: do NOT
47 * use local variables unless you are very sure what you are doing!
48 * Local (stack) variables are not preserved when the protothread
49 * blocks.
51 PSOCK_BEGIN(p);
54 * We start by sending out a welcoming message. The message is sent
55 * using the PSOCK_SEND_STR() function that sends a null-terminated
56 * string.
58 PSOCK_SEND_STR(p, "Welcome, please type something and press return.\n");
61 * Next, we use the PSOCK_READTO() function to read incoming data
62 * from the TCP connection until we get a newline character. The
63 * number of bytes that we actually keep is dependant of the length
64 * of the input buffer that we use. Since we only have a 10 byte
65 * buffer here (the buffer[] array), we can only remember the first
66 * 10 bytes received. The rest of the line up to the newline simply
67 * is discarded.
69 PSOCK_READTO(p, '\n');
72 * And we send back the contents of the buffer. The PSOCK_DATALEN()
73 * function provides us with the length of the data that we've
74 * received. Note that this length will not be longer than the input
75 * buffer we're using.
77 PSOCK_SEND_STR(p, "Got the following data: ");
78 PSOCK_SEND(p, buffer, PSOCK_DATALEN(p));
79 PSOCK_SEND_STR(p, "Good bye!\r\n");
82 * We close the protosocket.
84 PSOCK_CLOSE(p);
87 * And end the protosocket's protothread.
89 PSOCK_END(p);
91 /*---------------------------------------------------------------------------*/
93 * We declare the process.
95 PROCESS(example_psock_server_process, "Example protosocket server");
96 /*---------------------------------------------------------------------------*/
98 * The definition of the process.
100 PROCESS_THREAD(example_psock_server_process, ev, data)
103 * The process begins here.
105 PROCESS_BEGIN();
108 * We start with setting up a listening TCP port. Note how we're
109 * using the HTONS() macro to convert the port number (1010) to
110 * network byte order as required by the tcp_listen() function.
112 tcp_listen(HTONS(1010));
115 * We loop for ever, accepting new connections.
117 while(1) {
120 * We wait until we get the first TCP/IP event, which probably
121 * comes because someone connected to us.
123 PROCESS_WAIT_EVENT_UNTIL(ev == tcpip_event);
126 * If a peer connected with us, we'll initialize the protosocket
127 * with PSOCK_INIT().
129 if(uip_connected()) {
132 * The PSOCK_INIT() function initializes the protosocket and
133 * binds the input buffer to the protosocket.
135 PSOCK_INIT(&ps, buffer, sizeof(buffer));
138 * We loop until the connection is aborted, closed, or times out.
140 while(!(uip_aborted() || uip_closed() || uip_timedout())) {
143 * We wait until we get a TCP/IP event. Remember that we
144 * always need to wait for events inside a process, to let
145 * other processes run while we are waiting.
147 PROCESS_WAIT_EVENT_UNTIL(ev == tcpip_event);
150 * Here is where the real work is taking place: we call the
151 * handle_connection() protothread that we defined above. This
152 * protothread uses the protosocket to receive the data that
153 * we want it to.
155 handle_connection(&ps);
161 * We must always declare the end of a process.
163 PROCESS_END();
165 /*---------------------------------------------------------------------------*/