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, 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.
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.
A third 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 (like Linux). If you feel like implementing this, please email the code and I will include it.