• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1Nanopb example "network_server"
2===============================
3
4This example demonstrates the use of nanopb to communicate over network
5connections. It consists of a server that sends file listings, and of
6a client that requests the file list from the server.
7
8Example usage
9-------------
10
11user@host:~/nanopb/examples/network_server$ make        # Build the example
12protoc -ofileproto.pb fileproto.proto
13python ../../generator/nanopb_generator.py fileproto.pb
14Writing to fileproto.pb.h and fileproto.pb.c
15cc -ansi -Wall -Werror -I .. -g -O0 -I../.. -o server server.c
16    ../../pb_decode.c ../../pb_encode.c fileproto.pb.c common.c
17cc -ansi -Wall -Werror -I .. -g -O0 -I../.. -o client client.c
18    ../../pb_decode.c ../../pb_encode.c fileproto.pb.c common.c
19
20user@host:~/nanopb/examples/network_server$ ./server &  # Start the server on background
21[1] 24462
22
23petteri@oddish:~/nanopb/examples/network_server$ ./client /bin  # Request the server to list /bin
24Got connection.
25Listing directory: /bin
261327119    bzdiff
271327126    bzless
281327147    ps
291327178    ntfsmove
301327271    mv
311327187    mount
321327259    false
331327266    tempfile
341327285    zfgrep
351327165    gzexe
361327204    nc.openbsd
371327260    uname
38
39
40Details of implementation
41-------------------------
42fileproto.proto contains the portable Google Protocol Buffers protocol definition.
43It could be used as-is to implement a server or a client in any other language, for
44example Python or Java.
45
46fileproto.options contains the nanopb-specific options for the protocol file. This
47sets the amount of space allocated for file names when decoding messages.
48
49common.c/h contains functions that allow nanopb to read and write directly from
50network socket. This way there is no need to allocate a separate buffer to store
51the message.
52
53server.c contains the code to open a listening socket, to respond to clients and
54to list directory contents.
55
56client.c contains the code to connect to a server, to send a request and to print
57the response message.
58
59The code is implemented using the POSIX socket api, but it should be easy enough
60to port into any other socket api, such as lwip.
61