ecore_con_server_simple_example.c

Shows how to setup a simple server that accepts client connections and sends a "hello" string to them. See the complete example description at Ecore_Con - Creating a server

//Compile with:
// gcc -o ecore_con_server_simple_example ecore_con_server_simple_example.c `pkg-config --libs --cflags ecore ecore-con eina`
#include <stdio.h>
#include <Ecore.h>
#include <Ecore_Con.h>
struct _Client
{
int sdata;
};
_add(void *data EINA_UNUSED, int type EINA_UNUSED, Ecore_Con_Event_Client_Add *ev)
{
char welcome[] = "hello! - sent from the server";
const Eina_List *clients, *l;
struct _Client *client = malloc(sizeof(*client));
client->sdata = 0;
printf("Client with ip %s, port %d, connected = %d!\n",
ecore_con_client_send(ev->client, welcome, sizeof(welcome));
printf("Clients connected to this server:\n");
EINA_LIST_FOREACH(clients, l, cl)
printf("%s\n", ecore_con_client_ip_get(cl));
}
_del(void *data EINA_UNUSED, int type EINA_UNUSED, Ecore_Con_Event_Client_Del *ev)
{
struct _Client *client;
if (!ev->client)
printf("Lost client with ip %s!\n", ecore_con_client_ip_get(ev->client));
if (client)
{
printf("Total data received from this client: %d\n", client->sdata);
free(client);
}
printf("Client was connected for %0.3f seconds.\n",
}
_data(void *data EINA_UNUSED, int type EINA_UNUSED, Ecore_Con_Event_Client_Data *ev)
{
char fmt[128];
struct _Client *client = ecore_con_client_data_get(ev->client);
snprintf(fmt, sizeof(fmt),
"Received %i bytes from client %s port %d:\n"
">>>>>\n"
"%%.%is\n"
">>>>>\n",
printf(fmt, ev->data);
client->sdata += ev->size;
}
int
main(void)
{
const Eina_List *clients, *l;
if (!(svr = ecore_con_server_add(ECORE_CON_REMOTE_TCP, "127.0.0.1", 8080, NULL)))
exit(1);
printf("Clients connected to this server when exiting: %d\n",
eina_list_count(clients));
EINA_LIST_FOREACH(clients, l, cl)
{
printf("%s\n", ecore_con_client_ip_get(cl));
}
printf("Server was up for %0.3f seconds\n",
return 0;
}