CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 2 of 2
  1. #1
    Join Date
    Dec 2006

    I need help about multithreading in TCPListener ...

    In a TCPListener program I've coded, I used the sample codes about TCPListener I found in MSDN Library. But, when I execute this program, the interface doesn't give any response to other events such as clicking buttons, since the continuous loop prevents the other codes to be run. I know that I must do multithreading, but I don't know how to do. . Meanwhile, I also tried to use "BackgroundWorker" component, but it caused the data transfer to fail. Here's sample code, what can I do?

    void ServerActivate()
    Int32 port = 10000;
    IPAddress^ localAddr = IPAddress::Parse(myipBox->Text);
    TcpListener^ server = gcnew TcpListener(localAddr,port);
    array<Byte>^bytes = gcnew array<Byte>(256);
    String^ data = nullptr;

    // Enter the listening loop.
    while (true)
    MessageBox::Show("Ready To Receive!");
    TcpClient^ client = server->AcceptTcpClient();
    NetworkStream^ stream = client->GetStream();
    Int32 i;

    while ( i = stream->Read( bytes, 0, bytes->Length ) )
    data = System::Text::Encoding::ASCII->GetString( bytes, 0, i );
    data = data->ToUpper();
    array<Byte>^msg = System::Text::Encoding::ASCII->GetBytes(data);
    stream->Write( msg, 0, msg->Length );
    catch (SocketException^ e)
    MessageBox::Show("Socket Exception Occured!");

  2. #2
    Join Date
    Nov 2006

    Re: I need help about multithreading in TCPListener ...

    This is a managed C++ program, so you'll need to try this in that forum (moderators might move this).

    As a hint for a start, look up how to start a thread, move that loop you're concerned about into that thread - which will free up the GUI thread to respond.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Click Here to Expand Forum to Full Width