[RESOLVED] TcpListener Multi-threaded
I've created a multi-threaded TcpListener to receive request from clients.
I created a background worker to handle the endless loop that checks if there is a new connection.
Code:
bw.WorkerSupportsCancellation = true;
bw.WorkerReportsProgress = true;
bw.DoWork += new DoWorkEventHandler(bw_start);
if (!bw.IsBusy)
bw.RunWorkerAsync();
This is the bw_start:
Code:
private void bw_start(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
TcpClient clientSocket = default(TcpClient);
tListener.Start();
int counter = 0;
counter = 0;
while (true)
{
counter += 1;
clientSocket = tListener.AcceptTcpClient();
HandleClient client = new HandleClient();
client.startClient(clientSocket, Convert.ToString(counter));
}
clientSocket.Close();
tListener.Stop();
}
HandleClient Methods:
Code:
public void startClient(TcpClient inClientSocket, string clineNo)
{
this.clientSocket = inClientSocket;
this.clNo = clineNo;
ThreadPool.QueueUserWorkItem(new WaitCallback(doTransact));
}
Code:
private void doTransact(object state)
{
//some declarations here
while (True)
{
try
{
if (clientSocket.Connected)
{
recv = networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize); //this part receives an error that is realted about threading
//some processing here
//send response
}
else
{
break;
}
}
else
{
break;
}
}
}
Re: TcpListener Multi-threaded
Then I've created a multi-threaded client that sends transactions with random interval between 0-1000ms.
Then after 100+ sending it stucks in:
Code:
recv = networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
When I restart the client, it says that the server isn't listening.
Re: TcpListener Multi-threaded
I'm trying to reproducing the error and post the exact exception here.
Re: TcpListener Multi-threaded
The TcpListener inherently supports asynchronous processing via the BeginAcceptTcpClient and EndAcceptTcpClient methods.
Re: TcpListener Multi-threaded
Quote:
Originally Posted by
jmcilhinney
The TcpListener inherently supports asynchronous processing via the BeginAcceptTcpClient and EndAcceptTcpClient methods.
I'll try it.
But is it possible for a Java-based client to connect to an asynchronous .NET-based server?
Thanks.
Re: TcpListener Multi-threaded
Quote:
Originally Posted by
eSPiYa
I'll try it.
But is it possible for a Java-based client to connect to an asynchronous .NET-based server?
Thanks.
TCP is TCP. It's a standard protocol. It doesn't have to be a .NET TcpClient at the other end. It just has to be something talking TCP.
Re: TcpListener Multi-threaded
Quote:
Originally Posted by
jmcilhinney
TCP is TCP. It's a standard protocol. It doesn't have to be a .NET TcpClient at the other end. It just has to be something talking TCP.
Thanks.
But I've tried before to use an asynchronous TcpClient to connect to a Java-based server and I can't connect.
Anyway, I'll try to test it.
Re: TcpListener Multi-threaded
What's your definition of an asynchronous TcpClient? Are you talking about calling BeginConnect instead of Connect? How exactly did you do it? Did you provide a callback and then call EndConnect in that method?
Re: TcpListener Multi-threaded
Quote:
Originally Posted by
jmcilhinney
What's your definition of an asynchronous TcpClient? Are you talking about calling BeginConnect instead of Connect? How exactly did you do it? Did you provide a callback and then call EndConnect in that method?
I can't remember how I did it exactly but I used BeginConnect then when I tried to send any message; the connection was dropped.
Re: TcpListener Multi-threaded
It is working with Java, but after several connect-send-receive-disconnect(500+); the server refuses another connection.
Re: TcpListener Multi-threaded
Re: TcpListener Multi-threaded
It seems that the socket connection drops.
This is the error message from client:
Code:
No connection could be made because the target machine actively refused it
On the server side, it doesn't throw any exception.
Re: TcpListener Multi-threaded
I can't tell you much more I'm afraid as I've not really used TCP comms much myself. Hopefully the extra info will enable someone else to help you though.
Re: TcpListener Multi-threaded
Thanks.
I think I just need to limit incoming connections.
Re: TcpListener Multi-threaded
Have you googled for that error message - first link I saw had good info
https://www.google.com/webhp?sourcei...bih=1087&ion=1
Re: [RESOLVED] TcpListener Multi-threaded
Sorry, forgot to tag this thread as resolved.
If I remembered it correctly, after 3years, it is the .NET and Java [int] variable. One of them is using unsigned integer as default int, while the other one is signed integer.