Overview
Features
Download
Documentation
Community
Add-Ons & Services

Sending data across the Network

Please post support and help requests here.

Sending data across the Network

Postby Wombat3 » 04 Oct 2009, 01:32

OK, I'm relatively new at using libraries like Poco so I'm sure the answer is going to be something really obvious, but I've spent all day working on just this and haven't found the answer.

I want to send binary data, specifically an mp3 file, across the network between a server program and client program.

I've looked at several of the Net examples and the inflate/deflate examples in hopes of finding how to do it, but so far I haven't managed to get it to send.
The server program is largely the same as the EchoServer example. The code which is supposed to send data is here:

Code: Select all
void File_Sender::On_Readable(const Poco::AutoPtr<Poco::Net::ReadableNotification>& ptr)
{
   // so that it does not produce continuing ReadableNotifcations
   m_Socket.receiveBytes(m_Buffer, BUFFER_SIZE);

   std::cout << "something received\n";

   // m_File_In is a std::ifstream pointing to a <1kb file
   // m_Stream is a SocketStream made from a StreamSocket
   Poco::StreamCopier::copyStream(m_File_In, m_Stream);
}

The Client code is supposed to send a couple characters to produce a ReadableNotification (silly I know, just trying to get it to send data some way before changing much from the EchoServer example), and then copy the SocketStream to a std::ofstream

Code: Select all
int main(int argc, char* argv[])
{
   std::ofstream file_Out;

   file_Out.open("test123.txt");

   Poco::Net::SocketAddress sa("localhost",49180);
   Poco::Net::StreamSocket socket(sa);

   Poco::Net::SocketStream str(socket);

   str << "aa" << std::flush;

   socket.shutdownSend();

   Poco::StreamCopier::copyStream(str, file_Out);

   system("PAUSE");
   return 0;
}


Thanks much to everyone who takes the time to read my long post :)

-The Wombat
Wombat3
 
Posts: 3
Joined: 04 Oct 2009, 00:43

Re: Sending data across the Network

Postby alex » 04 Oct 2009, 19:19

Wombat3 wrote:
Code: Select all
void File_Sender::On_Readable(const Poco::AutoPtr<Poco::Net::ReadableNotification>& ptr)
{
   // so that it does not produce continuing ReadableNotifcations
   m_Socket.receiveBytes(m_Buffer, BUFFER_SIZE);

   std::cout << "something received\n";

   // m_File_In is a std::ifstream pointing to a <1kb file
   // m_Stream is a SocketStream made from a StreamSocket
   Poco::StreamCopier::copyStream(m_File_In, m_Stream);
}

The Client code is supposed to send a couple characters to produce a ReadableNotification (silly I know, just trying to get it to send data some way before changing much from the EchoServer example), and then copy the SocketStream to a std::ofstream


There's missing code, but it appears that you are doing the following:

There is one socket on the client side. On the server side, you receive data from the client and then try to send the file back on another socket. If that is indeed the case, where do you expect to receive the data sent back from the server?
alex
 
Posts: 1086
Joined: 11 Jul 2006, 16:27
Location: United_States

Re: Sending data across the Network

Postby alex » 04 Oct 2009, 20:04

This should work:
Code: Select all
void onReadable(const AutoPtr<ReadableNotification>& pNf)
{
   int n = _socket.receiveBytes(_pBuffer, BUFFER_SIZE);
   Poco::Net::SocketStream ss(_socket);
   std::ifstream ifs("test.txt");
   Poco::StreamCopier::copyStream(ifs, ss);
}


Run your server and try to telnet yourself to it , then type a key into the session. You should get the file contents back.
alex
 
Posts: 1086
Joined: 11 Jul 2006, 16:27
Location: United_States


Return to Support

Who is online

Users browsing this forum: No registered users and 1 guest