Ecore_Con - Creating a client

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:

main(int argc, const char *argv[])
{
const char *address;
int port = 8080;
if (argc < 2)
{
printf("wrong usage. Command syntax is:\n");
printf("\tecore_con_client_simple_example <address> [port]\n");
exit(1);
}
address = argv[1];
if (argc > 2)
port = atoi(argv[2]);
if (!(svr = ecore_con_server_connect(ECORE_CON_REMOTE_TCP, address, port, NULL)))
{
printf("could not connect to the server: %s, port %d.\n",
address, port);
exit(2);
}

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:

_add(void *data EINA_UNUSED, int type EINA_UNUSED, Ecore_Con_Event_Server_Add *ev)
{
char welcome[] = "hello! - sent from the client";
struct _Server *server = malloc(sizeof(*server));
server->sdata = 0;
printf("Server with ip %s, name %s, port %d, connected = %d!\n",
ecore_con_server_send(ev->server, welcome, sizeof(welcome));
}

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):

_del(void *data EINA_UNUSED, int type EINA_UNUSED, Ecore_Con_Event_Server_Del *ev)
{
if (!ev->server)
{
printf("Failed to establish connection to the server.\nExiting.\n");
}

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:

_data(void *data EINA_UNUSED, int type EINA_UNUSED, Ecore_Con_Event_Server_Data *ev)
{
char fmt[128];
struct _Server *server = ecore_con_server_data_get(ev->server);
snprintf(fmt, sizeof(fmt),
"Received %i bytes from server:\n"
">>>>>\n"
"%%.%is\n"
">>>>>\n",
ev->size, ev->size);
printf(fmt, ev->data);
server->sdata += ev->size;
}

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:

Note
This example contains a serious security flaw: it doesn't check for the size of data being received, thus allowing to the string to be exploited in some way. However, it is left like this to make the code simpler and just demonstrate the API usage.
_Ecore_Con_Event_Server_Add
Definition: Ecore_Con.h:504
ecore_con_server_connect
EAPI Ecore_Con_Server * ecore_con_server_connect(Ecore_Con_Type type, const char *name, int port, const void *data)
Creates a connection to the specified server and return an associated object.
Definition: ecore_con_legacy.c:2165
_Ecore_Con_Event_Server_Data
Definition: Ecore_Con.h:553
ecore_main_loop_quit
void ecore_main_loop_quit(void)
Quits the main loop once all the events currently on the queue have been processed.
Definition: ecore_main.c:1300
ecore_con_server_name_get
EAPI const char * ecore_con_server_name_get(const Ecore_Con_Server *svr)
Retrieves the name of server.
Definition: ecore_con_legacy.c:2260
EINA_UNUSED
#define EINA_UNUSED
Definition: eina_types.h:339
ecore_con_server_port_get
EAPI int ecore_con_server_port_get(const Ecore_Con_Server *svr)
Retrieves the server port in use.
Definition: ecore_con_legacy.c:2347
eina_init
EAPI int eina_init(void)
Initializes the Eina library.
Definition: eina_main.c:279
_Ecore_Con_Event_Server_Data::server
Ecore_Con_Server * server
the server that was connected to
Definition: Ecore_Con.h:554
ecore_con_server_data_get
EAPI void * ecore_con_server_data_get(Ecore_Con_Server *svr)
Retrieves the data associated with the given server.
Definition: ecore_con_legacy.c:2315
ECORE_CALLBACK_RENEW
#define ECORE_CALLBACK_RENEW
Return value to keep a callback.
Definition: Ecore_Common.h:153
Ecore_Con_Server
struct _Ecore_Con_Server Ecore_Con_Server
Definition: Ecore_Con.h:318
ecore_con_server_data_set
EAPI void * ecore_con_server_data_set(Ecore_Con_Server *svr, void *data)
Sets the data associated with the given server.
Definition: ecore_con_legacy.c:2322
ecore_con_server_connected_get
EAPI Eina_Bool ecore_con_server_connected_get(const Ecore_Con_Server *svr)
Retrieves whether the given server is currently connected.
Definition: ecore_con_legacy.c:2334
Eina_Bool
unsigned char Eina_Bool
Definition: eina_types.h:527
ecore_con_init
EAPI int ecore_con_init(void)
Initializes the Ecore_Con library.
Definition: ecore_con.c:72
_Ecore_Con_Event_Server_Data::data
void * data
the data that the server sent
Definition: Ecore_Con.h:555
_Ecore_Con_Event_Server_Del
Definition: Ecore_Con.h:523
ecore_con_server_flush
EAPI void ecore_con_server_flush(Ecore_Con_Server *svr)
Flushes all pending data to the given server.
Definition: ecore_con_legacy.c:2412
_Ecore_Con_Event_Server_Add::server
Ecore_Con_Server * server
the server that was connected to
Definition: Ecore_Con.h:505
ecore_con_server_ip_get
EAPI const char * ecore_con_server_ip_get(const Ecore_Con_Server *svr)
Gets the IP address of a server that has been connected to.
Definition: ecore_con_legacy.c:2398
_Ecore_Con_Event_Server_Data::size
int size
the length of the data sent
Definition: Ecore_Con.h:556
ecore_con_server_send
EAPI int ecore_con_server_send(Ecore_Con_Server *svr, const void *data, int size)
Sends the given data to the given server.
Definition: ecore_con_legacy.c:2354
_Ecore_Con_Event_Server_Del::server
Ecore_Con_Server * server
the client that was lost
Definition: Ecore_Con.h:524
ecore_init
EAPI int ecore_init(void)
Sets up connections, signal handlers, sockets etc.
Definition: ecore.c:229