Following the same idea as the \mbox{\hyperlink{ecore_con_server_simple_example_c}{Ecore\+\_\+\+Con -\/ Creating a server}} , this example will demonstrate how to create a client that connects to a specified server through a T\+CP port. You can see the full source code at \mbox{\hyperlink{ecore_con_client_simple_example_8c-example}{ecore\+\_\+con\+\_\+client\+\_\+simple\+\_\+example.\+c}}.

Starting from the {\ttfamily main} function, after reading the command line argument list and initializing the libraries, we try to connect to the server\+:

 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{main(\textcolor{keywordtype}{int} argc, \textcolor{keyword}{const} \textcolor{keywordtype}{char} *argv[])}
\DoxyCodeLine{\{}
\DoxyCodeLine{   \mbox{\hyperlink{group__Ecore__Con__Server__Group_ga80f90f9716013d6104624c2e606f8915}{Ecore\_Con\_Server}} *svr;}
\DoxyCodeLine{   \textcolor{keyword}{const} \textcolor{keywordtype}{char} *address;}
\DoxyCodeLine{   \textcolor{keywordtype}{int} port = 8080;}
\DoxyCodeLine{}
\DoxyCodeLine{   \textcolor{keywordflow}{if} (argc < 2)}
\DoxyCodeLine{     \{}
\DoxyCodeLine{        printf(\textcolor{stringliteral}{"wrong usage. Command syntax is:\(\backslash\)n"});}
\DoxyCodeLine{        printf(\textcolor{stringliteral}{"\(\backslash\)tecore\_con\_client\_simple\_example <address> [port]\(\backslash\)n"});}
\DoxyCodeLine{        exit(1);}
\DoxyCodeLine{     \}}
\DoxyCodeLine{}
\DoxyCodeLine{   address = argv[1];}
\DoxyCodeLine{}
\DoxyCodeLine{   \textcolor{keywordflow}{if} (argc > 2)}
\DoxyCodeLine{     port = atoi(argv[2]);}
\DoxyCodeLine{}
\DoxyCodeLine{   \mbox{\hyperlink{group__Eina__Main__Group_gafee4bfc0e509b6ffed6598edf6f4b3de}{eina\_init}}();}
\DoxyCodeLine{   \mbox{\hyperlink{group__Ecore__Init__Group_ga77757609684a2c922dc5ec398274751b}{ecore\_init}}();}
\DoxyCodeLine{   \mbox{\hyperlink{group__Ecore__Con__Lib__Group_gacf5ecf42c516bb9c8b6759721e9559bd}{ecore\_con\_init}}();}
\DoxyCodeLine{}
\DoxyCodeLine{   \textcolor{keywordflow}{if} (!(svr = \mbox{\hyperlink{group__Ecore__Con__Server__Group_gae3d7391f7da5e0f748d7b6804d010b2f}{ecore\_con\_server\_connect}}(ECORE\_CON\_REMOTE\_TCP, address, port, NULL)))}
\DoxyCodeLine{     \{}
\DoxyCodeLine{        printf(\textcolor{stringliteral}{"could not connect to the server: \%s, port \%d.\(\backslash\)n"},}
\DoxyCodeLine{               address, port);}
\DoxyCodeLine{        exit(2);}
\DoxyCodeLine{     \}}

\end{DoxyCodeInclude}


After doing this, everything else in {\ttfamily main} is setting up callbacks for the client events, starting the main loop and shutting down the libraries after it.

Now let\textquotesingle{}s go to the callbacks. These callbacks are very similar to the server callbacks (our implementation for this example is very simple). On the {\ttfamily \+\_\+add} callback, we just set a data structure to the server, print some information about the server, and send a welcome message to it\+:

 
\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__Server__Add}{Ecore\_Con\_Event\_Server\_Add}} *ev)}
\DoxyCodeLine{\{}
\DoxyCodeLine{   \textcolor{keywordtype}{char} welcome[] = \textcolor{stringliteral}{"hello! -\/ sent from the client"};}
\DoxyCodeLine{   \textcolor{keyword}{struct }\_Server *server = malloc(\textcolor{keyword}{sizeof}(*server));}
\DoxyCodeLine{   server-\/>sdata = 0;}
\DoxyCodeLine{}
\DoxyCodeLine{   \mbox{\hyperlink{group__Ecore__Con__Server__Group_ga09ee7d7908fb1588740b19ba22d7c29d}{ecore\_con\_server\_data\_set}}(ev-\/>\mbox{\hyperlink{struct__Ecore__Con__Event__Server__Add_a382a9545ed7fb941a30dc4d598dc7e4d}{server}}, server);}
\DoxyCodeLine{   printf(\textcolor{stringliteral}{"Server with ip \%s, name \%s, port \%d, connected = \%d!\(\backslash\)n"},}
\DoxyCodeLine{          \mbox{\hyperlink{group__Ecore__Con__Server__Group_ga03369030d39ae060350751abd6833c11}{ecore\_con\_server\_ip\_get}}(ev-\/>\mbox{\hyperlink{struct__Ecore__Con__Event__Server__Add_a382a9545ed7fb941a30dc4d598dc7e4d}{server}}),}
\DoxyCodeLine{          \mbox{\hyperlink{group__Ecore__Con__Server__Group_ga1a69bf6d7eb18a74587f5f6636bf8de0}{ecore\_con\_server\_name\_get}}(ev-\/>\mbox{\hyperlink{struct__Ecore__Con__Event__Server__Add_a382a9545ed7fb941a30dc4d598dc7e4d}{server}}),}
\DoxyCodeLine{          \mbox{\hyperlink{group__Ecore__Con__Server__Group_gac21ef0f2db46b32042160b83534138f9}{ecore\_con\_server\_port\_get}}(ev-\/>\mbox{\hyperlink{struct__Ecore__Con__Event__Server__Add_a382a9545ed7fb941a30dc4d598dc7e4d}{server}}),}
\DoxyCodeLine{          \mbox{\hyperlink{group__Ecore__Con__Server__Group_ga68bd9a81d91669aa6c85e41460acb100}{ecore\_con\_server\_connected\_get}}(ev-\/>\mbox{\hyperlink{struct__Ecore__Con__Event__Server__Add_a382a9545ed7fb941a30dc4d598dc7e4d}{server}}));}
\DoxyCodeLine{   \mbox{\hyperlink{group__Ecore__Con__Server__Group_gadf16abc2d4fc16a8b65709eab2cd8337}{ecore\_con\_server\_send}}(ev-\/>\mbox{\hyperlink{struct__Ecore__Con__Event__Server__Add_a382a9545ed7fb941a30dc4d598dc7e4d}{server}}, welcome, \textcolor{keyword}{sizeof}(welcome));}
\DoxyCodeLine{   \mbox{\hyperlink{group__Ecore__Con__Server__Group_ga3ddb6c88484b59a1f5a5bacb8b6e60fe}{ecore\_con\_server\_flush}}(ev-\/>\mbox{\hyperlink{struct__Ecore__Con__Event__Server__Add_a382a9545ed7fb941a30dc4d598dc7e4d}{server}});}
\DoxyCodeLine{}
\DoxyCodeLine{   \textcolor{keywordflow}{return} \mbox{\hyperlink{group__Ecore__Main__Loop__Group_ga1ee9db07f9a46a8b20fb83e1c6dbed09}{ECORE\_CALLBACK\_RENEW}};}
\DoxyCodeLine{\}}

\end{DoxyCodeInclude}


The {\ttfamily \+\_\+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\textquotesingle{}s nothing to do once we disconnect)\+:


\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__Server__Del}{Ecore\_Con\_Event\_Server\_Del}} *ev)}
\DoxyCodeLine{\{}
\DoxyCodeLine{   \textcolor{keywordflow}{if} (!ev-\/>\mbox{\hyperlink{struct__Ecore__Con__Event__Server__Del_a116963b404600e7154c61f94ebf52a2e}{server}})}
\DoxyCodeLine{     \{}
\DoxyCodeLine{        printf(\textcolor{stringliteral}{"Failed to establish connection to the server.\(\backslash\)nExiting.\(\backslash\)n"});}
\DoxyCodeLine{        \mbox{\hyperlink{group__Ecore__Main__Loop__Group_ga95cf8e97dff0716433c2c5474d606a98}{ecore\_main\_loop\_quit}}();}
\DoxyCodeLine{        \textcolor{keywordflow}{return} \mbox{\hyperlink{group__Ecore__Main__Loop__Group_ga1ee9db07f9a46a8b20fb83e1c6dbed09}{ECORE\_CALLBACK\_RENEW}};}
\DoxyCodeLine{     \}}

\end{DoxyCodeInclude}


The {\ttfamily \+\_\+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\+:


\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\mbox{\hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool}}}
\DoxyCodeLine{\_data(\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__Server__Data}{Ecore\_Con\_Event\_Server\_Data}} *ev)}
\DoxyCodeLine{\{}
\DoxyCodeLine{   \textcolor{keywordtype}{char} fmt[128];}
\DoxyCodeLine{   \textcolor{keyword}{struct }\_Server *server = \mbox{\hyperlink{group__Ecore__Con__Server__Group_ga0a03ab73a105ad9a17265966f5683625}{ecore\_con\_server\_data\_get}}(ev-\/>\mbox{\hyperlink{struct__Ecore__Con__Event__Server__Data_a632e6c0033302088e26479c4fa17e0c0}{server}});}
\DoxyCodeLine{}
\DoxyCodeLine{   snprintf(fmt, \textcolor{keyword}{sizeof}(fmt),}
\DoxyCodeLine{            \textcolor{stringliteral}{"Received \%i bytes from server:\(\backslash\)n"}}
\DoxyCodeLine{            \textcolor{stringliteral}{">>>>>\(\backslash\)n"}}
\DoxyCodeLine{            \textcolor{stringliteral}{"\%\%.\%is\(\backslash\)n"}}
\DoxyCodeLine{            \textcolor{stringliteral}{">>>>>\(\backslash\)n"},}
\DoxyCodeLine{            ev-\/>\mbox{\hyperlink{struct__Ecore__Con__Event__Server__Data_a41877d1f1cb9792508b9b1d89d53bec2}{size}}, ev-\/>\mbox{\hyperlink{struct__Ecore__Con__Event__Server__Data_a41877d1f1cb9792508b9b1d89d53bec2}{size}});}
\DoxyCodeLine{}
\DoxyCodeLine{   printf(fmt, ev-\/>\mbox{\hyperlink{struct__Ecore__Con__Event__Server__Data_a8d45b6d75c55b26771d241211a0d7e7e}{data}});}
\DoxyCodeLine{}
\DoxyCodeLine{   server-\/>sdata += ev-\/>\mbox{\hyperlink{struct__Ecore__Con__Event__Server__Data_a41877d1f1cb9792508b9b1d89d53bec2}{size}};}
\DoxyCodeLine{   \textcolor{keywordflow}{return} \mbox{\hyperlink{group__Ecore__Main__Loop__Group_ga1ee9db07f9a46a8b20fb83e1c6dbed09}{ECORE\_CALLBACK\_RENEW}};}
\DoxyCodeLine{\}}

\end{DoxyCodeInclude}


You can see the server counterpart functions of the ones used in this example in the \mbox{\hyperlink{ecore_con_server_simple_example_c}{Ecore\+\_\+\+Con -\/ Creating a server}}.

This example will connect to the server and start comunicating with it, as demonstrated in the following diagram\+: 

 
\begin{DoxyImageNoCaption}
  \mbox{\includegraphics[width=\textwidth]{ecore_con-client-server-example2}}
\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 A\+PI usage. 
\end{DoxyNote}
