|
-
Aug 26th, 2009, 05:21 AM
#1
Thread Starter
Fanatic Member
[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;
}
}
}
-
Aug 26th, 2009, 05:25 AM
#2
Thread Starter
Fanatic Member
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.
-
Aug 26th, 2009, 05:26 AM
#3
Thread Starter
Fanatic Member
Re: TcpListener Multi-threaded
I'm trying to reproducing the error and post the exact exception here.
-
Aug 26th, 2009, 06:36 AM
#4
Re: TcpListener Multi-threaded
The TcpListener inherently supports asynchronous processing via the BeginAcceptTcpClient and EndAcceptTcpClient methods.
-
Aug 26th, 2009, 06:39 AM
#5
Thread Starter
Fanatic Member
Re: TcpListener Multi-threaded
 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.
-
Aug 26th, 2009, 07:43 PM
#6
Re: TcpListener Multi-threaded
 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.
-
Aug 26th, 2009, 08:26 PM
#7
Thread Starter
Fanatic Member
Re: TcpListener Multi-threaded
 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.
-
Aug 26th, 2009, 08:31 PM
#8
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?
-
Aug 26th, 2009, 08:41 PM
#9
Thread Starter
Fanatic Member
Re: TcpListener Multi-threaded
 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.
-
Aug 27th, 2009, 01:37 AM
#10
Thread Starter
Fanatic Member
Re: TcpListener Multi-threaded
It is working with Java, but after several connect-send-receive-disconnect(500+); the server refuses another connection.
-
Aug 27th, 2009, 01:50 AM
#11
Re: TcpListener Multi-threaded
-
Aug 27th, 2009, 02:01 AM
#12
Thread Starter
Fanatic Member
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.
-
Aug 27th, 2009, 02:18 AM
#13
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.
-
Aug 27th, 2009, 03:20 AM
#14
Thread Starter
Fanatic Member
Re: TcpListener Multi-threaded
Thanks.
I think I just need to limit incoming connections.
-
Dec 11th, 2012, 07:51 PM
#15
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
-
Dec 11th, 2012, 08:46 PM
#16
Thread Starter
Fanatic Member
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.
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
|