socketpair.cpp
Go to the documentation of this file.
00001 
00009 /*****************************************************************************
00010 ** Includes
00011 *****************************************************************************/
00012 
00013 #include <iostream>
00014 #include <cstdlib>
00015 #include <ecl/config/ecl.hpp>
00016 #ifndef ECL_IS_WIN32
00017   #include <unistd.h>
00018 #endif
00019 #include <ecl/errors/handlers.hpp>
00020 #include "../../include/ecl/io/socketpair.hpp"
00021 
00022 /*****************************************************************************
00023 ** Macros
00024 *****************************************************************************/
00025 
00026 #define DATA1 "In Xanadu, did Kublai Khan . . ."
00027 #define DATA2 "A stately pleasure dome decree . . ."
00028 
00029 /*****************************************************************************
00030 ** Globals
00031 *****************************************************************************/
00032 
00033 ecl::socket_descriptor socket_pair[2];
00034 
00035 /*****************************************************************************
00036 ** Functions
00037 *****************************************************************************/
00038 
00039 #ifdef ECL_IS_WIN32
00040 DWORD WINAPI f(LPVOID args) {
00041         char buf[1024];
00042         /* This is the child. */
00043         while ( 1 ) {
00044                 if( ::send(socket_pair[1], reinterpret_cast<const char*>(DATA1), sizeof(DATA1), 0) < 0 ) {
00045                         std::cerr << "Failed to write to the threaded socket." << std::endl;
00046                 }
00047                 if ( ::recv(socket_pair[1], reinterpret_cast<char*>(buf), 1024, 0) < 0 ) {
00048                         std::cerr << "Failed to read from the threaded socket." << std::endl;
00049                 } else {
00050                         std::cout << buf << std::endl;
00051                 }
00052                 Sleep(1000);
00053         }
00054         return 0;
00055 }
00056 #endif
00057 
00058 int socket_error() {
00059 #ifdef ECL_IS_WIN32
00060         return WSAGetLastError();
00061 #else
00062         return errno;
00063 #endif
00064 }
00065 /*****************************************************************************
00066 ** Main
00067 *****************************************************************************/
00068 
00069 int main(int argc, char **argv) {
00070 
00071         ecl::SocketError error = ecl::init_sockets();
00072         if ( error.flag() != ecl::NoError ) {
00073                 std::cout << error.what() << std::endl;
00074                 abort();
00075         }
00076 
00077         // blocking mode
00078         error = ecl::socketpair(socket_pair);
00079         // non blocking mode
00080         // error = ecl::socketpair(socket_pair,true);
00081 
00082         if ( error.flag() != ecl::NoError) {
00083                 std::cout << error.what() << std::endl;
00084                 std::cout << "SocketPair Error: " << socket_error() << std::endl;
00085                 abort();
00086         }
00087         char buf[1024];
00088 #ifdef ECL_IS_WIN32
00089         DWORD id;
00090         char thread_param[3];
00091         HANDLE thread_handle = CreateThread(
00092             NULL,                   // default security attributes
00093             0,                      // use default stack size
00094             f,                                  // thread function name
00095             &thread_param,                      // argument to thread function
00096             0,                      // use default creation flags
00097             &id                                 // returns the thread identifier
00098      );
00099         /* This is the parent */
00100         while ( 1 ) {
00101                 if ( ::recv(socket_pair[0], reinterpret_cast<char*>(buf), 1024, 0) < 0 ) {
00102                         std::cerr << "Failed to read from the main socket." << std::endl;
00103                 } else {
00104                         std::cout << buf << std::endl;
00105                 }
00106                 if( ::send(socket_pair[0], reinterpret_cast<const char*>(DATA2), sizeof(DATA2), 0) < 0 ) {
00107                         std::cerr << "Failed to write to the main socket." << std::endl;
00108                 }
00109                 Sleep(1000);
00110         }
00111 #else
00112         int child;
00113         if ((child = fork()) == -1) {
00114                 std::cerr << "Failed to fork." << std::endl;
00115         } else if (child) {
00116                 /* This is the parent */
00117                 while ( 1 ) {
00118                         if (read(socket_pair[0], buf, 1024) < 0) {
00119                                 std::cerr << "Failed to read from the socket." << std::endl;
00120                         } else {
00121                                 std::cout << buf << std::endl;
00122                         }
00123                         if (write(socket_pair[0], DATA2, sizeof(DATA2)) < 0) {
00124                                 std::cerr << "Failed to write to the socket." << std::endl;
00125                         }
00126                         sleep(1);
00127                 }
00128         } else {
00129                 /* This is the child. */
00130                 while ( 1 ) {
00131                         if (write(socket_pair[1], DATA1, sizeof(DATA1)) < 0) {
00132                                 std::cerr << "Failed to write to the socket." << std::endl;
00133                         }
00134                         if (read(socket_pair[1], buf, 1024) < 0) {
00135                                 std::cerr << "Failed to read from the socket." << std::endl;
00136                         } else {
00137                                 std::cout << buf << std::endl;
00138                         }
00139                         sleep(1); // 1 sec
00140                 }
00141         }
00142 #endif
00143         ecl::close_socket(socket_pair[0]);
00144         ecl::close_socket(socket_pair[1]);
00145         return 0;
00146 }


ecl_io
Author(s): Daniel Stonier
autogenerated on Thu Jun 16 2016 09:47:15