\hypertarget{ecore_con_client_simple_example_8c-example}{}\section{ecore\+\_\+con\+\_\+client\+\_\+simple\+\_\+example.\+c}
Shows how to setup a simple client that connects to a server and sends a \char`\"{}hello\char`\"{} string to it. See the complete example description at \mbox{\hyperlink{ecore_con_client_simple_example_c}{Ecore\+\_\+\+Con -\/ Creating a client}}


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

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

\textcolor{keyword}{struct }\_Server
\{
   \textcolor{keywordtype}{int} sdata;
\};

\mbox{\hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool}}
\_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)
\{
   \textcolor{keywordtype}{char} welcome[] = \textcolor{stringliteral}{"hello! - sent from the client"};
   \textcolor{keyword}{struct }\_Server *server = malloc(\textcolor{keyword}{sizeof}(*server));
   server->sdata = 0;

   \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);
   printf(\textcolor{stringliteral}{"Server with ip %s, name %s, port %d, connected = %d!\(\backslash\)n"},
          \mbox{\hyperlink{group__Ecore__Con__Server__Group_gaea2a907ae4ca478a360928fd16654d45}{ecore\_con\_server\_ip\_get}}(ev->\mbox{\hyperlink{struct__Ecore__Con__Event__Server__Add_a382a9545ed7fb941a30dc4d598dc7e4d}{server}}),
          \mbox{\hyperlink{group__Ecore__Con__Server__Group_ga8fd712100ae1842b58695e54f78f0e78}{ecore\_con\_server\_name\_get}}(ev->\mbox{\hyperlink{struct__Ecore__Con__Event__Server__Add_a382a9545ed7fb941a30dc4d598dc7e4d}{server}}),
          \mbox{\hyperlink{group__Ecore__Con__Server__Group_gac21ef0f2db46b32042160b83534138f9}{ecore\_con\_server\_port\_get}}(ev->\mbox{\hyperlink{struct__Ecore__Con__Event__Server__Add_a382a9545ed7fb941a30dc4d598dc7e4d}{server}}),
          \mbox{\hyperlink{group__Ecore__Con__Server__Group_ga68bd9a81d91669aa6c85e41460acb100}{ecore\_con\_server\_connected\_get}}(ev->
      \mbox{\hyperlink{struct__Ecore__Con__Event__Server__Add_a382a9545ed7fb941a30dc4d598dc7e4d}{server}}));
   \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));
   \mbox{\hyperlink{group__Ecore__Con__Server__Group_ga3ddb6c88484b59a1f5a5bacb8b6e60fe}{ecore\_con\_server\_flush}}(ev->\mbox{\hyperlink{struct__Ecore__Con__Event__Server__Add_a382a9545ed7fb941a30dc4d598dc7e4d}{server}});

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

\mbox{\hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool}}
\_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)
\{
   \textcolor{keywordflow}{if} (!ev->\mbox{\hyperlink{struct__Ecore__Con__Event__Server__Del_a116963b404600e7154c61f94ebf52a2e}{server}})
     \{
        printf(\textcolor{stringliteral}{"Failed to establish connection to the server.\(\backslash\)nExiting.\(\backslash\)n"});
        \mbox{\hyperlink{group__Ecore__Main__Loop__Group_ga95cf8e97dff0716433c2c5474d606a98}{ecore\_main\_loop\_quit}}();
        \textcolor{keywordflow}{return} \mbox{\hyperlink{group__Ecore__Main__Loop__Group_ga1ee9db07f9a46a8b20fb83e1c6dbed09}{ECORE\_CALLBACK\_RENEW}};
     \}

   \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__Del_a116963b404600e7154c61f94ebf52a2e}{server}});

   printf(\textcolor{stringliteral}{"Lost server with ip %s!\(\backslash\)n"}, \mbox{\hyperlink{group__Ecore__Con__Server__Group_gaea2a907ae4ca478a360928fd16654d45}{ecore\_con\_server\_ip\_get}}(ev->
      \mbox{\hyperlink{struct__Ecore__Con__Event__Server__Del_a116963b404600e7154c61f94ebf52a2e}{server}}));

   \textcolor{keywordflow}{if} (server)
     \{
        printf(\textcolor{stringliteral}{"Total data received from this server: %d\(\backslash\)n"}, server->sdata);
        free(server);
     \}

   \mbox{\hyperlink{group__Ecore__Con__Server__Group_ga79c76b9f725ceda216e9c7e5caf6cbd9}{ecore\_con\_server\_del}}(ev->\mbox{\hyperlink{struct__Ecore__Con__Event__Server__Del_a116963b404600e7154c61f94ebf52a2e}{server}});

   \mbox{\hyperlink{group__Ecore__Main__Loop__Group_ga95cf8e97dff0716433c2c5474d606a98}{ecore\_main\_loop\_quit}}();
   \textcolor{keywordflow}{return} \mbox{\hyperlink{group__Ecore__Main__Loop__Group_ga1ee9db07f9a46a8b20fb83e1c6dbed09}{ECORE\_CALLBACK\_RENEW}};
\}

\mbox{\hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool}}
\_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)
\{
   \textcolor{keywordtype}{char} fmt[128];
   \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}});

   snprintf(fmt, \textcolor{keyword}{sizeof}(fmt),
            \textcolor{stringliteral}{"Received %i bytes from server:\(\backslash\)n"}
            \textcolor{stringliteral}{">>>>>\(\backslash\)n"}
            \textcolor{stringliteral}{"%%.%is\(\backslash\)n"}
            \textcolor{stringliteral}{">>>>>\(\backslash\)n"},
            ev->\mbox{\hyperlink{struct__Ecore__Con__Event__Server__Data_a41877d1f1cb9792508b9b1d89d53bec2}{size}}, ev->\mbox{\hyperlink{struct__Ecore__Con__Event__Server__Data_a41877d1f1cb9792508b9b1d89d53bec2}{size}});

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

   server->sdata += ev->\mbox{\hyperlink{struct__Ecore__Con__Event__Server__Data_a41877d1f1cb9792508b9b1d89d53bec2}{size}};
   \textcolor{keywordflow}{return} \mbox{\hyperlink{group__Ecore__Main__Loop__Group_ga1ee9db07f9a46a8b20fb83e1c6dbed09}{ECORE\_CALLBACK\_RENEW}};
\}

\textcolor{keywordtype}{int}
main(\textcolor{keywordtype}{int} argc, \textcolor{keyword}{const} \textcolor{keywordtype}{char} *argv[])
\{
   \mbox{\hyperlink{group__Ecore__Con__Server__Group_ga80f90f9716013d6104624c2e606f8915}{Ecore\_Con\_Server}} *svr;
   \textcolor{keyword}{const} \textcolor{keywordtype}{char} *address;
   \textcolor{keywordtype}{int} port = 8080;

   \textcolor{keywordflow}{if} (argc < 2)
     \{
        printf(\textcolor{stringliteral}{"wrong usage. Command syntax is:\(\backslash\)n"});
        printf(\textcolor{stringliteral}{"\(\backslash\)tecore\_con\_client\_simple\_example <address> [port]\(\backslash\)n"});
        exit(1);
     \}

   address = argv[1];

   \textcolor{keywordflow}{if} (argc > 2)
     port = atoi(argv[2]);

   \mbox{\hyperlink{group__Eina__Main__Group_gafee4bfc0e509b6ffed6598edf6f4b3de}{eina\_init}}();
   \mbox{\hyperlink{group__Ecore__Init__Group_ga77757609684a2c922dc5ec398274751b}{ecore\_init}}();
   \mbox{\hyperlink{group__Ecore__Con__Lib__Group_gacf5ecf42c516bb9c8b6759721e9559bd}{ecore\_con\_init}}();

   \textcolor{keywordflow}{if} (!(svr = \mbox{\hyperlink{group__Ecore__Con__Server__Group_gae3d7391f7da5e0f748d7b6804d010b2f}{ecore\_con\_server\_connect}}(ECORE\_CON\_REMOTE\_TCP, address, port, NULL))
      )
     \{
        printf(\textcolor{stringliteral}{"could not connect to the server: %s, port %d.\(\backslash\)n"},
               address, port);
        exit(2);
     \}

   \textcolor{comment}{/* set event handler for server connect */}
   \mbox{\hyperlink{group__Ecore__Event__Group_gae75b0b1b2c50e73b8e42afc628b00a62}{ecore\_event\_handler\_add}}(\mbox{\hyperlink{group__Ecore__Con__Events__Group_ga4eed60012259c24f9ac75852c5ec1950}{ECORE\_CON\_EVENT\_SERVER\_ADD}}, (
      \mbox{\hyperlink{group__Ecore__Event__Group_ga64b4df04cbafda481593d0bc29617c6d}{Ecore\_Event\_Handler\_Cb}})\_add, NULL);
   \textcolor{comment}{/* set event handler for server disconnect */}
   \mbox{\hyperlink{group__Ecore__Event__Group_gae75b0b1b2c50e73b8e42afc628b00a62}{ecore\_event\_handler\_add}}(\mbox{\hyperlink{group__Ecore__Con__Events__Group_gabf1abac1d209506c2813837e36dfe37b}{ECORE\_CON\_EVENT\_SERVER\_DEL}}, (
      \mbox{\hyperlink{group__Ecore__Event__Group_ga64b4df04cbafda481593d0bc29617c6d}{Ecore\_Event\_Handler\_Cb}})\_del, NULL);
   \textcolor{comment}{/* set event handler for receiving server data */}
   \mbox{\hyperlink{group__Ecore__Event__Group_gae75b0b1b2c50e73b8e42afc628b00a62}{ecore\_event\_handler\_add}}(\mbox{\hyperlink{group__Ecore__Con__Events__Group_ga4bd54e9f3c04f7d601bd15b2e109f050}{ECORE\_CON\_EVENT\_SERVER\_DATA}}, (
      \mbox{\hyperlink{group__Ecore__Event__Group_ga64b4df04cbafda481593d0bc29617c6d}{Ecore\_Event\_Handler\_Cb}})\_data, NULL);

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

   \mbox{\hyperlink{group__Ecore__Con__Lib__Group_gaccfd8dd2a1187b71339b5cc39dc83938}{ecore\_con\_shutdown}}();
   \mbox{\hyperlink{group__Ecore__Init__Group_ga768298b932f18d7e7593a447493e5cde}{ecore\_shutdown}}();
   \mbox{\hyperlink{group__Eina__Main__Group_ga2c8b4020427108a30c44707af22c99d3}{eina\_shutdown}}();

   \textcolor{keywordflow}{return} 0;
\}

\end{DoxyCodeInclude}
 