socket_client.cpp
Go to the documentation of this file.
1 
8 /*****************************************************************************
9 ** Platform Check
10 *****************************************************************************/
11 
12 #include <ecl/config/ecl.hpp>
13 
14 #ifdef ECL_IS_POSIX
15 #ifndef ECL_IS_MAC
16 
17 /*****************************************************************************
18 ** Includes
19 *****************************************************************************/
20 
21 #include <iostream>
22 #include <string>
23 #include <ecl/command_line.hpp>
24 #include <ecl/formatters.hpp>
25 #include <ecl/threads/thread.hpp>
26 #include <ecl/time/sleep.hpp>
27 #include <ecl/time/timestamp.hpp>
28 #include <ecl/devices/socket.hpp>
29 
30 /*****************************************************************************
31 ** Namespaces
32 *****************************************************************************/
33 
34 namespace ecl {
35 namespace demos {
36 
37 /*****************************************************************************
38 ** Using
39 *****************************************************************************/
40 
41 using std::string;
42 using ecl::CmdLine;
43 using ecl::ValueArg;
44 using ecl::SwitchArg;
45 using ecl::ArgException;
47 using ecl::SocketClient;
48 using ecl::Format;
49 using ecl::Hex;
50 using ecl::NoAlign;
51 using ecl::Thread;
52 using ecl::MilliSleep;
53 using ecl::TimeStamp;
54 
55 
56 /*****************************************************************************
57 ** Classes
58 *****************************************************************************/
59 
60 class Reader {
61 public:
62  Reader(SocketClient &socket_server, bool timestamps_reqd, bool hex_format_reqd = false) :
63  socket(socket_server),
64  timestamps(timestamps_reqd),
65  hex(hex_format_reqd),
66  new_line(true)
67  {}
68 
69  void loop() {
70 
71  Format<unsigned char> hex_format(-1,NoAlign,Hex);
72  char c;
73  long ch_read;
74  MilliSleep sleep;
75 
76  if ( hex ) { std::cout << "Hex format" << std::endl; } else { std::cout << "Ascii format" << std::endl; }
77 
78  while (1) {
79  ch_read = socket.read(c);
80  if ( ch_read > 0 ) {
81  if ( hex ) {
82  if ( timestamps ) {
83  if ( new_line ) { timestamp.stamp(); }
84  std::cout << "[" << timestamp << "] : ";
85  }
86  std::cout << hex_format(c) << std::endl;
87  } else {
88  if ( timestamps && new_line ) {
89  timestamp.stamp();
90  std::cout << "[" << timestamp << "] : ";
91  }
92  std::cout << c;
93  }
94  std::cout.flush();
95  if ( timestamps ) {
96  if ( new_line ) {
97  new_line = false;
98  } else if ( c == '\n' ) {
99  new_line = true;
100  }
101  }
102  } else if ( ch_read < 0 ) {
103  if ( ch_read == ConnectionHungUp ) {
104  std::cout << "Server Hung Up." << std::endl;
105  break;
106  }
107  } else {
108  // nothing read
109  }
110  sleep(15);
111  }
112 
113  }
114 
115 private:
116  SocketClient &socket;
117  TimeStamp timestamp;
118  bool timestamps;
119  bool hex;
120  bool new_line;
121 };
122 
123 class Writer {
124 public:
125  Writer(SocketClient &socket_server, bool timestamps_reqd) : socket(socket_server), timestamps(timestamps_reqd) {}
126 
127  void loop() {
128  MilliSleep sleep;
129  char buffer[256];
130  memset(buffer,0,256);
131  char *s_ptr;
132  while( 1 ) {
133  if ( fgets(buffer,80,stdin) == NULL ) {
134  break;
135  }
136  // fgets always terminates with a null character, even if it manages
137  // to read n-1 chars
138  s_ptr = buffer;
139  while ( *s_ptr != '\0') { ++s_ptr; }
140  if ( timestamps ) { timestamp.stamp(); }
141  socket.write(buffer,s_ptr - buffer);
142  if ( timestamps ) {
143  std::cout << "[" << timestamp << "] : ";
144  }
145  std::cout << buffer; // already terminated by an endl;
146  std::cout.flush();
147  sleep(500);
148  }
149 
150  }
151 
152 private:
153  SocketClient &socket;
154  TimeStamp timestamp;
155  bool timestamps;
156 };
157 
158 } // namespace demos
159 } // namespace ecl
160 
161 /*****************************************************************************
162 ** Using
163 *****************************************************************************/
164 
165 using namespace ecl::demos;
166 
167 /*****************************************************************************
168 ** Main
169 *****************************************************************************/
170 
171 int main(int argc, char** argv) {
172 
173  std::cout << std::endl;
174  std::cout << "***********************************************************" << std::endl;
175  std::cout << " Parsing Command Line" << std::endl;
176  std::cout << "***********************************************************" << std::endl;
177  std::cout << std::endl;
178 
179  int port = 0;
180  string hostname;
181  bool hex(false);
182  bool timestamps(false);
183 
184  try {
185  CmdLine cmd("This is a simple interface for making a socket client connection.",' ',"0.1");
186  ValueArg<string> arg_hostname("n","hostname","Hostname of the server [localhost]",false,"localhost","string");
187  ValueArg<int> arg_port("p","port","Port number to connect to [1470].",false,1470,"integer");
188  SwitchArg switch_timestamps("t","timestamp","Timestamp incoming/outgoings.",false);
189  SwitchArg switch_hex("x","hex","Enable hex output.",false);
190 
191  cmd.add(arg_hostname);
192  cmd.add(arg_port);
193  cmd.add(switch_hex);
194  cmd.add(switch_timestamps);
195  cmd.parse(argc,argv);
196 
197  hostname = arg_hostname.getValue();
198  port = arg_port.getValue();
199  hex = switch_hex.getValue();
200  timestamps = switch_timestamps.getValue();
201 
202  } catch ( ArgException &e ) {
203  std::cerr << "error: " << e.error() << " for arg " << e.argId() << std::endl;
204  }
205 
206  std::cout << "Connecting to: " << hostname << " [" << port << "]" << std::endl;
207 
208  std::cout << std::endl;
209  std::cout << "***********************************************************" << std::endl;
210  std::cout << " Read and Write Threads " << std::endl;
211  std::cout << "***********************************************************" << std::endl;
212  std::cout << std::endl;
213 
214  SocketClient client(hostname,port);
215  Writer writer(client,timestamps);
216  Reader reader(client,timestamps,hex);
217  Thread read_thread(&Reader::loop, reader);
218  Thread write_thread(&Writer::loop, writer);
219 
220  read_thread.join();
221  write_thread.join();
222 
223 
224  std::cout << std::endl;
225  std::cout << "***********************************************************" << std::endl;
226  std::cout << " Passed" << std::endl;
227  std::cout << "***********************************************************" << std::endl;
228  std::cout << std::endl;
229 
230  return 0;
231 }
232 
233 #endif /* ECL_IS_POSIX */
234 #endif /* !ECL_IS_MAC */
235 
236 #if !defined ECL_IS_POSIX || ECL_IS_MAC
237 
238 #include <iostream>
239 
240 int main(int argc, char **argv) {
241 
242  std::cout << "This is a posix (not mac) only app." << std::endl;
243  return 0;
244 }
245 
246 #endif
Demo programs.
ConnectionHungUp
int main(int argc, char **argv)


ecl_core_apps
Author(s): Daniel Stonier
autogenerated on Mon Jun 10 2019 13:08:55