-
March 1st, 2007, 10:38 PM
#1
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?
private:
void ServerActivate()
{
try
{
Int32 port = 10000;
IPAddress^ localAddr = IPAddress::Parse(myipBox->Text);
TcpListener^ server = gcnew TcpListener(localAddr,port);
server->Start();
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 );
outputBox->Items->Add(data);
data = data->ToUpper();
array<Byte>^msg = System::Text::Encoding::ASCII->GetBytes(data);
stream->Write( msg, 0, msg->Length );
}
client->Close();
}
}
catch (SocketException^ e)
{
MessageBox::Show("Socket Exception Occured!");
}
}
Last edited by Noreturn; March 1st, 2007 at 10:40 PM.
-
March 2nd, 2007, 11:40 AM
#2
Re: I need help about multithreading in TCPListener ...
You can have a look at my C# FTP server which uses blocking sockets in threads : http://www.codeguru.com/csharp/cshar...cle.php/c7409/.
It's in C# (obviously) but should show you what to do.
Yours,
Darwen.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|