examples/socketpair.cpp
Go to the documentation of this file.
1 
9 /*****************************************************************************
10 ** Includes
11 *****************************************************************************/
12 
13 #include <iostream>
14 #include <cstdlib>
15 #include <ecl/config/ecl.hpp>
16 #ifndef ECL_IS_WIN32
17  #include <unistd.h>
18 #endif
19 #include <ecl/errors/handlers.hpp>
20 #include "../../include/ecl/io/socketpair.hpp"
21 
22 /*****************************************************************************
23 ** Macros
24 *****************************************************************************/
25 
26 #define DATA1 "In Xanadu, did Kublai Khan . . ."
27 #define DATA2 "A stately pleasure dome decree . . ."
28 
29 /*****************************************************************************
30 ** Globals
31 *****************************************************************************/
32 
34 
35 /*****************************************************************************
36 ** Functions
37 *****************************************************************************/
38 
39 #ifdef ECL_IS_WIN32
40 DWORD WINAPI f(LPVOID args) {
41  char buf[1024];
42  /* This is the child. */
43  while ( 1 ) {
44  if( ::send(socket_pair[1], reinterpret_cast<const char*>(DATA1), sizeof(DATA1), 0) < 0 ) {
45  std::cerr << "Failed to write to the threaded socket." << std::endl;
46  }
47  if ( ::recv(socket_pair[1], reinterpret_cast<char*>(buf), 1024, 0) < 0 ) {
48  std::cerr << "Failed to read from the threaded socket." << std::endl;
49  } else {
50  std::cout << buf << std::endl;
51  }
52  Sleep(1000);
53  }
54  return 0;
55 }
56 #endif
57 
58 int socket_error() {
59 #ifdef ECL_IS_WIN32
60  return WSAGetLastError();
61 #else
62  return errno;
63 #endif
64 }
65 /*****************************************************************************
66 ** Main
67 *****************************************************************************/
68 
69 int main(int argc, char **argv) {
70 
72  if ( error.flag() != ecl::NoError ) {
73  std::cout << error.what() << std::endl;
74  abort();
75  }
76 
77  // blocking mode
79  // non blocking mode
80  // error = ecl::socketpair(socket_pair,true);
81 
82  if ( error.flag() != ecl::NoError) {
83  std::cout << error.what() << std::endl;
84  std::cout << "SocketPair Error: " << socket_error() << std::endl;
85  abort();
86  }
87  char buf[1024];
88 #ifdef ECL_IS_WIN32
89  DWORD id;
90  char thread_param[3];
91  HANDLE thread_handle = CreateThread(
92  NULL, // default security attributes
93  0, // use default stack size
94  f, // thread function name
95  &thread_param, // argument to thread function
96  0, // use default creation flags
97  &id // returns the thread identifier
98  );
99  /* This is the parent */
100  while ( 1 ) {
101  if ( ::recv(socket_pair[0], reinterpret_cast<char*>(buf), 1024, 0) < 0 ) {
102  std::cerr << "Failed to read from the main socket." << std::endl;
103  } else {
104  std::cout << buf << std::endl;
105  }
106  if( ::send(socket_pair[0], reinterpret_cast<const char*>(DATA2), sizeof(DATA2), 0) < 0 ) {
107  std::cerr << "Failed to write to the main socket." << std::endl;
108  }
109  Sleep(1000);
110  }
111 #else
112  int child;
113  if ((child = fork()) == -1) {
114  std::cerr << "Failed to fork." << std::endl;
115  } else if (child) {
116  /* This is the parent */
117  while ( 1 ) {
118  if (read(socket_pair[0], buf, 1024) < 0) {
119  std::cerr << "Failed to read from the socket." << std::endl;
120  } else {
121  std::cout << buf << std::endl;
122  }
123  if (write(socket_pair[0], DATA2, sizeof(DATA2)) < 0) {
124  std::cerr << "Failed to write to the socket." << std::endl;
125  }
126  sleep(1);
127  }
128  } else {
129  /* This is the child. */
130  while ( 1 ) {
131  if (write(socket_pair[1], DATA1, sizeof(DATA1)) < 0) {
132  std::cerr << "Failed to write to the socket." << std::endl;
133  }
134  if (read(socket_pair[1], buf, 1024) < 0) {
135  std::cerr << "Failed to read from the socket." << std::endl;
136  } else {
137  std::cout << buf << std::endl;
138  }
139  sleep(1); // 1 sec
140  }
141  }
142 #endif
145  return 0;
146 }
virtual ErrorFlag flag() const
virtual const char * what() const
ecl_io_PUBLIC SocketError socketpair(socket_descriptor socket_fd_pair[2], const bool non_blocking=false)
Creates a socket pair internal to the current process.
int socket_error()
int main(int argc, char **argv)
#define DATA2
int socket_descriptor
Cross-platform typedef for a socket file descriptor.
Definition: sockets.hpp:58
ecl_io_PUBLIC SocketError init_sockets()
Initialise the socket subsystem.
Definition: lib/sockets.cpp:27
ecl::socket_descriptor socket_pair[2]
#define DATA1
ecl_io_PUBLIC SocketError close_socket(const socket_descriptor &sock)
Close a socket.
Definition: lib/sockets.cpp:49
Extends the generic error handler with socket specific error strings.
Definition: sockets.hpp:68


ecl_io
Author(s): Daniel Stonier
autogenerated on Fri Jun 7 2019 21:52:45