Go to the documentation of this file.00001
00009
00010
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
00021
00022
00023 #define DATA1 "In Xanadu, did Kublai Khan . . ."
00024 #define DATA2 "A stately pleasure dome decree . . ."
00025
00026
00027
00028
00029
00030 ecl::socket_descriptor socket_pair[2];
00031
00032
00033
00034
00035
00036 #ifdef ECL_IS_WIN32
00037 DWORD WINAPI f(LPVOID args) {
00038 char buf[1024];
00039
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
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
00075 error = ecl::socketpair(socket_pair);
00076
00077
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,
00090 0,
00091 f,
00092 &thread_param,
00093 0,
00094 &id
00095 );
00096
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
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
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);
00137 }
00138 }
00139 #endif
00140 ecl::close_socket(socket_pair[0]);
00141 ecl::close_socket(socket_pair[1]);
00142 return 0;
00143 }