Following the same idea as the Ecore_Con - Creating a server , this example will demonstrate how to create a client that connects to a specified server through a TCP port.
You can see the full source code at ecore_con_client_simple_example.c.
Starting from the main
function, after reading the command line argument list and initializing the libraries, we try to connect to the server:
After doing this, everything else in main
is setting up callbacks for the client events, starting the main loop and shutting down the libraries after it.
Now let's go to the callbacks. These callbacks are very similar to the server callbacks (our implementation for this example is very simple). On the _add
callback, we just set a data structure to the server, print some information about the server, and send a welcome message to it:
The _del
callback is as simple as the previous one. We free the data associated with the server, print the uptime of this client, and quit the main loop (since there's nothing to do once we disconnect):
The _data
callback is also similar to the server data callback. it will print any received data, and increase the data counter in the structure associated with this server:
You can see the server counterpart functions of the ones used in this example in the Ecore_Con - Creating a server.
This example will connect to the server and start comunicating with it, as demonstrated in the following diagram: