[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
RE: Reading from a TCP socket into a file using binary.
From: |
Alex Pavloff |
Subject: |
RE: Reading from a TCP socket into a file using binary. |
Date: |
Mon, 10 Feb 2003 10:43:28 -0800 |
Why are you creating this seperate tcpstream and then passing it?
What I have done (and works) is create a class that derives from
ost::TCPSession (which derives from ost::Thread) itself. Then, when the
listening socket accepts a connection, I do this:
CMyTCPSession* pMySession = new CMyTCPSession( *sock );
pMySession->start(); // (or detach, what works for you)
The run() method of CMyTCPSession then does the work. I believe this is how
the examples do it.
Oh, and whats with the strange std::string,then allocate buffer and strcpy
thing? Just do a .c_str() in the fstream open call.
Alex Pavloff - address@hidden
Eason Technology -- www.eason.com
> -----Original Message-----
> From: Jon Wilson [mailto:address@hidden
> Sent: Saturday, February 08, 2003 7:44 AM
> To: address@hidden
> Subject: Reading from a TCP socket into a file using binary.
>
>
> I am trying to write an application to read data from a TCP
> socket into
> a file using binary. It is to be used to transfer a PNG file from a
> client to the server. However, I am having problems getting
> it to work.
>
> The program is a multithreaded server which dispatches a
> thread upon an
> attempted connection which generates a random filename from
> the current
> time and then writes the data sent to the socket into the file.
>
> As it stands, the program seems to copy one byte into the file, but
> doesn't seem to be the first bit from the file sent by the client.
> Please can someone help me with this. Have I missused the
> library? Or am
> I missing something obvious, I have been struggling with this for a
> while now!
>
> Many Thanks.
>
> Jon Wilson
>
> #include <cc++/socket.h>
> #include <iostream>
> #include <fstream>
> #include <cstdlib>
> #include <sstream>
> #include <ctime>
>
> #ifdef CCXX_NAMESPACES
> using namespace std;
> using namespace ost;
> #endif
> class ThreadOut: public Thread{
> tcpstream* tcp;
> public:
> ThreadOut(tcpstream* tcp)
> {
> this->tcp=tcp;
> start();
> }
> void run(){
> time_t seconds= time(NULL);
> int i=seconds;
>
> ostringstream istr;
> istr << i;
> string name=istr.str()+"out.png";
>
> char nm[name.length()];
> strcpy(nm,name.c_str());
> ofstream out(nm,ios::binary);
>
> /*ERROR HERE
> while(*tcp){
> out.put(tcp->get());
> }
> */
>
> cout << "Read complete, closing" << endl;
> tcp->close();
> out.close();
> }
> };
>
> int main(int argc, char *argv[]){
> if(argc!=2){
> cout << "Arguments wrong" << endl;
> exit(1);
> }
> cout << argv[1] << endl;
> InetAddress addr = argv[1];
> TCPSocket *sock = new TCPSocket(addr, 9382);
>
> cout << "Streams open" << endl;
> char c;
> while (1){
> cout << "WAITING" << endl;
> if (sock->isPendingConnection()){
> cout << "Connection Established" << endl;
> tcpstream* tcp=new tcpstream(*sock,ios::binary);
> new ThreadOut(tcp);
>
> }
> }
> return 0;
> }
>
>
>
> _______________________________________________
> Bug-commoncpp mailing list
> address@hidden
> http://mail.gnu.org/mailman/listinfo/bug-commoncpp
>