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!");
    Last edited by Noreturn; March 1st, 2007 at 10:40 PM.

  2. #2
    Join Date
    Jan 2002
    Scaro, UK

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

    You can have a look at my C# FTP server which uses blocking sockets in threads :

    It's in C# (obviously) but should show you what to do.


    Darwen. - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

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