473,395 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Sockets Server / Sockets Client - unable to read data from the transport connection

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.LineReceivedEventHandle r}
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
Nov 20 '05 #1
4 1584

"Mike Dole" <m_******@hotmail.com> wrote in message
news:fd*************************@posting.google.co m...
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.

Is there a reason you're writing all your socket handling code from scratch?
If the client and server are both .Net I would recommend you use remoting;
otherwise consider SOAP

Andy

Nov 20 '05 #2
> Is there a reason you're writing all your socket handling code from scratch?
If the client and server are both .Net I would recommend you use remoting;
otherwise consider SOAP

Andy


No particular reason, I've read some examples with the tcpClient and
tcpListener Class and I had it looked pretty straightforward...
But thanks for your advice I'm gonna take a look at remoting / SOAP.

Regards,

Michael
Nov 20 '05 #3
The strange thing is that with a desktop client with exactly the same
code it runs just fine??

In other words, if I run the desktop version I can send xml strings,
text strings, etc back and forth without any error.

Isn't there anybody around who can shine a light on this??

Thanks in advance,

Michael
m_******@hotmail.com (Mike Dole) wrote in message news:<fd*************************@posting.google.c om>...
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.LineReceivedEventHandle r}
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

Nov 20 '05 #4
The strange thing is that with a desktop client with exactly the same
code it runs just fine??

In other words, if I run the desktop version I can send xml strings,
text strings, etc back and forth without any error.

Isn't there anybody around who can shine a light on this??

Thanks in advance,

Michael
m_******@hotmail.com (Mike Dole) wrote in message news:<fd*************************@posting.google.c om>...
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.LineReceivedEventHandle r}
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

Nov 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Mike Dole | last post by:
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...
4
by: 0to60 | last post by:
I have a question about socket programming in general. Exactly what happens behind the scenes when I one socket connects to a different socket in listen mode? Using the dotnet framework, I...
6
by: Laxmikant Rashinkar | last post by:
Is there any way to use a C# socket in promiscuous mode? Any sample code that shows how this is done? any assistance is much appreciated! thanks LK
5
by: zxo102 | last post by:
Hi, I am doing a small project using socket server and thread in python. This is first time for me to use socket and thread things. Here is my case. I have 20 socket clients. Each client send a...
3
by: OneMustFall | last post by:
Reciently i wrote a simple client (in twisted) using Reconnecting Factory. That client logins to my socket server.. and that`s it. Interesting thing is that it is seems that twisted client,...
14
by: eliss.carmine | last post by:
I'm using TCP/IP to send a Bitmap object over Sockets. This is my first time using C# at all so I don't know if this is the "right" way to do it. I've already found out several times the way I was...
1
by: larspeter | last post by:
Hi all. I have a problem with TcpClient ... I am conneting to a server with TcpClient and returning the answer through a webservice. It actully all works fine. BUT if I make a lot of...
6
by: 7stud | last post by:
My question pertains to this example: #!/usr/bin/env python import socket, sys, time host = sys.argv textport = sys.argv s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.