GNet Examples

echoclient and echoserver
dnslookup, hash, hfetch, hostinfo, and SDR

GNet comes with several example programs. They are intended 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.

The tests in the tests directory also demonstrate to use several modules: MD5/SHA, InetAddr, IPv6, Pack/Unpack, and URI.

echoclient and echoserver

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 client and server. There are three versions of the TCP echoclient and echoserver. Each demonstrates a different method of using GNet: blocking, asynchronous, and object GConn/GServer. There is also a blocking UDP example and a Unix sockets example.

echoclient/echoserver. The blocking echoserver 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.

echoclient-async/echoserver-async. The asynchronous 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 connections when it isn't reading or writing to another socket. The advantage of the this method is that multiple clients can be served at once.

echoclient-gconn/echoserver-gserver. echoclient-gconn uses GNet's GConn object. echoserver-gserver uses GNet's GServer object. GConn and GServer are high-level wrappers around GTcpSocket. Using GConn and GServer is not as flexible as using GTcpSocket directly, but it is much easier and is flexible enough for most applications.

There is no example using threads. If you would like to contribute one, please send it to us.