[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Reading from a TCP socket into a file using binary.
From: |
Jon Wilson |
Subject: |
Reading from a TCP socket into a file using binary. |
Date: |
Sat, 08 Feb 2003 15:44:05 +0000 |
User-agent: |
Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.2.1) Gecko/20021130 |
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;
}
- Reading from a TCP socket into a file using binary.,
Jon Wilson <=