A
Arno
reposted with the right microsoft managed newsgroup ID: Sorry for the
inconvinience
Hi,
I've written a class for client-socket connection, but I get a lot of times
the error message "Unable to read data from the transport connection" when
restart reading the stream with socket.BeginRead in the Sub
SocketIncomingMsg. I'm debugging now for weeks, but I can't detect where
it's
going wrong.
the part of code where it fails:
Try
RnetworkStream.BeginRead(myReceivedData, 0,
myReceivedData.Length, New AsyncCallback(AddressOf SocketIncomingMsg),
RnetworkStream)
Catch ex As Exception
'THIS ONE OFTEN FAILES
'UNABLE TO READ DATA FROM THE TRANSPORT CONNECTION
DisconnectSocket()
End Try
Can someone please review my code and point me in the right direction. This
thing is driving me nuts..
The Complete code:
Imports System.Net.Sockets
Imports System.Text
Imports System.Threading
Namespace TCPIP
Public Enum Status
Disconnected = 0
Listening = 1
Connected = 3
Connecting = 2
End Enum
Public Class TCPClientSock
Private mState As TCPIP.Status
Private mTcpClient As TcpClient
Private RnetworkStream As NetworkStream
Public Event DataReceived(ByVal Data As String)
Public Event CommError(ByVal ErrorMessage As Exception)
Public Event StatusChanged(ByVal State As TCPIP.Status)
Private myReceivedData() As Byte
Public Sub New()
mState = TCPIP.Status.Disconnected
RaiseEvent StatusChanged(mState)
End Sub
Public ReadOnly Property Connected() As Boolean
Get
Return (mState = Status.Connected)
End Get
End Property
Public Sub Shutdown()
Me.DisconnectSocket()
End Sub
Public Function ConnectSocket(ByVal Server As String, ByVal Port As
Int16) As Boolean
Try
mTcpClient = Nothing
mTcpClient = New TcpClient
'make connection
mState = TCPIP.Status.Connecting
RaiseEvent StatusChanged(mState)
mTcpClient.Connect(Server, Port)
'connected
RnetworkStream = mTcpClient.GetStream
If RnetworkStream.CanRead Then
mState = TCPIP.Status.Connected
ReDim myReceivedData(mTcpClient.ReceiveBufferSize)
'start reading stream
RnetworkStream.BeginRead(myReceivedData, 0,
myReceivedData.Length, New AsyncCallback(AddressOf SocketIncomingMsg),
RnetworkStream)
RaiseEvent StatusChanged(mState)
Return True
End If
Catch e As Exception ' error while connecting
RaiseEvent CommError(e)
'not connected
mState = TCPIP.Status.Disconnected
RaiseEvent StatusChanged(mState)
Try
mTcpClient.Close()
Catch
End Try
End Try
End Function
Private Sub SocketIncomingMsg(ByVal arMsg As IAsyncResult)
Dim intCount As Integer
Dim lus As Int16
Dim myMessage As String = ""
Try
intCount = RnetworkStream.EndRead(arMsg)
If intCount < 1 Then
'socket closed
DisconnectSocket()
End If
Catch ex As Exception
End Try
For lus = 0 To intCount - 1
myMessage &= Chr(myReceivedData(lus))
Next
'process message in background
ThreadPool.QueueUserWorkItem(AddressOf Me.ProcessReceivedData,
CType(myMessage, Object))
'restart reading stream
Try
RnetworkStream.BeginRead(myReceivedData, 0,
myReceivedData.Length, New AsyncCallback(AddressOf SocketIncomingMsg),
RnetworkStream)
Catch ex As Exception
'THIS ONE OFTEN FAILES
'UNABLE TO READ DATA FROM THE TRANSPORT CONNECTION
DisconnectSocket()
End Try
End Sub
Private Sub ProcessReceivedData(ByVal Data As Object)
If CStr(Data) <> "" Then RaiseEvent DataReceived(CStr(Data))
End Sub
Public Sub DisconnectSocket()
Try
RnetworkStream.Close()
Catch ex As Exception
End Try
Try
mTcpClient.Close()
Catch ex As Exception
End Try
Try
mTcpClient = Nothing
Catch ex As Exception
End Try
mState = TCPIP.Status.Disconnected
RaiseEvent StatusChanged(mState)
End Sub
Public Sub SendSocketData(ByVal Data As String)
Try
Dim SnetworkStream As NetworkStream = mTcpClient.GetStream()
Dim sendBytes(Data.Length - 1) As Byte
Dim lus As Int16
For lus = 0 To Data.Length - 1
sendBytes(lus) = Asc(Data.Substring(lus, 1))
Next
SnetworkStream.Write(sendBytes, 0, sendBytes.Length)
Catch e As Exception
RaiseEvent CommError(e)
End Try
End Sub
End Class
Public Class TCPServerSock
'removed due to irrelevance
End Class
End Namespace
inconvinience
Hi,
I've written a class for client-socket connection, but I get a lot of times
the error message "Unable to read data from the transport connection" when
restart reading the stream with socket.BeginRead in the Sub
SocketIncomingMsg. I'm debugging now for weeks, but I can't detect where
it's
going wrong.
the part of code where it fails:
Try
RnetworkStream.BeginRead(myReceivedData, 0,
myReceivedData.Length, New AsyncCallback(AddressOf SocketIncomingMsg),
RnetworkStream)
Catch ex As Exception
'THIS ONE OFTEN FAILES
'UNABLE TO READ DATA FROM THE TRANSPORT CONNECTION
DisconnectSocket()
End Try
Can someone please review my code and point me in the right direction. This
thing is driving me nuts..
The Complete code:
Imports System.Net.Sockets
Imports System.Text
Imports System.Threading
Namespace TCPIP
Public Enum Status
Disconnected = 0
Listening = 1
Connected = 3
Connecting = 2
End Enum
Public Class TCPClientSock
Private mState As TCPIP.Status
Private mTcpClient As TcpClient
Private RnetworkStream As NetworkStream
Public Event DataReceived(ByVal Data As String)
Public Event CommError(ByVal ErrorMessage As Exception)
Public Event StatusChanged(ByVal State As TCPIP.Status)
Private myReceivedData() As Byte
Public Sub New()
mState = TCPIP.Status.Disconnected
RaiseEvent StatusChanged(mState)
End Sub
Public ReadOnly Property Connected() As Boolean
Get
Return (mState = Status.Connected)
End Get
End Property
Public Sub Shutdown()
Me.DisconnectSocket()
End Sub
Public Function ConnectSocket(ByVal Server As String, ByVal Port As
Int16) As Boolean
Try
mTcpClient = Nothing
mTcpClient = New TcpClient
'make connection
mState = TCPIP.Status.Connecting
RaiseEvent StatusChanged(mState)
mTcpClient.Connect(Server, Port)
'connected
RnetworkStream = mTcpClient.GetStream
If RnetworkStream.CanRead Then
mState = TCPIP.Status.Connected
ReDim myReceivedData(mTcpClient.ReceiveBufferSize)
'start reading stream
RnetworkStream.BeginRead(myReceivedData, 0,
myReceivedData.Length, New AsyncCallback(AddressOf SocketIncomingMsg),
RnetworkStream)
RaiseEvent StatusChanged(mState)
Return True
End If
Catch e As Exception ' error while connecting
RaiseEvent CommError(e)
'not connected
mState = TCPIP.Status.Disconnected
RaiseEvent StatusChanged(mState)
Try
mTcpClient.Close()
Catch
End Try
End Try
End Function
Private Sub SocketIncomingMsg(ByVal arMsg As IAsyncResult)
Dim intCount As Integer
Dim lus As Int16
Dim myMessage As String = ""
Try
intCount = RnetworkStream.EndRead(arMsg)
If intCount < 1 Then
'socket closed
DisconnectSocket()
End If
Catch ex As Exception
End Try
For lus = 0 To intCount - 1
myMessage &= Chr(myReceivedData(lus))
Next
'process message in background
ThreadPool.QueueUserWorkItem(AddressOf Me.ProcessReceivedData,
CType(myMessage, Object))
'restart reading stream
Try
RnetworkStream.BeginRead(myReceivedData, 0,
myReceivedData.Length, New AsyncCallback(AddressOf SocketIncomingMsg),
RnetworkStream)
Catch ex As Exception
'THIS ONE OFTEN FAILES
'UNABLE TO READ DATA FROM THE TRANSPORT CONNECTION
DisconnectSocket()
End Try
End Sub
Private Sub ProcessReceivedData(ByVal Data As Object)
If CStr(Data) <> "" Then RaiseEvent DataReceived(CStr(Data))
End Sub
Public Sub DisconnectSocket()
Try
RnetworkStream.Close()
Catch ex As Exception
End Try
Try
mTcpClient.Close()
Catch ex As Exception
End Try
Try
mTcpClient = Nothing
Catch ex As Exception
End Try
mState = TCPIP.Status.Disconnected
RaiseEvent StatusChanged(mState)
End Sub
Public Sub SendSocketData(ByVal Data As String)
Try
Dim SnetworkStream As NetworkStream = mTcpClient.GetStream()
Dim sendBytes(Data.Length - 1) As Byte
Dim lus As Int16
For lus = 0 To Data.Length - 1
sendBytes(lus) = Asc(Data.Substring(lus, 1))
Next
SnetworkStream.Write(sendBytes, 0, sendBytes.Length)
Catch e As Exception
RaiseEvent CommError(e)
End Try
End Sub
End Class
Public Class TCPServerSock
'removed due to irrelevance
End Class
End Namespace