client_code.tex 1.79 KB
Ejemplo de aplicación cliente


\begin{DoxyCode}
\textcolor{preprocessor}{#include "\hyperlink{_socket_8h}{Socket.h}"}
\textcolor{preprocessor}{#include <iostream>}
\textcolor{preprocessor}{#include "\hyperlink{_socket_exception_8h}{SocketException.h}"}
\textcolor{preprocessor}{#include <sstream>}
\textcolor{preprocessor}{#include <signal.h>}
\textcolor{preprocessor}{#include <cstdlib>}
\textcolor{preprocessor}{#include <sys/time.h>}

\textcolor{keyword}{using namespace }std;

\textcolor{keywordtype}{void} exitClient(\textcolor{keywordtype}{int} signal)
\{
        cout << \textcolor{stringliteral}{"Server connection terminated unexpectedly"} << endl << \textcolor{stringliteral}{"Exiting"} << endl;
        exit(-1);
\}

\textcolor{keywordtype}{int} \hyperlink{client_8cpp_ae66f6b31b5ad750f1fe042a706a4e3d4}{main}()
\{
        signal(SIGPIPE, exitClient);
        signal(SIGINT, exitClient);
        \hyperlink{class_socket}{Socket} s;
        \textcolor{keywordtype}{string} send, recv, host;
        \textcolor{keywordtype}{int} port;
        s.\hyperlink{class_socket_add736ccf41f102910a2b8ac5e77eb8b1}{Create}();
        cout << \textcolor{stringliteral}{"Created socket"} << endl;
        cout << \textcolor{stringliteral}{"Hostname: "};
        cin >> host;
        cout << \textcolor{stringliteral}{"Port: "};
        cin >> port;
        cin.ignore();
        s.\hyperlink{class_socket_aa1d074c1aa400e8bca7c7da79b036abe}{Connect}(host,port);
        cout << \textcolor{stringliteral}{"Connected"} << endl;
        \textcolor{keywordflow}{while}(\textcolor{keyword}{true})
        \{
                cout << \textcolor{stringliteral}{"> "};
                getline(cin,send);
                s >> recv;
                cout << \textcolor{stringliteral}{"Received: "} << recv << endl;
        \}
\}
\end{DoxyCode}