ecore_con_client_simple_example.c

- Main Page
- Related Pages
- Topics
- Data Structures
- Files
- Examples

ecore_con_client_simple_example.c

Shows how to setup a simple client that connects to a server and sends a "hello" string to it.

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

//Compile with:
// gcc -o ecore_con_client_simple_example ecore_con_client_simple_example.c `pkg-config --libs --cflags ecore ecore-con eina`
#include <stdio.h>
#include <Ecore.h>
#include <Ecore_Con.h>
struct _Server
{
int sdata;
};
Eina_Bool
_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;
ecore_con_server_data_set(ev->server, server);
printf("Server with ip %s, name %s, port %d, connected = %d!\n",
ecore_con_server_ip_get(ev->server),
ecore_con_server_name_get(ev->server),
ecore_con_server_port_get(ev->server),
ecore_con_server_connected_get(ev->server));
ecore_con_server_send(ev->server, welcome, sizeof(welcome));
ecore_con_server_flush(ev->server);
return ECORE_CALLBACK_RENEW;
}
Eina_Bool
_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");
ecore_main_loop_quit();
return ECORE_CALLBACK_RENEW;
}
struct _Server *server = ecore_con_server_data_get(ev->server);
printf("Lost server with ip %s!\n", ecore_con_server_ip_get(ev->server));
if (server)
{
printf("Total data received from this server: %d\n", server->sdata);
free(server);
}
ecore_con_server_del(ev->server);
ecore_main_loop_quit();
return ECORE_CALLBACK_RENEW;
}
Eina_Bool
_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;
return ECORE_CALLBACK_RENEW;
}
int
main(int argc, const char *argv[])
{
Ecore_Con_Server *svr;
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]);
eina_init();
ecore_init();
ecore_con_init();
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);
}
/* set event handler for server connect */
ecore_event_handler_add(ECORE_CON_EVENT_SERVER_ADD, (Ecore_Event_Handler_Cb)_add, NULL);
/* set event handler for server disconnect */
ecore_event_handler_add(ECORE_CON_EVENT_SERVER_DEL, (Ecore_Event_Handler_Cb)_del, NULL);
/* set event handler for receiving server data */
ecore_event_handler_add(ECORE_CON_EVENT_SERVER_DATA, (Ecore_Event_Handler_Cb)_data, NULL);
/* start client */
ecore_main_loop_begin();
ecore_con_shutdown();
ecore_shutdown();
eina_shutdown();
return 0;
}
ECORE_CON_EVENT_SERVER_ADD
ECORE_CON_API int ECORE_CON_EVENT_SERVER_ADD
A server was created.
Definition ecore_con_legacy.c:160
ECORE_CON_EVENT_SERVER_DEL
ECORE_CON_API int ECORE_CON_EVENT_SERVER_DEL
A server connection was lost.
Definition ecore_con_legacy.c:154
ECORE_CON_EVENT_SERVER_DATA
ECORE_CON_API int ECORE_CON_EVENT_SERVER_DATA
A server connection object has data.
Definition ecore_con_legacy.c:161
ecore_con_shutdown
ECORE_CON_API int ecore_con_shutdown(void)
Shuts down the Ecore_Con library.
Definition ecore_con.c:133
ecore_con_init
ECORE_CON_API int ecore_con_init(void)
Initializes the Ecore_Con library.
Definition ecore_con.c:68
ecore_con_server_port_get
ECORE_CON_API int ecore_con_server_port_get(const Ecore_Con_Server *svr)
Retrieves the server port in use.
Definition ecore_con_legacy.c:2347
ecore_con_server_data_set
ECORE_CON_API 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_del
ECORE_CON_API void * ecore_con_server_del(Ecore_Con_Server *svr)
Closes the connection and free the given server.
Definition ecore_con_legacy.c:2302
ecore_con_server_ip_get
ECORE_CON_API 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_server_name_get
ECORE_CON_API const char * ecore_con_server_name_get(const Ecore_Con_Server *svr)
Retrieves the name of server.
Definition ecore_con_legacy.c:2260
ecore_con_server_data_get
ECORE_CON_API 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_con_server_connected_get
ECORE_CON_API 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
ecore_con_server_connect
ECORE_CON_API 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_server_flush
ECORE_CON_API 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_server_send
ECORE_CON_API 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_Event_Handler_Cb
Eina_Bool(* Ecore_Event_Handler_Cb)(void *data, int type, void *event)
A callback used by the main loop to handle events of a specified type.
Definition Ecore_Common.h:603
ecore_event_handler_add
Ecore_Event_Handler * ecore_event_handler_add(int type, Ecore_Event_Handler_Cb func, const void *data)
Adds an event handler.
Definition ecore_events.c:13
ecore_shutdown
EAPI int ecore_shutdown(void)
Shuts down connections, signal handlers sockets etc.
Definition ecore.c:371
ecore_init
EAPI int ecore_init(void)
Sets up connections, signal handlers, sockets etc.
Definition ecore.c:230
ECORE_CALLBACK_RENEW
#define ECORE_CALLBACK_RENEW
Return value to keep a callback.
Definition Ecore_Common.h:153
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:1326
ecore_main_loop_begin
void ecore_main_loop_begin(void)
Runs the application main loop.
Definition ecore_main.c:1316
eina_shutdown
EINA_API int eina_shutdown(void)
Shuts down the Eina library.
Definition eina_main.c:379
eina_init
EINA_API int eina_init(void)
Initializes the Eina library.
Definition eina_main.c:291
Eina_Bool
unsigned char Eina_Bool
Type to mimic a boolean.
Definition eina_types.h:527
EINA_UNUSED
#define EINA_UNUSED
Used to indicate that a function parameter is purposely unused.
Definition eina_types.h:339
_Ecore_Con_Event_Server_Add
Used as the data param for the ECORE_CON_EVENT_SERVER_ADD event.
Definition Ecore_Con.h:480
_Ecore_Con_Event_Server_Add::server
Ecore_Con_Server * server
the server that was connected to
Definition Ecore_Con.h:481
_Ecore_Con_Event_Server_Data
Used as the data param for the ECORE_CON_EVENT_SERVER_DATA event.
Definition Ecore_Con.h:529
_Ecore_Con_Event_Server_Data::size
int size
the length of the data sent
Definition Ecore_Con.h:532
_Ecore_Con_Event_Server_Data::server
Ecore_Con_Server * server
the server that was connected to
Definition Ecore_Con.h:530
_Ecore_Con_Event_Server_Data::data
void * data
the data that the server sent
Definition Ecore_Con.h:531
_Ecore_Con_Event_Server_Del
Used as the data param for the ECORE_CON_EVENT_SERVER_DEL event.
Definition Ecore_Con.h:499
_Ecore_Con_Event_Server_Del::server
Ecore_Con_Server * server
the client that was lost
Definition Ecore_Con.h:500