In this example we are going to create a server that listens for connections from clients through a TCP port.

You can get the full source code at \mbox{\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 \mbox{\hyperlink{group__Ecore__Con__Server__Group_ga8993f75b5a4948b6b3975575d807fb21}{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 ECORE\+\_\+\+CON\+\_\+\+REMOTE\+\_\+\+TCP (a TCP 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}{0}
\DoxyCodeLine{main(\textcolor{keywordtype}{void})}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Con__Server__Group_ga80f90f9716013d6104624c2e606f8915}{Ecore\_Con\_Server}}\ *svr;}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Con__Client__Group_gae9afe250f5420f4731d1a94050275756}{Ecore\_Con\_Client}}\ *cl;}
\DoxyCodeLine{\ \ \ \textcolor{keyword}{const}\ \mbox{\hyperlink{struct__Eina__List}{Eina\_List}}\ *clients,\ *l;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Eina__Main__Group_ga81b8137665b8a4d2cb38ca50017d4c74}{eina\_init}}();}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Init__Group_ga77757609684a2c922dc5ec398274751b}{ecore\_init}}();}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Con__Lib__Group_ga8a20f47372627a03e86a8705a6f8a82d}{ecore\_con\_init}}();}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{if}\ (!(svr\ =\ \mbox{\hyperlink{group__Ecore__Con__Server__Group_ga8993f75b5a4948b6b3975575d807fb21}{ecore\_con\_server\_add}}(ECORE\_CON\_REMOTE\_TCP,\ \textcolor{stringliteral}{"{}127.0.0.1"{}},\ 8080,\ NULL)))}
\DoxyCodeLine{\ \ \ \ \ 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}{0}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Event__Group_ga886c90c3f917f87b3b4c931adcbc41c5}{ecore\_event\_handler\_add}}(\mbox{\hyperlink{group__Ecore__Con__Events__Group_ga4757b5128fed6457910b996a65577111}{ECORE\_CON\_EVENT\_CLIENT\_ADD}},\ (\mbox{\hyperlink{group__Ecore__Event__Group_ga64b4df04cbafda481593d0bc29617c6d}{Ecore\_Event\_Handler\_Cb}})\_add,\ NULL);}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Event__Group_ga886c90c3f917f87b3b4c931adcbc41c5}{ecore\_event\_handler\_add}}(\mbox{\hyperlink{group__Ecore__Con__Events__Group_gab6a527ae1614876cddf4630ff03ae1dc}{ECORE\_CON\_EVENT\_CLIENT\_DEL}},\ (\mbox{\hyperlink{group__Ecore__Event__Group_ga64b4df04cbafda481593d0bc29617c6d}{Ecore\_Event\_Handler\_Cb}})\_del,\ NULL);}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Event__Group_ga886c90c3f917f87b3b4c931adcbc41c5}{ecore\_event\_handler\_add}}(\mbox{\hyperlink{group__Ecore__Con__Events__Group_gaf909435b89ab32078adc52f0a18c70c7}{ECORE\_CON\_EVENT\_CLIENT\_DATA}},\ (\mbox{\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 API. 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}{0}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Con__Server__Group_ga82e6c66eef006a627468f2ca4e619641}{ecore\_con\_server\_timeout\_set}}(svr,\ 10);}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Con__Server__Group_ga6188bbadcdc4dc2f1878dd3fe18f6fb8}{ecore\_con\_server\_client\_limit\_set}}(svr,\ 3,\ 0);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \mbox{\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 \mbox{\hyperlink{group__Ecore__Con__Client__Group_gabe69c02ac5c0ef03e322a5d34904f858}{ecore\+\_\+con\+\_\+client\+\_\+data\+\_\+set()}}\+:


\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ clients\ =\ \mbox{\hyperlink{group__Ecore__Con__Server__Group_ga688ddcc88a17c527a6ea772a66bf53d6}{ecore\_con\_server\_clients\_get}}(svr);}
\DoxyCodeLine{\ \ \ printf(\textcolor{stringliteral}{"{}Clients\ connected\ to\ this\ server\ when\ exiting:\ \%d\(\backslash\)n"{}},}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ eina\_list\_count(clients));}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Eina__List__Group_gac6efafc19f526d916cdb9757240a8773}{EINA\_LIST\_FOREACH}}(clients,\ l,\ cl)}
\DoxyCodeLine{\ \ \ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ printf(\textcolor{stringliteral}{"{}\%s\(\backslash\)n"{}},\ \mbox{\hyperlink{group__Ecore__Con__Client__Group_gab02f9ae8613aca1de53996239dd5f7ea}{ecore\_con\_client\_ip\_get}}(cl));}
\DoxyCodeLine{\ \ \ \ \ \ \ \ free(\mbox{\hyperlink{group__Ecore__Con__Client__Group_gadacc90bc2f169308e67a3efadfa49820}{ecore\_con\_client\_data\_get}}(cl));}
\DoxyCodeLine{\ \ \ \ \ \}}

\end{DoxyCodeInclude}
 Then before exiting we show the total uptime of the server\+:


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

\end{DoxyCodeInclude}
 Now let\textquotesingle{}s go back to the used callbacks.

The first callback, {\ttfamily \+\_\+add}, is registered to the event \mbox{\hyperlink{group__Ecore__Con__Events__Group_ga4757b5128fed6457910b996a65577111}{ECORE\+\_\+\+CON\+\_\+\+EVENT\+\_\+\+CLIENT\+\_\+\+ADD}}, 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. \mbox{\hyperlink{group__Ecore__Con__Client__Group_ga8eeeb449d9af80885a26309471330de9}{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}{0}
\DoxyCodeLine{\mbox{\hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool}}}
\DoxyCodeLine{\_add(\textcolor{keywordtype}{void}\ *data\ \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}},\ \textcolor{keywordtype}{int}\ type\ \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}},\ \mbox{\hyperlink{struct__Ecore__Con__Event__Client__Add}{Ecore\_Con\_Event\_Client\_Add}}\ *ev)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \textcolor{keywordtype}{char}\ welcome[]\ =\ \textcolor{stringliteral}{"{}hello!\ -\/\ sent\ from\ the\ server"{}};}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Con__Server__Group_ga80f90f9716013d6104624c2e606f8915}{Ecore\_Con\_Server}}\ *srv;}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Con__Client__Group_gae9afe250f5420f4731d1a94050275756}{Ecore\_Con\_Client}}\ *cl;}
\DoxyCodeLine{\ \ \ \textcolor{keyword}{const}\ \mbox{\hyperlink{struct__Eina__List}{Eina\_List}}\ *clients,\ *l;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keyword}{struct\ }\_Client\ *client\ =\ malloc(\textcolor{keyword}{sizeof}(*client));}
\DoxyCodeLine{\ \ \ client-\/>sdata\ =\ 0;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ printf(\textcolor{stringliteral}{"{}Client\ with\ ip\ \%s,\ port\ \%d,\ connected\ =\ \%d!\(\backslash\)n"{}},}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \mbox{\hyperlink{group__Ecore__Con__Client__Group_gab02f9ae8613aca1de53996239dd5f7ea}{ecore\_con\_client\_ip\_get}}(ev-\/>\mbox{\hyperlink{struct__Ecore__Con__Event__Client__Add_a9877727e79213077e8592dfca5895cbe}{client}}),}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \mbox{\hyperlink{group__Ecore__Con__Client__Group_ga8aac792bcc08647c91e346255723ecdc}{ecore\_con\_client\_port\_get}}(ev-\/>\mbox{\hyperlink{struct__Ecore__Con__Event__Client__Add_a9877727e79213077e8592dfca5895cbe}{client}}),}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \mbox{\hyperlink{group__Ecore__Con__Client__Group_ga9f668e92d167f19ed81d3e70c060219f}{ecore\_con\_client\_connected\_get}}(ev-\/>\mbox{\hyperlink{struct__Ecore__Con__Event__Client__Add_a9877727e79213077e8592dfca5895cbe}{client}}));}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Con__Client__Group_ga89f79f973aeea2505078d87cb7b045b3}{ecore\_con\_client\_send}}(ev-\/>\mbox{\hyperlink{struct__Ecore__Con__Event__Client__Add_a9877727e79213077e8592dfca5895cbe}{client}},\ welcome,\ \textcolor{keyword}{sizeof}(welcome));}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Con__Client__Group_ga8eeeb449d9af80885a26309471330de9}{ecore\_con\_client\_flush}}(ev-\/>\mbox{\hyperlink{struct__Ecore__Con__Event__Client__Add_a9877727e79213077e8592dfca5895cbe}{client}});}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Con__Client__Group_ga02e595c6d808c590822abd7c8e2109aa}{ecore\_con\_client\_timeout\_set}}(ev-\/>\mbox{\hyperlink{struct__Ecore__Con__Event__Client__Add_a9877727e79213077e8592dfca5895cbe}{client}},\ 6);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Con__Client__Group_gabe69c02ac5c0ef03e322a5d34904f858}{ecore\_con\_client\_data\_set}}(ev-\/>\mbox{\hyperlink{struct__Ecore__Con__Event__Client__Add_a9877727e79213077e8592dfca5895cbe}{client}},\ client);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ srv\ =\ \mbox{\hyperlink{group__Ecore__Con__Client__Group_ga70d6fe610ac62b88d14dc91ec5671bab}{ecore\_con\_client\_server\_get}}(ev-\/>\mbox{\hyperlink{struct__Ecore__Con__Event__Client__Add_a9877727e79213077e8592dfca5895cbe}{client}});}
\DoxyCodeLine{\ \ \ printf(\textcolor{stringliteral}{"{}Clients\ connected\ to\ this\ server:\(\backslash\)n"{}});}
\DoxyCodeLine{\ \ \ clients\ =\ \mbox{\hyperlink{group__Ecore__Con__Server__Group_ga688ddcc88a17c527a6ea772a66bf53d6}{ecore\_con\_server\_clients\_get}}(srv);}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Eina__List__Group_gac6efafc19f526d916cdb9757240a8773}{EINA\_LIST\_FOREACH}}(clients,\ l,\ cl)}
\DoxyCodeLine{\ \ \ \ \ printf(\textcolor{stringliteral}{"{}\%s\(\backslash\)n"{}},\ \mbox{\hyperlink{group__Ecore__Con__Client__Group_gab02f9ae8613aca1de53996239dd5f7ea}{ecore\_con\_client\_ip\_get}}(cl));}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{return}\ \mbox{\hyperlink{group__Ecore__Main__Loop__Group_ga1ee9db07f9a46a8b20fb83e1c6dbed09}{ECORE\_CALLBACK\_RENEW}};}
\DoxyCodeLine{\}}

\end{DoxyCodeInclude}
 The second callback is {\ttfamily \+\_\+del}. It is associated with \mbox{\hyperlink{group__Ecore__Con__Events__Group_gab6a527ae1614876cddf4630ff03ae1dc}{ECORE\+\_\+\+CON\+\_\+\+EVENT\+\_\+\+CLIENT\+\_\+\+DEL}}, 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 \mbox{\hyperlink{group__Ecore__Con__Client__Group_gab961e9decd8dc44e4c04f2ceb0ce2e68}{ecore\+\_\+con\+\_\+client\+\_\+del()}} on it before exiting the callback. Here\textquotesingle{}s its code\+:


\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{}
\DoxyCodeLine{\mbox{\hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool}}}
\DoxyCodeLine{\_del(\textcolor{keywordtype}{void}\ *data\ \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}},\ \textcolor{keywordtype}{int}\ type\ \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}},\ \mbox{\hyperlink{struct__Ecore__Con__Event__Client__Del}{Ecore\_Con\_Event\_Client\_Del}}\ *ev)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \textcolor{keyword}{struct\ }\_Client\ *client;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{if}\ (!ev-\/>\mbox{\hyperlink{struct__Ecore__Con__Event__Client__Del_a47278613bc4acaf1d9f3e8c217fd2035}{client}})}
\DoxyCodeLine{\ \ \ \ \ \textcolor{keywordflow}{return}\ \mbox{\hyperlink{group__Ecore__Main__Loop__Group_ga1ee9db07f9a46a8b20fb83e1c6dbed09}{ECORE\_CALLBACK\_RENEW}};}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ client\ =\ \mbox{\hyperlink{group__Ecore__Con__Client__Group_gadacc90bc2f169308e67a3efadfa49820}{ecore\_con\_client\_data\_get}}(ev-\/>\mbox{\hyperlink{struct__Ecore__Con__Event__Client__Del_a47278613bc4acaf1d9f3e8c217fd2035}{client}});}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ printf(\textcolor{stringliteral}{"{}Lost\ client\ with\ ip\ \%s!\(\backslash\)n"{}},\ \mbox{\hyperlink{group__Ecore__Con__Client__Group_gab02f9ae8613aca1de53996239dd5f7ea}{ecore\_con\_client\_ip\_get}}(ev-\/>\mbox{\hyperlink{struct__Ecore__Con__Event__Client__Del_a47278613bc4acaf1d9f3e8c217fd2035}{client}}));}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{if}\ (client)}
\DoxyCodeLine{\ \ \ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ printf(\textcolor{stringliteral}{"{}Total\ data\ received\ from\ this\ client:\ \%d\(\backslash\)n"{}},\ client-\/>sdata);}
\DoxyCodeLine{\ \ \ \ \ \ \ \ free(client);}
\DoxyCodeLine{\ \ \ \ \ \}}

\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}{0}
\DoxyCodeLine{\ \ \ printf(\textcolor{stringliteral}{"{}Client\ was\ connected\ for\ \%0.3f\ seconds.\(\backslash\)n"{}},}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \mbox{\hyperlink{group__Ecore__Con__Client__Group_gaa2a166c0e95b48ab214a5d5ea52e9cce}{ecore\_con\_client\_uptime\_get}}(ev-\/>\mbox{\hyperlink{struct__Ecore__Con__Event__Client__Del_a47278613bc4acaf1d9f3e8c217fd2035}{client}}));}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Con__Client__Group_gab961e9decd8dc44e4c04f2ceb0ce2e68}{ecore\_con\_client\_del}}(ev-\/>\mbox{\hyperlink{struct__Ecore__Con__Event__Client__Del_a47278613bc4acaf1d9f3e8c217fd2035}{client}});}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{return}\ \mbox{\hyperlink{group__Ecore__Main__Loop__Group_ga1ee9db07f9a46a8b20fb83e1c6dbed09}{ECORE\_CALLBACK\_RENEW}};}
\DoxyCodeLine{\}}

\end{DoxyCodeInclude}
 The important parts of this example were described above. If you need to see the full source code for it, there\textquotesingle{}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{DoxyImageNoCaption}
  \mbox{\includegraphics[width=\textwidth]{ecore_con-client-server-example}}
\end{DoxyImageNoCaption}


\begin{DoxyNote}{Note}
This example contains a serious security flaw\+: it doesn\textquotesingle{}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. 
\end{DoxyNote}
