S
Sin Jeong-hun
Hi. I'm writing a Client/Multi-threaded Server program on Windows
Vista. It worked fine on Windows Vista, but when the server ran on
Windows XP,
I/O operation has been aborted because of either a thread exit or an
application request
exception randomly occurred at the OnReceive method (asynchronous tcp
stream reading). I searched all over the internet and found a post
posted few years ago. He had the same problem as me, and he said it
seemed like a bug of the .NET framework. He said changing normal
Thread to ThreadPool solved the problem but he couldn't explain the
reason. So I tried his explanation and it really solved the problem
(as least till now)
My original code was:
Dim client As TcpClient = Listener.AcceptTcpClient()
Dim t As New Thread(New ParameterizedThreadStart(AddressOf
OnNewClient))
t.Start(client)
And I rewrote like:
Dim client As TcpClient = Listener.AcceptTcpClient()
ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf OnNewClient),
client)
Could you please tell me what was wrong with my original code? And
using ThreadPool really solves the problem? I mean can there be any
other possiblilities of new problem?
Thank you as always.
--------------------------------------------------
PS, other parts of the code are
Private Sub OnNewClient(ByVal client As Object)
Interlocked.Increment(LastID)
If LastID = Integer.MaxValue Then
LastID = Integer.MinValue
End If
Dim c As TcpClient = CType(client, TcpClient)
Dim handler As New ClientHandler(c, LastID)
Clients.Add(handler)
AddHandler handler.MessageReceived, AddressOf OnMessageReceived
AddHandler handler.ConnectionClosed, AddressOf OnConnectionClosed
End Sub
Public Class ClientHandler
Public Sub New(ByVal client As TcpClient, ByVal id As Integer)
TheClient = client
_ClientID = id
ClientStream = client.GetStream()
Dim state As New StateObject()
state.WorkSocket = client.Client
client.Client.BeginReceive(state.RawBuffer, 0,
StateObject.BufferSize, SocketFlags.None, New AsyncCallback(AddressOf
OnReceive), state)
End Sub
Vista. It worked fine on Windows Vista, but when the server ran on
Windows XP,
I/O operation has been aborted because of either a thread exit or an
application request
exception randomly occurred at the OnReceive method (asynchronous tcp
stream reading). I searched all over the internet and found a post
posted few years ago. He had the same problem as me, and he said it
seemed like a bug of the .NET framework. He said changing normal
Thread to ThreadPool solved the problem but he couldn't explain the
reason. So I tried his explanation and it really solved the problem
(as least till now)
My original code was:
Dim client As TcpClient = Listener.AcceptTcpClient()
Dim t As New Thread(New ParameterizedThreadStart(AddressOf
OnNewClient))
t.Start(client)
And I rewrote like:
Dim client As TcpClient = Listener.AcceptTcpClient()
ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf OnNewClient),
client)
Could you please tell me what was wrong with my original code? And
using ThreadPool really solves the problem? I mean can there be any
other possiblilities of new problem?
Thank you as always.
--------------------------------------------------
PS, other parts of the code are
Private Sub OnNewClient(ByVal client As Object)
Interlocked.Increment(LastID)
If LastID = Integer.MaxValue Then
LastID = Integer.MinValue
End If
Dim c As TcpClient = CType(client, TcpClient)
Dim handler As New ClientHandler(c, LastID)
Clients.Add(handler)
AddHandler handler.MessageReceived, AddressOf OnMessageReceived
AddHandler handler.ConnectionClosed, AddressOf OnConnectionClosed
End Sub
Public Class ClientHandler
Public Sub New(ByVal client As TcpClient, ByVal id As Integer)
TheClient = client
_ClientID = id
ClientStream = client.GetStream()
Dim state As New StateObject()
state.WorkSocket = client.Client
client.Client.BeginReceive(state.RawBuffer, 0,
StateObject.BufferSize, SocketFlags.None, New AsyncCallback(AddressOf
OnReceive), state)
End Sub