M
Mike Dole
I'm working on a client - server application based on the 'How to
Sockets Server and How to Sockets Client' code from the Visual Basic
..NET Resource Kit.
Since I want to be able to send 'big strings' instead of 'one liners'
I check the streams for terminators.
I'm having problems with the connection, I've been looking and
debugging for 2 weeks now (debugging with an emulator is terribly
slow..) but I'm not getting it...
I want to send xml strings back and forth between client and server
and on the side I have the original chat application to check the
connection.
I have a userconnection class with a memorystream (streamread). When
there's a terminator I process the memorystream, remove the terminator
in the process and raise an event (RaiseEvent LineReceived(Me,
strMessage))
I don't know why it is happening but it seems like the userconnection
{DigiServer.UserConnection}
LineReceivedEvent:
{DigiServer.UserConnection.LineReceivedEventHandler}
MainClient: {System.Net.Sockets.TcpClient}
Name: Nothing
READ_BUFFER_SIZE: 255
readBuffer: {Length=256}
streamRead: {System.IO.MemoryStream}
strName: Nothing
is 'nameless' the second time, the first time when it's connecting it
seems to go ok, the userconnection gets the name of the device which
connects (handheld1) but the second time the userconnection's name =
nothing? (should be handheld1..)
I'm keep getting errors like these:
System.IO.IOException - Unable to read data from the transport
connection - The IASyncResult object was not returned from the
corresponding asynchronous method on this class
Anyway here are the main 'snippets' the error mostly occurs where I
put **ERROR TRAP**:
Class Userconnection part..
' Overload the New operator to set up a read thread.
Public Sub New(ByVal client As TcpClient)
Me.MainClient = client
' This starts the asynchronous read thread. The data will be
saved into
' readBuffer.
Me.MainClient.GetStream.BeginRead(readBuffer, 0,
READ_BUFFER_SIZE, AddressOf StreamReceiver, Nothing)
' Me.client.ReceiveTimeout = 1000
End Sub
Private MainClient As TcpClient
Public Event LineReceived(ByVal sender As UserConnection, ByVal
Data As String)
' This is the callback function for TcpClient.GetStream.Begin. It
begins an
' asynchronous read from a stream.
Private Sub StreamReceiver(ByVal ar As IAsyncResult)
Dim BytesRead As Integer
Try
' Ensure that no other threads try to use the stream at
the same time.
SyncLock MainClient.GetStream
BytesRead = MainClient.GetStream.EndRead(ar)
End SyncLock
If BytesRead > 0 Then
streamRead.Write(readBuffer, 0, BytesRead)
If Network.CheckForTerminator(streamRead.ToArray()) =
True Then
ProcessCommand(streamRead)
End If
End If
' Ensure that no other threads try to use the stream at
the same time.
SyncLock MainClient.GetStream
' Start a new asynchronous read into readBuffer.
**ERROR TRAP** MainClient.GetStream.BeginRead(readBuffer, 0,
READ_BUFFER_SIZE, AddressOf StreamReceiver, Nothing)
End SyncLock
Catch SocketError As SocketException
MsgBox(SocketError.ToString)
End Try
End Sub
If BytesRead > 0 Then
streamRead.Write(readBuffer, 0, BytesRead)
If Network.CheckForTerminator(streamRead.ToArray()) =
True Then
ProcessCommand(streamRead)
End If
End If
SyncLock MainClient.GetStream
' Start a new asynchronous read into readBuffer.
MainClient.GetStream.BeginRead(readBuffer, 0, READ_BUFFER_SIZE,
AddressOf StreamReceiver, Nothing)
End SyncLock
End Sub
' Process the command that was received from the client.
Private Sub ProcessCommand(ByVal streamRead As MemoryStream)
Dim strMessage As String
Try
' remove message terminator
streamRead.SetLength((streamRead.Length -
Network.Terminator.Length))
' get the command data
streamRead.Position = 0
Dim data As Byte() = streamRead.ToArray()
' Convert the byte array the message was saved into, minus
one for the
' Chr(13).
strMessage = System.Text.Encoding.ASCII.GetString(data)
RaiseEvent LineReceived(Me, strMessage)
'Maak streamreader leeg
streamRead.SetLength(0)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
MainForm part..
Private Sub DoListen()
Try
' Listen for new connections.
listener = New TcpListener(System.Net.IPAddress.Any,
PORT_NUM)
listener.Start()
Do
' Create a new user connection using TcpClient
returned by
' TcpListener.AcceptTcpClient()
Dim MFclient As New
UserConnection(listener.AcceptTcpClient)
' Create an event handler to allow the UserConnection
to communicate
' with the window.
AddHandler MFclient.LineReceived, AddressOf
OnLineReceived
'UpdateStatus("New connection found: waiting for
log-in")
Loop Until False
Catch ex As SocketException
MsgBox(ex.ToString)
End Try
End Sub
Private Sub MainForm_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
listenerThread = New Threading.Thread(AddressOf DoListen)
listenerThread.Start()
UpdateStatus("Listener started")
End Sub
Thanks a million in advance!
Mike
Sockets Server and How to Sockets Client' code from the Visual Basic
..NET Resource Kit.
Since I want to be able to send 'big strings' instead of 'one liners'
I check the streams for terminators.
I'm having problems with the connection, I've been looking and
debugging for 2 weeks now (debugging with an emulator is terribly
slow..) but I'm not getting it...
I want to send xml strings back and forth between client and server
and on the side I have the original chat application to check the
connection.
I have a userconnection class with a memorystream (streamread). When
there's a terminator I process the memorystream, remove the terminator
in the process and raise an event (RaiseEvent LineReceived(Me,
strMessage))
I don't know why it is happening but it seems like the userconnection
{DigiServer.UserConnection}
LineReceivedEvent:
{DigiServer.UserConnection.LineReceivedEventHandler}
MainClient: {System.Net.Sockets.TcpClient}
Name: Nothing
READ_BUFFER_SIZE: 255
readBuffer: {Length=256}
streamRead: {System.IO.MemoryStream}
strName: Nothing
is 'nameless' the second time, the first time when it's connecting it
seems to go ok, the userconnection gets the name of the device which
connects (handheld1) but the second time the userconnection's name =
nothing? (should be handheld1..)
I'm keep getting errors like these:
System.IO.IOException - Unable to read data from the transport
connection - The IASyncResult object was not returned from the
corresponding asynchronous method on this class
Anyway here are the main 'snippets' the error mostly occurs where I
put **ERROR TRAP**:
Class Userconnection part..
' Overload the New operator to set up a read thread.
Public Sub New(ByVal client As TcpClient)
Me.MainClient = client
' This starts the asynchronous read thread. The data will be
saved into
' readBuffer.
Me.MainClient.GetStream.BeginRead(readBuffer, 0,
READ_BUFFER_SIZE, AddressOf StreamReceiver, Nothing)
' Me.client.ReceiveTimeout = 1000
End Sub
Private MainClient As TcpClient
Public Event LineReceived(ByVal sender As UserConnection, ByVal
Data As String)
' This is the callback function for TcpClient.GetStream.Begin. It
begins an
' asynchronous read from a stream.
Private Sub StreamReceiver(ByVal ar As IAsyncResult)
Dim BytesRead As Integer
Try
' Ensure that no other threads try to use the stream at
the same time.
SyncLock MainClient.GetStream
BytesRead = MainClient.GetStream.EndRead(ar)
End SyncLock
If BytesRead > 0 Then
streamRead.Write(readBuffer, 0, BytesRead)
If Network.CheckForTerminator(streamRead.ToArray()) =
True Then
ProcessCommand(streamRead)
End If
End If
' Ensure that no other threads try to use the stream at
the same time.
SyncLock MainClient.GetStream
' Start a new asynchronous read into readBuffer.
**ERROR TRAP** MainClient.GetStream.BeginRead(readBuffer, 0,
READ_BUFFER_SIZE, AddressOf StreamReceiver, Nothing)
End SyncLock
Catch SocketError As SocketException
MsgBox(SocketError.ToString)
End Try
End Sub
If BytesRead > 0 Then
streamRead.Write(readBuffer, 0, BytesRead)
If Network.CheckForTerminator(streamRead.ToArray()) =
True Then
ProcessCommand(streamRead)
End If
End If
SyncLock MainClient.GetStream
' Start a new asynchronous read into readBuffer.
MainClient.GetStream.BeginRead(readBuffer, 0, READ_BUFFER_SIZE,
AddressOf StreamReceiver, Nothing)
End SyncLock
End Sub
' Process the command that was received from the client.
Private Sub ProcessCommand(ByVal streamRead As MemoryStream)
Dim strMessage As String
Try
' remove message terminator
streamRead.SetLength((streamRead.Length -
Network.Terminator.Length))
' get the command data
streamRead.Position = 0
Dim data As Byte() = streamRead.ToArray()
' Convert the byte array the message was saved into, minus
one for the
' Chr(13).
strMessage = System.Text.Encoding.ASCII.GetString(data)
RaiseEvent LineReceived(Me, strMessage)
'Maak streamreader leeg
streamRead.SetLength(0)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
MainForm part..
Private Sub DoListen()
Try
' Listen for new connections.
listener = New TcpListener(System.Net.IPAddress.Any,
PORT_NUM)
listener.Start()
Do
' Create a new user connection using TcpClient
returned by
' TcpListener.AcceptTcpClient()
Dim MFclient As New
UserConnection(listener.AcceptTcpClient)
' Create an event handler to allow the UserConnection
to communicate
' with the window.
AddHandler MFclient.LineReceived, AddressOf
OnLineReceived
'UpdateStatus("New connection found: waiting for
log-in")
Loop Until False
Catch ex As SocketException
MsgBox(ex.ToString)
End Try
End Sub
Private Sub MainForm_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
listenerThread = New Threading.Thread(AddressOf DoListen)
listenerThread.Start()
UpdateStatus("Listener started")
End Sub
Thanks a million in advance!
Mike