|
From: | Eng Hian |
Subject: | howto create a thread to connect to tcp server and send binary data? |
Date: | Fri, 28 Feb 2003 11:44:27 +0800 |
Hi
I know this is a "Bug" mailing list. Forgive me but
I am quite a newbie for C++ and commoncpp.
I want to write a program and can connect to a TCP
server and then send and receive binary data (which may contain
zeroes).
I found that are various classes like TCPSocket,
TCPStream, tcpstream, ...
I also see that only TCP server example programs
are available in the commoncpp.
If I use tcpstream to send binary with embedded
'\0' I cannot use << operator.
Furthermore the data may not be terminated by endl.
So to send I use:
class ThreadClient: public
Thread
{ public: ThreadClient() { sleep(1000); start(); } void run() { char inbuf[1024], outbuf[10]; tcpstream tcp("127.0.0.1:3999"); //CHUSocketPort tcp("127.0.0.1:3999"); outbuf[0] = 'p'; outbuf[1] = 'i'; outbuf[2] = 'p'; outbuf[3] = 'a'; outbuf[4] = '\0'; outbuf[5] = 'o'; outbuf[6] = 'g'; outbuf[7] = '\r'; outbuf[8] = '\n'; outbuf[9] = '\0'; cout << "[Client]: Connected" << endl; //tcp << "pippo\r" << endl; //tcp << outbuf << endl; for (int i = 0; i < 8; i++) { cout << "[Client]: sent=" << outbuf[i] << endl; tcp.put(outbuf[i]); } tcp.sync(); cout << "[Client]: Sent Message" << endl; tcp >> inbuf; cout << "[Reply]: " << inbuf << endl; tcp.close(); cout << "[Client]: Disconnected" << endl; } }; Questions:
(1) any other way to flush the send data without
using sync()? Because I dont want to flush the input at the same
time.
(2) the document says tcpstream is for non-threaded
application. Can my code above be use then?
(3) If possible, can anyone provide me a sample
source code that implements simple TCP client for send/receive binary
data?
Thanks in advance.
/enghian
|
[Prev in Thread] | Current Thread | [Next in Thread] |