In this example we are going to create a server that listens for connections from clients through a T\+C\+P port.

You can get the full source code at \hyperlink{ecore_con_server_simple_example_8c-example}{ecore\+\_\+con\+\_\+server\+\_\+simple\+\_\+example.\+c}.

We begin our example in the main function, to demonstrate how to setup things, and then go to the callbacks that are needed for it to run properly.

In the {\ttfamily main} function, after initializing the libraries, we use \hyperlink{group__Ecore__Con__Server__Group_gaf450c4039d5e61d4f7a58d7b67efc1e8}{ecore\+\_\+con\+\_\+server\+\_\+add()} to startup the server. Look at the reference documentation of this function\+: it supports many types of server, and we are going to use \hyperlink{group__Ecore__Con__Lib__Group_gga4862bfa05383e6e16a1dfc6eafc90564ab02a4b8522d1ed409cfb24131b24c4ed}{E\+C\+O\+R\+E\+\_\+\+C\+O\+N\+\_\+\+R\+E\+M\+O\+T\+E\+\_\+\+T\+C\+P} (a T\+C\+P based server). Other arguments to this function are the address where we are listening on, the port, and a data pointer that will associate that data with the server\+:

 
\begin{DoxyCodeInclude}
main(\textcolor{keywordtype}{void})
\{
   Ecore\_Con\_Server *svr;
   Ecore\_Con\_Client *cl;
   \textcolor{keyword}{const} \hyperlink{struct__Eina__List}{Eina\_List} *clients, *l;

   \hyperlink{group__Eina__Main__Group_gabf448ada5bc81a07342b66c6f818ee5e}{eina\_init}();
   \hyperlink{group__Ecore__Init__Group_ga77757609684a2c922dc5ec398274751b}{ecore\_init}();
   \hyperlink{group__Ecore__Con__Events__Group_gacf5ecf42c516bb9c8b6759721e9559bd}{ecore\_con\_init}();

   \textcolor{keywordflow}{if} (!(svr = \hyperlink{group__Ecore__Con__Server__Group_gaf450c4039d5e61d4f7a58d7b67efc1e8}{ecore\_con\_server\_add}(\hyperlink{group__Ecore__Con__Lib__Group_gga4862bfa05383e6e16a1dfc6eafc90564ab02a4b8522d1ed409cfb24131b24c4ed}{ECORE\_CON\_REMOTE\_TCP}, \textcolor{stringliteral}{"
      127.0.0.1"}, 8080, NULL)))
     exit(1);

\end{DoxyCodeInclude}


Notice that we are listening only on 127.\+0.\+0.\+1, which is the internal loopback interface. If the server needs to listening on all of its ips, use 0.\+0.\+0.\+0 instead.

We also need to set event handlers to be called when we receive any data from the clients, when a new client connects to our server, or when a client disconnects. These callbacks are\+:


\begin{DoxyCodeInclude}

   \hyperlink{group__Ecore__Event__Group_gae75b0b1b2c50e73b8e42afc628b00a62}{ecore\_event\_handler\_add}(\hyperlink{group__Ecore__Con__Events__Group_ga2ccb9d84c4e5480a787e4672af3e353f}{ECORE\_CON\_EVENT\_CLIENT\_ADD}, (
      \hyperlink{group__Ecore__Event__Group_ga64b4df04cbafda481593d0bc29617c6d}{Ecore\_Event\_Handler\_Cb})\_add, NULL);
   \hyperlink{group__Ecore__Event__Group_gae75b0b1b2c50e73b8e42afc628b00a62}{ecore\_event\_handler\_add}(\hyperlink{group__Ecore__Con__Events__Group_ga0daddd73ab2ab92cbfd91f50ea344e12}{ECORE\_CON\_EVENT\_CLIENT\_DEL}, (
      \hyperlink{group__Ecore__Event__Group_ga64b4df04cbafda481593d0bc29617c6d}{Ecore\_Event\_Handler\_Cb})\_del, NULL);
   \hyperlink{group__Ecore__Event__Group_gae75b0b1b2c50e73b8e42afc628b00a62}{ecore\_event\_handler\_add}(\hyperlink{group__Ecore__Con__Events__Group_gafb9ce301001338915a45969e4d11d016}{ECORE\_CON\_EVENT\_CLIENT\_DATA}, (
      \hyperlink{group__Ecore__Event__Group_ga64b4df04cbafda481593d0bc29617c6d}{Ecore\_Event\_Handler\_Cb})\_data, NULL);

\end{DoxyCodeInclude}


More details about what these callbacks do will be given later.

Now, before running the main loop, we also want to set some limits to our server. To avoid it to be overloaded with too many connections to handle, we are going to set a maximum of 3 clients connected at the same time. This number is used just to demonstrate the A\+P\+I. A good number to be used here would need to be determined by tests done on the server, to check the load supported by it.

Any other client trying to connect to this server, after the limit is reached, will wait until one of the connected clients disconnect and the server accepts the new connection.

Another important thing to do is setting a timeout, to avoid that a client hold a connection for too long without doing anything. This timeout will disconnect the idle client, allowing that other clients that may be waiting to connect finally can do it.

Then we just start the main loop\+:


\begin{DoxyCodeInclude}

   \hyperlink{group__Ecore__Con__Server__Group_ga468eec82ada0234b6ea2b5b8d6e25407}{ecore\_con\_server\_timeout\_set}(svr, 10);
   \hyperlink{group__Ecore__Con__Server__Group_gabaef36c991be8922766611c75372ced4}{ecore\_con\_server\_client\_limit\_set}(svr, 3, 0);

   \hyperlink{group__Ecore__Main__Loop__Group_gaf103b9f668bb3e4fed12e52c6180132d}{ecore\_main\_loop\_begin}();

\end{DoxyCodeInclude}


After exiting the main loop, we print the list of connected clients, and also free the data associated with each respective client. This data was previously associated using \hyperlink{group__Ecore__Con__Client__Group_ga0c81c3b201d7fa6d48b6f5e99a651edc}{ecore\+\_\+con\+\_\+client\+\_\+data\+\_\+set()}\+:


\begin{DoxyCodeInclude}

   clients = ecore\_con\_server\_clients\_get(svr);
   printf(\textcolor{stringliteral}{"Clients connected to this server when exiting: %d\(\backslash\)n"},
          \hyperlink{group__Eina__List__Group_ga301729054a498a2df09a5f1dd760697e}{eina\_list\_count}(clients));
   \hyperlink{group__Eina__List__Group_gad02f746a08ee0b12b7ed66d4bc0d71d8}{EINA\_LIST\_FOREACH}(clients, l, cl)
     \{
        printf(\textcolor{stringliteral}{"%s\(\backslash\)n"}, \hyperlink{group__Ecore__Con__Client__Group_ga86b3797ad455971d370c0486e5b59854}{ecore\_con\_client\_ip\_get}(cl));
        free(\hyperlink{group__Ecore__Con__Client__Group_ga34e96711b04adecc9ade2b1f3408c390}{ecore\_con\_client\_data\_get}(cl));
     \}

\end{DoxyCodeInclude}


Then before exiting we show the total uptime of the server\+:


\begin{DoxyCodeInclude}

   printf(\textcolor{stringliteral}{"Server was up for %0.3f seconds\(\backslash\)n"},
          \hyperlink{group__Ecore__Con__Server__Group_ga1f7c1040e60e9e9a53a9a01f70aa52b3}{ecore\_con\_server\_uptime\_get}(svr));

\end{DoxyCodeInclude}


Now let's go back to the used callbacks.

The first callback, {\ttfamily \+\_\+add}, is registered to the event \hyperlink{group__Ecore__Con__Events__Group_ga2ccb9d84c4e5480a787e4672af3e353f}{E\+C\+O\+R\+E\+\_\+\+C\+O\+N\+\_\+\+E\+V\+E\+N\+T\+\_\+\+C\+L\+I\+E\+N\+T\+\_\+\+A\+D\+D}, which will be called whenever a client connects to the server.

This callback will associate a data structure to this client, that will be used to count how many bytes were received from it. It also prints some info about the client, and send a welcome string to it. \hyperlink{group__Ecore__Con__Client__Group_gaccf37688bb1ecbedc8791b5b43c1595c}{ecore\+\_\+con\+\_\+client\+\_\+flush()} is used to ensure that the string is sent immediately, instead of being buffered.

A timeout for idle specific for this client is also set, to demonstrate that it is independent of the general timeout of the server.

Before exiting, the callback will display a list of all clients still connected to this server. The code for this callback follows\+:

 
\begin{DoxyCodeInclude}
\hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool}
\_add(\textcolor{keywordtype}{void} *data EINA\_UNUSED, \textcolor{keywordtype}{int} type EINA\_UNUSED, \hyperlink{struct__Ecore__Con__Event__Client__Add}{Ecore\_Con\_Event\_Client\_Add} *ev
      )
\{
   \textcolor{keywordtype}{char} welcome[] = \textcolor{stringliteral}{"hello! - sent from the server"};
   Ecore\_Con\_Server *srv;
   Ecore\_Con\_Client *cl;
   \textcolor{keyword}{const} \hyperlink{struct__Eina__List}{Eina\_List} *clients, *l;

   \textcolor{keyword}{struct }\_Client *client = malloc(\textcolor{keyword}{sizeof}(*client));
   client->sdata = 0;

   printf(\textcolor{stringliteral}{"Client with ip %s, port %d, connected = %d!\(\backslash\)n"},
          \hyperlink{group__Ecore__Con__Client__Group_ga86b3797ad455971d370c0486e5b59854}{ecore\_con\_client\_ip\_get}(ev->\hyperlink{struct__Ecore__Con__Event__Client__Add_a9877727e79213077e8592dfca5895cbe}{client}),
          \hyperlink{group__Ecore__Con__Client__Group_ga555c9fd886a4833006888e685565ad03}{ecore\_con\_client\_port\_get}(ev->\hyperlink{struct__Ecore__Con__Event__Client__Add_a9877727e79213077e8592dfca5895cbe}{client}),
          \hyperlink{group__Ecore__Con__Client__Group_gaade0309ee7c315f7e54bc902078791e8}{ecore\_con\_client\_connected\_get}(ev->
      \hyperlink{struct__Ecore__Con__Event__Client__Add_a9877727e79213077e8592dfca5895cbe}{client}));

   \hyperlink{group__Ecore__Con__Client__Group_ga166fd469acaa809faa4247b12d79278f}{ecore\_con\_client\_send}(ev->\hyperlink{struct__Ecore__Con__Event__Client__Add_a9877727e79213077e8592dfca5895cbe}{client}, welcome, \textcolor{keyword}{sizeof}(welcome));
   \hyperlink{group__Ecore__Con__Client__Group_gaccf37688bb1ecbedc8791b5b43c1595c}{ecore\_con\_client\_flush}(ev->\hyperlink{struct__Ecore__Con__Event__Client__Add_a9877727e79213077e8592dfca5895cbe}{client});

   \hyperlink{group__Ecore__Con__Client__Group_gac96c76af4f6c9d842e52ea38312c73dd}{ecore\_con\_client\_timeout\_set}(ev->\hyperlink{struct__Ecore__Con__Event__Client__Add_a9877727e79213077e8592dfca5895cbe}{client}, 6);

   \hyperlink{group__Ecore__Con__Client__Group_ga0c81c3b201d7fa6d48b6f5e99a651edc}{ecore\_con\_client\_data\_set}(ev->\hyperlink{struct__Ecore__Con__Event__Client__Add_a9877727e79213077e8592dfca5895cbe}{client}, client);

   srv = ecore\_con\_client\_server\_get(ev->\hyperlink{struct__Ecore__Con__Event__Client__Add_a9877727e79213077e8592dfca5895cbe}{client});
   printf(\textcolor{stringliteral}{"Clients connected to this server:\(\backslash\)n"});
   clients = ecore\_con\_server\_clients\_get(srv);
   \hyperlink{group__Eina__List__Group_gad02f746a08ee0b12b7ed66d4bc0d71d8}{EINA\_LIST\_FOREACH}(clients, l, cl)
     printf("%s\(\backslash\)n", \hyperlink{group__Ecore__Con__Client__Group_ga86b3797ad455971d370c0486e5b59854}{ecore\_con\_client\_ip\_get}(cl));

   return \hyperlink{group__Ecore__Main__Loop__Group_ga1ee9db07f9a46a8b20fb83e1c6dbed09}{ECORE\_CALLBACK\_RENEW};
\}

\end{DoxyCodeInclude}


The second callback is {\ttfamily \+\_\+del}. It is associated with \hyperlink{group__Ecore__Con__Events__Group_ga0daddd73ab2ab92cbfd91f50ea344e12}{E\+C\+O\+R\+E\+\_\+\+C\+O\+N\+\_\+\+E\+V\+E\+N\+T\+\_\+\+C\+L\+I\+E\+N\+T\+\_\+\+D\+E\+L}, and is called whenever a client disconnects from this server.

It will just print some information about the client, free the associated data structure, and call \hyperlink{group__Ecore__Con__Client__Group_gaddd302cb689b5040363246a58f20a680}{ecore\+\_\+con\+\_\+client\+\_\+del()} on it before exiting the callback. Here's its code\+:


\begin{DoxyCodeInclude}

\hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool}
\_del(\textcolor{keywordtype}{void} *data EINA\_UNUSED, \textcolor{keywordtype}{int} type EINA\_UNUSED, \hyperlink{struct__Ecore__Con__Event__Client__Del}{Ecore\_Con\_Event\_Client\_Del} *ev
      )
\{
   \textcolor{keyword}{struct }\_Client *client;

   \textcolor{keywordflow}{if} (!ev->\hyperlink{struct__Ecore__Con__Event__Client__Del_a47278613bc4acaf1d9f3e8c217fd2035}{client})
     \textcolor{keywordflow}{return} \hyperlink{group__Ecore__Main__Loop__Group_ga1ee9db07f9a46a8b20fb83e1c6dbed09}{ECORE\_CALLBACK\_RENEW};

   client = \hyperlink{group__Ecore__Con__Client__Group_ga34e96711b04adecc9ade2b1f3408c390}{ecore\_con\_client\_data\_get}(ev->\hyperlink{struct__Ecore__Con__Event__Client__Del_a47278613bc4acaf1d9f3e8c217fd2035}{client});

   printf(\textcolor{stringliteral}{"Lost client with ip %s!\(\backslash\)n"}, \hyperlink{group__Ecore__Con__Client__Group_ga86b3797ad455971d370c0486e5b59854}{ecore\_con\_client\_ip\_get}(ev->
      \hyperlink{struct__Ecore__Con__Event__Client__Del_a47278613bc4acaf1d9f3e8c217fd2035}{client}));
   printf(\textcolor{stringliteral}{"Total data received from this client: %d\(\backslash\)n"}, client->sdata);
   printf(\textcolor{stringliteral}{"Client was connected for %0.3f seconds.\(\backslash\)n"},
          \hyperlink{group__Ecore__Con__Client__Group_ga6ce6fef375f4d5a0d28a513f2fc40023}{ecore\_con\_client\_uptime\_get}(ev->\hyperlink{struct__Ecore__Con__Event__Client__Del_a47278613bc4acaf1d9f3e8c217fd2035}{client}));

   \textcolor{keywordflow}{if} (client)
     free(client);

   \hyperlink{group__Ecore__Con__Client__Group_gaddd302cb689b5040363246a58f20a680}{ecore\_con\_client\_del}(ev->\hyperlink{struct__Ecore__Con__Event__Client__Del_a47278613bc4acaf1d9f3e8c217fd2035}{client});

   \textcolor{keywordflow}{return} \hyperlink{group__Ecore__Main__Loop__Group_ga1ee9db07f9a46a8b20fb83e1c6dbed09}{ECORE\_CALLBACK\_RENEW};
\}

\end{DoxyCodeInclude}


The last callback will print any data received by this server from its clients. It also increments the \char`\"{}bytes received\char`\"{} counter, sdata, in the data structure associated with this client. The callback code follows\+:


\begin{DoxyCodeInclude}

\hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool}
\_data(\textcolor{keywordtype}{void} *data EINA\_UNUSED, \textcolor{keywordtype}{int} type EINA\_UNUSED, \hyperlink{struct__Ecore__Con__Event__Client__Data}{Ecore\_Con\_Event\_Client\_Data} 
      *ev)
\{
   \textcolor{keywordtype}{char} fmt[128];
   \textcolor{keyword}{struct }\_Client *client = \hyperlink{group__Ecore__Con__Client__Group_ga34e96711b04adecc9ade2b1f3408c390}{ecore\_con\_client\_data\_get}(ev->
      \hyperlink{struct__Ecore__Con__Event__Client__Data_a2c3e5b340b67fde235d2da9814f3d413}{client});

   snprintf(fmt, \textcolor{keyword}{sizeof}(fmt),
            \textcolor{stringliteral}{"Received %i bytes from client %s port %d:\(\backslash\)n"}
            \textcolor{stringliteral}{">>>>>\(\backslash\)n"}
            \textcolor{stringliteral}{"%%.%is\(\backslash\)n"}
            \textcolor{stringliteral}{">>>>>\(\backslash\)n"},
            ev->\hyperlink{struct__Ecore__Con__Event__Client__Data_a6e87f88d094ec774fb22133a0632d8b3}{size}, \hyperlink{group__Ecore__Con__Client__Group_ga86b3797ad455971d370c0486e5b59854}{ecore\_con\_client\_ip\_get}(ev->
      \hyperlink{struct__Ecore__Con__Event__Client__Data_a2c3e5b340b67fde235d2da9814f3d413}{client}),
            \hyperlink{group__Ecore__Con__Client__Group_ga555c9fd886a4833006888e685565ad03}{ecore\_con\_client\_port\_get}(ev->\hyperlink{struct__Ecore__Con__Event__Client__Data_a2c3e5b340b67fde235d2da9814f3d413}{client}), ev->
      \hyperlink{struct__Ecore__Con__Event__Client__Data_a6e87f88d094ec774fb22133a0632d8b3}{size});

   printf(fmt, ev->\hyperlink{struct__Ecore__Con__Event__Client__Data_a1e2cdb017a18b0515d2102edc016b4c5}{data});

   client->sdata += ev->\hyperlink{struct__Ecore__Con__Event__Client__Data_a6e87f88d094ec774fb22133a0632d8b3}{size};

   \textcolor{keywordflow}{return} \hyperlink{group__Ecore__Main__Loop__Group_ga1ee9db07f9a46a8b20fb83e1c6dbed09}{ECORE\_CALLBACK\_RENEW};
\}

\end{DoxyCodeInclude}


The important parts of this example were described above. If you need to see the full source code for it, there's a link to the code in the beginning of this page.

This example will start a server and start accepting connections from clients, as demonstrated in the following diagram\+: 

 
\begin{DoxyImage}
\includegraphics{ecore_con-client-server-example}
\caption{width=}
\end{DoxyImage}


\begin{DoxyNote}{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 A\+P\+I usage. 
\end{DoxyNote}
