Overview
Features
Download
Documentation
Community
Add-Ons & Services

Why this happened?(about use SocketStream many times)

Please post support and help requests here.

Why this happened?(about use SocketStream many times)

Postby coolzdp » 25 Jan 2008, 16:24

Server code is:
Code: Select all

void FileServer::run()
{
   _ready.set();
   Poco::Timespan span(250000);
   while (!_stop)
   {
      if (_socket.poll(span, Socket::SELECT_READ))
      {
         StreamSocket ss = _socket.acceptConnection();
         std::string path;
         try
         {
            for (int i = 0; i < 3; i++)
            {
               SocketStream str(ss);
               BinaryReader r(str);

               r >> path;
               std::cout << i << " path is: " << path << std::endl;
            }
            
         }
         catch (Poco::Exception& exc)
         {
            std::cerr << "FileServer: " << exc.displayText() << std::endl;
         }
      }
   }
}


Client is:
Code: Select all

         StreamSocket ss;
         unsigned short port = 1970;
         std::string path;
         ss.connect(SocketAddress("localhost", port));

         for (int i = 0; i < 3; i++)
         {
            SocketStream str(ss);
            BinaryWriter w(str);
            w << "AAAA";
            w.flush();
            //Thread::sleep(1);  // if no this code, only send one time,
                        //then server will hang up on second "r >> path;", why?
         }
coolzdp
 
Posts: 8
Joined: 12 Jan 2008, 05:04
Location: China

Re: Why this happened?(about use SocketStream many times)

Postby alex » 28 Jan 2008, 19:02

On the server side, all the data is received by the first ))SocketStream(( you create. However, only the first 4 characters are extracted by the ))BinaryReader((.

When you get into the next step and create new SocketStream, there's no data left, since it is gone with the previous ))SocketStream((.

This should solve your problem:

Code: Select all

SocketStream str(ss);
BinaryReader r(str);
for (int i = 0; i < 3; ++i) r >> path;


HTH

Alex
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 3 guests