Go to the documentation of this file.00001
00008
00009
00010
00011
00012 #include <iostream>
00013 #include <cstdlib>
00014 #include <ecl/config/ecl.hpp>
00015 #include <ecl/errors/handlers.hpp>
00016 #include "../../include/ecl/io/poll.hpp"
00017 #include "../../include/ecl/io/socketpair.hpp"
00018 #ifndef ECL_IS_WIN32
00019 #include <unistd.h>
00020 #endif
00021
00022
00023
00024
00025
00026 #define DATA1 "In Xanadu, did Kublai Khan . . ."
00027 #define DATA2 "A stately pleasure dome decree . . ."
00028
00029
00030
00031
00032
00033 ecl::socket_descriptor socket_pair[2];
00034
00035
00036
00037
00038
00039 int socket_error() {
00040 #ifdef ECL_IS_WIN32
00041 return WSAGetLastError();
00042 #else
00043 return errno;
00044 #endif
00045 }
00046
00047 void sleep_one_sec() {
00048 #ifdef ECL_IS_WIN32
00049 Sleep(1000);
00050 #else
00051 sleep(1);
00052 #endif
00053 }
00054
00055
00056
00057
00058
00059 int main(int argc, char **argv) {
00060
00061 ecl::SocketError error = ecl::init_sockets();
00062 if ( error.flag() != ecl::NoError ) {
00063 std::cout << error.what() << std::endl;
00064 abort();
00065 }
00066
00067
00068 error = ecl::socketpair(socket_pair,true);
00069
00070 if ( error.flag() != ecl::NoError) {
00071 std::cout << error.what() << std::endl;
00072 std::cout << "SocketPair Error: " << socket_error() << std::endl;
00073 abort();
00074 }
00075 ecl::socket_pollfd pfd[2];
00076 pfd[0].fd = socket_pair[0];
00077 pfd[0].events = POLLIN;
00078 pfd[0].revents = 0;
00079 pfd[1].fd = socket_pair[1];
00080 pfd[1].events = POLLIN;
00081 pfd[1].revents = 0;
00082
00083 int poll_timeout = 500;
00084 int result = 0;
00085 unsigned int count = 0;
00086 while ( count < 10 ) {
00087 if((result = ecl::poll_sockets(pfd, 2, poll_timeout)) < 0) {
00088 std::cout << socket_error();
00089 } else if ( result > 0 ) {
00090 char receiving_buffer[256];
00091 int n;
00092 for ( unsigned int i = 0; i < 2; ++i ) {
00093 if ( pfd[i].revents == POLLIN ) {
00094 #ifdef ECL_IS_WIN32
00095 while ( ( n = ::recv(socket_pair[i], reinterpret_cast<char*>(receiving_buffer), 256, 0) ) > 0 ) {
00096 #else
00097 while( (n = read(socket_pair[i], receiving_buffer, 256)) > 0) {
00098 #endif
00099 std::cout << receiving_buffer << std::endl;
00100 if ( i == 0 ) {
00101 #ifdef ECL_IS_WIN32
00102 if( ::send(socket_pair[0], reinterpret_cast<const char*>(DATA1), sizeof(DATA1), 0) < 0 ) {
00103 #else
00104 if (write(socket_pair[0], DATA1, sizeof(DATA1)) < 0) {
00105 #endif
00106 std::cerr << "Failed to write to the socket." << std::endl;
00107 }
00108 } else {
00109 #ifdef ECL_IS_WIN32
00110 if( ::send(socket_pair[1], reinterpret_cast<const char*>(DATA2), sizeof(DATA2), 0) < 0 ) {
00111 #else
00112 if (write(socket_pair[1], DATA2, sizeof(DATA2)) < 0) {
00113 #endif
00114 std::cerr << "Failed to write to the socket." << std::endl;
00115 }
00116 }
00117 };
00118 pfd[i].revents = 0;
00119 }
00120 }
00121 sleep_one_sec();
00122 } else {
00123
00124 #ifdef ECL_IS_WIN32
00125 if( ::send(socket_pair[0], reinterpret_cast<const char*>(DATA1), sizeof(DATA1), 0) < 0 ) {
00126 #else
00127 if (write(socket_pair[0], DATA1, sizeof(DATA1)) < 0) {
00128 #endif
00129 std::cerr << "Failed to write to the socket." << std::endl;
00130 }
00131 sleep_one_sec();
00132 }
00133 ++count;
00134 }
00135
00136
00137 ecl::close_socket(socket_pair[0]);
00138 ecl::close_socket(socket_pair[1]);
00139
00140 return 0;
00141 }
00142
00143