GNet Network Library Reference Manual | |||
---|---|---|---|
<<< Previous Page | Home | Next Page >>> |
GNet comes with several example programs. They are intented to be used by developers as examples of how to use GNet. The example programs are in the examples directory that comes with the source code. To build the examples, install GNet then go to this directory and type make examples. This section describes what each of the example programs does.
The echoclient connects to the echoserver and sends data it reads in from the user. echoserver then sends it back and echoclient prints it out. These programs demonstrate how to write a basic TCP-based client and server. echoclient-udp and echoserver-udp are the UDP equivalents.
There are two methods of writing an server demonstrated: blocking and non-blocking. The blocking methods accepts a connection and reads and writes to the socket until the socket is closed. No more than one echoclient can be connected at once. A good, robust server wouldn't use this method, but it is suitable for many simple applications.
The second method is the non-blocking method. The server does not block while reading or writing to a socket or waiting for a connect. Since it's never blocked, it can accept new connects when it isn't reading or writing to another socket. Really it could block a little bit on those reads or writes, but since it only reads when there's something to be read and writes when there's buffer space to write into, this is rare.
The advantage of the non-blocking method is that multiple clients can be served at once. It's generally the best method to use. We use the glib event loop and GIOChannels for this.
(There is also an experimental third object method that uses the Conn and Server modules. If you are adventurous, you can use this method. Note that these modules may be buggy and the interfaces may change.)
Another method, not implemented yet, is to use threads. The advantage is that it's easier to code. The disadvantage is it's not as efficient on a single-processor machine or on a machine that doesn't have a good threads implementation. If you feel like implementing this, please email us the code and we will include it.