\hypertarget{ecore_con_server_example_8c-example}{}\section{ecore\+\_\+con\+\_\+server\+\_\+example.\+c}
Shows how to write a simple server using the Ecore\+\_\+\+Con library.


\begin{DoxyCodeInclude}
\textcolor{comment}{//Compile with:}
\textcolor{comment}{// gcc -o ecore\_con\_server\_example ecore\_con\_server\_example.c `pkg-config --libs --cflags ecore ecore-con
       eina`}

\textcolor{preprocessor}{#ifdef HAVE\_CONFIG\_H}
\textcolor{preprocessor}{#include "config.h"}
\textcolor{preprocessor}{#endif}

\textcolor{preprocessor}{#include <stdio.h>}
\textcolor{preprocessor}{#include <Ecore.h>}
\textcolor{preprocessor}{#include <Ecore\_Con.h>}

\textcolor{comment}{/* Ecore\_Con server example}
\textcolor{comment}{ * 2010 Mike Blumenkrantz}
\textcolor{comment}{ */}

\textcolor{preprocessor}{#ifdef HAVE\_GNUTLS}
\textcolor{preprocessor}{#include <gnutls/gnutls.h>}

\textcolor{keyword}{static} \textcolor{keywordtype}{void}
tls\_log\_func(\textcolor{keywordtype}{int} level, \textcolor{keyword}{const} \textcolor{keywordtype}{char} *str)
\{
   fprintf(stderr, \textcolor{stringliteral}{"|<%d>| %s"}, level, str);
\}
\textcolor{preprocessor}{#endif}

\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
      )
\{
   printf(\textcolor{stringliteral}{"Client with ip %s connected!\(\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_ga166fd469acaa809faa4247b12d79278f}{ecore\_con\_client\_send}(ev->\hyperlink{struct__Ecore__Con__Event__Client__Add_a9877727e79213077e8592dfca5895cbe}{client}, \textcolor{stringliteral}{"hello!"}, 6);
\textcolor{comment}{//   ecore\_con\_client\_flush(ev->client);}
   \hyperlink{group__Ecore__Con__Client__Group_gac96c76af4f6c9d842e52ea38312c73dd}{ecore\_con\_client\_timeout\_set}(ev->\hyperlink{struct__Ecore__Con__Event__Client__Add_a9877727e79213077e8592dfca5895cbe}{client}, 5);

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

\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
      )
\{
   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}));
   \hyperlink{group__Ecore__Main__Loop__Group_ga95cf8e97dff0716433c2c5474d606a98}{ecore\_main\_loop\_quit}();
   \textcolor{keywordflow}{return} \hyperlink{group__Ecore__Main__Loop__Group_ga1ee9db07f9a46a8b20fb83e1c6dbed09}{ECORE\_CALLBACK\_RENEW};
\}

\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];

   snprintf(fmt, \textcolor{keyword}{sizeof}(fmt),
            \textcolor{stringliteral}{"Received %i bytes from client:\(\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}, ev->\hyperlink{struct__Ecore__Con__Event__Client__Data_a6e87f88d094ec774fb22133a0632d8b3}{size});

   printf(fmt, ev->\hyperlink{struct__Ecore__Con__Event__Client__Data_a1e2cdb017a18b0515d2102edc016b4c5}{data});
   \textcolor{keywordflow}{return} \hyperlink{group__Ecore__Main__Loop__Group_ga1ee9db07f9a46a8b20fb83e1c6dbed09}{ECORE\_CALLBACK\_RENEW};
\}

\textcolor{keywordtype}{int}
main()
\{
   Ecore\_Con\_Server *svr;
   \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{preprocessor}{#ifdef HAVE\_GNUTLS}
   gnutls\_global\_set\_log\_level(9);
   gnutls\_global\_set\_log\_function(tls\_log\_func);
\textcolor{preprocessor}{#endif}

\textcolor{comment}{/* to use a PEM certificate with TLS and SSL3, uncomment the lines below */}
   \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} | 
      \hyperlink{group__Ecore__Con__Lib__Group_gga4862bfa05383e6e16a1dfc6eafc90564a49f8b863d411675dbbc1e6e839144495}{ECORE\_CON\_USE\_TLS} | \hyperlink{group__Ecore__Con__Lib__Group_gga4862bfa05383e6e16a1dfc6eafc90564aaacab64bf751eaa6fc8b715502cee65b}{ECORE\_CON\_USE\_SSL3} | 
      \hyperlink{group__Ecore__Con__Lib__Group_gga4862bfa05383e6e16a1dfc6eafc90564ac39b40844a3d8da484c91d0ddbcc8ba9}{ECORE\_CON\_LOAD\_CERT}, \textcolor{stringliteral}{"127.0.0.1"}, 8080, NULL)))
\textcolor{comment}{/* to use simple tcp with ssl/tls, use this line */}
\textcolor{comment}{//   if (!ecore\_con\_server\_add(ECORE\_CON\_REMOTE\_TCP | ECORE\_CON\_USE\_SSL3, "127.0.0.1", 8080, NULL))}
     exit(1);

   \hyperlink{group__Ecore__Con__SSL__Group_ga3e2cc9dc19a76ed7d0cd69c08b2482d5}{ecore\_con\_ssl\_server\_cert\_add}(svr, \textcolor{stringliteral}{"server.pem"});
   \hyperlink{group__Ecore__Con__SSL__Group_ga336f50cd629b7bab5f45c2f13fe2d614}{ecore\_con\_ssl\_server\_privkey\_add}(svr, \textcolor{stringliteral}{"server.pem"});
\textcolor{comment}{/* set event handler for client connect */}
   \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);
\textcolor{comment}{/* set event handler for client disconnect */}
   \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);
\textcolor{comment}{/* set event handler for receiving client data */}
   \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);

\textcolor{comment}{/* start server */}
   \hyperlink{group__Ecore__Main__Loop__Group_gaf103b9f668bb3e4fed12e52c6180132d}{ecore\_main\_loop\_begin}();
\}

\end{DoxyCodeInclude}
 