1 Nanopb example "network_server"
2 ===============================
4 This example demonstrates the use of nanopb to communicate over network
5 connections. It consists of a server that sends file listings, and of
6 a client that requests the file list from the server.
11 user@host:~/nanopb/examples/network_server$ make # Build the example
12 protoc -ofileproto.pb fileproto.proto
13 python ../../generator/nanopb_generator.py fileproto.pb
14 Writing to fileproto.pb.h and fileproto.pb.c
15 cc -ansi -Wall -Werror -I .. -g -O0 -I../.. -o server server.c
16 ../../pb_decode.c ../../pb_encode.c fileproto.pb.c common.c
17 cc -ansi -Wall -Werror -I .. -g -O0 -I../.. -o client client.c
18 ../../pb_decode.c ../../pb_encode.c fileproto.pb.c common.c
20 user@host:~/nanopb/examples/network_server$ ./server & # Start the server on background
23 petteri@oddish:~/nanopb/examples/network_server$ ./client /bin # Request the server to list /bin
25 Listing directory: /bin
40 Details of implementation
41 -------------------------
42 fileproto.proto contains the portable Google Protocol Buffers protocol definition.
43 It could be used as-is to implement a server or a client in any other language, for
44 example Python or Java.
46 fileproto.options contains the nanopb-specific options for the protocol file. This
47 sets the amount of space allocated for file names when decoding messages.
49 common.c/h contains functions that allow nanopb to read and write directly from
50 network socket. This way there is no need to allocate a separate buffer to store
53 server.c contains the code to open a listening socket, to respond to clients and
54 to list directory contents.
56 client.c contains the code to connect to a server, to send a request and to print
59 The code is implemented using the POSIX socket api, but it should be easy enough
60 to port into any other socket api, such as lwip.