BobFX 0 Light Poster

Hi,

I have a server running with a Form based managed C++ app.

It initializes with:

try
  {
    listener = gcnew TcpListener(ipAddress, PORT_lock);

    listener->Start();              // Start listening for client requests.

    listener->BeginAcceptTcpClient(gcnew AsyncCallback(DoAcceptTcpClientCallback), listener);

The callback funtion is defined as:

public: static void Form1::DoAcceptTcpClientCallback(IAsyncResult^ result)
{
  try
   { // Get the listener that handles the client request.
     TcpListener^ listener = (TcpListener^) result->AsyncState;

//
// >>>>>>>>>>   First breakpoint
//
     TcpClient^ client = listener->AcceptTcpClient();

//
// >>>>>>>>>>   Second breakpoint
//
     Console::WriteLine("Connected!");
     String ^data = nullptr;
     array<Byte>^bytes = gcnew array<Byte>(MAX_LEN_MES);

     // Get a stream Object* for reading and writing
     NetworkStream^ stream = client->GetStream();
     Int32 len, i;

     len = stream->Read(bytes, 0, bytes->Length);
     if (len && len < MAX_LEN_MES)
      {  char buff[MAX_LEN_MES+1];
         for(i = 0; i<len; i++)
           buff[i] = bytes[i];
         buff[len] = '\0';
         Console::WriteLine("Received " + Char2String(buff));

         char rep[] = "Answering";
         for(i=0; i<10; i++)
           bytes[i] = rep[i];
         stream->Write(bytes, 0, 10);
         client->Close();  // Shutdown and end connection
      }
     listener->BeginAcceptTcpClient(gcnew AsyncCallback(DoAcceptTcpClientCallback), listener);
 
   }

  catch (Exception^ e)
   {
     Console::WriteLine(e->ToString());
   }


}

When a client connects to the server, and sends some data, DoAcceptTcpClientCallback is indeed called, and the debugger stops at the line marked:

//
// >>>>>>>>>>   First breakpoint
//
     TcpClient^ client = listener->AcceptTcpClient();

Hitting F10 to continue debugging *does not* go the second breakpoint, but instead returns to the main Form loop.

At that point, the client stalls waiting for the response that never happens.

If I restart a new client connection, this time the debugger stops at:

//
// >>>>>>>>>>   Second breakpoint
//
     Console::WriteLine("Connected!");

*without* stopping at the first breakpoint.

Hitting F5 make the callback function answer, and the client to get that answer.

Subsequent tries go through the same cycle: a first connection does not reach the second breakpoint, and a second one skips it to go to the second one.

Any idea what I am doing wrong?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.