12 #include <ecl/config/ecl.hpp> 47 using ecl::SocketClient;
52 using ecl::MilliSleep;
62 Reader(SocketClient &socket_server,
bool timestamps_reqd,
bool hex_format_reqd =
false) :
63 socket(socket_server),
64 timestamps(timestamps_reqd),
71 Format<unsigned char> hex_format(-1,
NoAlign,
Hex);
76 if ( hex ) { std::cout <<
"Hex format" << std::endl; }
else { std::cout <<
"Ascii format" << std::endl; }
79 ch_read = socket.read(c);
83 if ( new_line ) { timestamp.stamp(); }
84 std::cout <<
"[" << timestamp <<
"] : ";
86 std::cout << hex_format(c) << std::endl;
88 if ( timestamps && new_line ) {
90 std::cout <<
"[" << timestamp <<
"] : ";
98 }
else if ( c ==
'\n' ) {
102 }
else if ( ch_read < 0 ) {
104 std::cout <<
"Server Hung Up." << std::endl;
116 SocketClient &socket;
125 Writer(SocketClient &socket_server,
bool timestamps_reqd) : socket(socket_server), timestamps(timestamps_reqd) {}
130 memset(buffer,0,256);
133 if ( fgets(buffer,80,stdin) == NULL ) {
139 while ( *s_ptr !=
'\0') { ++s_ptr; }
140 if ( timestamps ) { timestamp.stamp(); }
141 socket.write(buffer,s_ptr - buffer);
143 std::cout <<
"[" << timestamp <<
"] : ";
153 SocketClient &socket;
171 int main(
int argc,
char** argv) {
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;
182 bool timestamps(
false);
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);
191 cmd.add(arg_hostname);
194 cmd.add(switch_timestamps);
195 cmd.parse(argc,argv);
197 hostname = arg_hostname.getValue();
198 port = arg_port.getValue();
199 hex = switch_hex.getValue();
200 timestamps = switch_timestamps.getValue();
202 }
catch ( ArgException &e ) {
203 std::cerr <<
"error: " << e.error() <<
" for arg " << e.argId() << std::endl;
206 std::cout <<
"Connecting to: " << hostname <<
" [" << port <<
"]" << std::endl;
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;
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);
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;
236 #if !defined ECL_IS_POSIX || ECL_IS_MAC 240 int main(
int argc,
char **argv) {
242 std::cout <<
"This is a posix (not mac) only app." << std::endl;
int main(int argc, char **argv)