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


ecl_io
Author(s): Daniel Stonier (d.stonier@gmail.com)
autogenerated on Thu Jan 2 2014 11:11:37