473,587 Members | 2,227 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Use rConnection}
LineReceivedEve nt:
{DigiServer.Use rConnection.Lin eReceivedEventH andler}
MainClient: {System.Net.Soc kets.TcpClient}
Name: Nothing
READ_BUFFER_SIZ E: 255
readBuffer: {Length=256}
streamRead: {System.IO.Memo ryStream}
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.IOExc eption - 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.G etStream.BeginR ead(readBuffer, 0,
READ_BUFFER_SIZ E, AddressOf StreamReceiver, Nothing)
' Me.client.Recei veTimeout = 1000
End Sub

Private MainClient As TcpClient

Public Event LineReceived(By Val sender As UserConnection, ByVal
Data As String)
' This is the callback function for TcpClient.GetSt ream.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.GetS tream
BytesRead = MainClient.GetS tream.EndRead(a r)
End SyncLock

If BytesRead > 0 Then
streamRead.Writ e(readBuffer, 0, BytesRead)
If Network.CheckFo rTerminator(str eamRead.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.GetS tream
' Start a new asynchronous read into readBuffer.
**ERROR TRAP** MainClient.GetS tream.BeginRead (readBuffer, 0,
READ_BUFFER_SIZ E, AddressOf StreamReceiver, Nothing)
End SyncLock
Catch SocketError As SocketException
MsgBox(SocketEr ror.ToString)
End Try
End Sub
If BytesRead > 0 Then
streamRead.Writ e(readBuffer, 0, BytesRead)
If Network.CheckFo rTerminator(str eamRead.ToArray ()) =
True Then
ProcessCommand( streamRead)
End If
End If

SyncLock MainClient.GetS tream
' Start a new asynchronous read into readBuffer.
MainClient.GetS tream.BeginRead (readBuffer, 0, READ_BUFFER_SIZ E,
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.SetL ength((streamRe ad.Length -
Network.Termina tor.Length))

' get the command data
streamRead.Posi tion = 0
Dim data As Byte() = streamRead.ToAr ray()

' Convert the byte array the message was saved into, minus
one for the
' Chr(13).
strMessage = System.Text.Enc oding.ASCII.Get String(data)
RaiseEvent LineReceived(Me , strMessage)
'Maak streamreader leeg
streamRead.SetL ength(0)

Catch ex As Exception
MsgBox(ex.ToStr ing)
End Try
End Sub

MainForm part..

Private Sub DoListen()
Try
' Listen for new connections.
listener = New TcpListener(Sys tem.Net.IPAddre ss.Any,
PORT_NUM)
listener.Start( )
Do
' Create a new user connection using TcpClient
returned by
' TcpListener.Acc eptTcpClient()
Dim MFclient As New
UserConnection( listener.Accept TcpClient)

' Create an event handler to allow the UserConnection
to communicate
' with the window.
AddHandler MFclient.LineRe ceived, AddressOf
OnLineReceived
'UpdateStatus(" New connection found: waiting for
log-in")
Loop Until False
Catch ex As SocketException
MsgBox(ex.ToStr ing)
End Try
End Sub
Private Sub MainForm_Load(B yVal sender As Object, ByVal e As
System.EventArg s) Handles MyBase.Load
listenerThread = New Threading.Threa d(AddressOf DoListen)
listenerThread. Start()
UpdateStatus("L istener started")
End Sub
Thanks a million in advance!

Mike
Jul 21 '05 #1
4 4418

"Mike Dole" <m_******@hotma il.com> wrote in message
news:fd******** *************** **@posting.goog le.com...
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

Jul 21 '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
Jul 21 '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_******@hotmai l.com (Mike Dole) wrote in message news:<fd******* *************** ***@posting.goo gle.com>...
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.Use rConnection}
LineReceivedEve nt:
{DigiServer.Use rConnection.Lin eReceivedEventH andler}
MainClient: {System.Net.Soc kets.TcpClient}
Name: Nothing
READ_BUFFER_SIZ E: 255
readBuffer: {Length=256}
streamRead: {System.IO.Memo ryStream}
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.IOExc eption - 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.G etStream.BeginR ead(readBuffer, 0,
READ_BUFFER_SIZ E, AddressOf StreamReceiver, Nothing)
' Me.client.Recei veTimeout = 1000
End Sub

Private MainClient As TcpClient

Public Event LineReceived(By Val sender As UserConnection, ByVal
Data As String)
' This is the callback function for TcpClient.GetSt ream.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.GetS tream
BytesRead = MainClient.GetS tream.EndRead(a r)
End SyncLock

If BytesRead > 0 Then
streamRead.Writ e(readBuffer, 0, BytesRead)
If Network.CheckFo rTerminator(str eamRead.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.GetS tream
' Start a new asynchronous read into readBuffer.
**ERROR TRAP** MainClient.GetS tream.BeginRead (readBuffer, 0,
READ_BUFFER_SIZ E, AddressOf StreamReceiver, Nothing)
End SyncLock
Catch SocketError As SocketException
MsgBox(SocketEr ror.ToString)
End Try
End Sub
If BytesRead > 0 Then
streamRead.Writ e(readBuffer, 0, BytesRead)
If Network.CheckFo rTerminator(str eamRead.ToArray ()) =
True Then
ProcessCommand( streamRead)
End If
End If

SyncLock MainClient.GetS tream
' Start a new asynchronous read into readBuffer.
MainClient.GetS tream.BeginRead (readBuffer, 0, READ_BUFFER_SIZ E,
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.SetL ength((streamRe ad.Length -
Network.Termina tor.Length))

' get the command data
streamRead.Posi tion = 0
Dim data As Byte() = streamRead.ToAr ray()

' Convert the byte array the message was saved into, minus
one for the
' Chr(13).
strMessage = System.Text.Enc oding.ASCII.Get String(data)
RaiseEvent LineReceived(Me , strMessage)
'Maak streamreader leeg
streamRead.SetL ength(0)

Catch ex As Exception
MsgBox(ex.ToStr ing)
End Try
End Sub

MainForm part..

Private Sub DoListen()
Try
' Listen for new connections.
listener = New TcpListener(Sys tem.Net.IPAddre ss.Any,
PORT_NUM)
listener.Start( )
Do
' Create a new user connection using TcpClient
returned by
' TcpListener.Acc eptTcpClient()
Dim MFclient As New
UserConnection( listener.Accept TcpClient)

' Create an event handler to allow the UserConnection
to communicate
' with the window.
AddHandler MFclient.LineRe ceived, AddressOf
OnLineReceived
'UpdateStatus(" New connection found: waiting for
log-in")
Loop Until False
Catch ex As SocketException
MsgBox(ex.ToStr ing)
End Try
End Sub
Private Sub MainForm_Load(B yVal sender As Object, ByVal e As
System.EventArg s) Handles MyBase.Load
listenerThread = New Threading.Threa d(AddressOf DoListen)
listenerThread. Start()
UpdateStatus("L istener started")
End Sub
Thanks a million in advance!

Mike

Jul 21 '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_******@hotmai l.com (Mike Dole) wrote in message news:<fd******* *************** ***@posting.goo gle.com>...
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.Use rConnection}
LineReceivedEve nt:
{DigiServer.Use rConnection.Lin eReceivedEventH andler}
MainClient: {System.Net.Soc kets.TcpClient}
Name: Nothing
READ_BUFFER_SIZ E: 255
readBuffer: {Length=256}
streamRead: {System.IO.Memo ryStream}
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.IOExc eption - 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.G etStream.BeginR ead(readBuffer, 0,
READ_BUFFER_SIZ E, AddressOf StreamReceiver, Nothing)
' Me.client.Recei veTimeout = 1000
End Sub

Private MainClient As TcpClient

Public Event LineReceived(By Val sender As UserConnection, ByVal
Data As String)
' This is the callback function for TcpClient.GetSt ream.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.GetS tream
BytesRead = MainClient.GetS tream.EndRead(a r)
End SyncLock

If BytesRead > 0 Then
streamRead.Writ e(readBuffer, 0, BytesRead)
If Network.CheckFo rTerminator(str eamRead.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.GetS tream
' Start a new asynchronous read into readBuffer.
**ERROR TRAP** MainClient.GetS tream.BeginRead (readBuffer, 0,
READ_BUFFER_SIZ E, AddressOf StreamReceiver, Nothing)
End SyncLock
Catch SocketError As SocketException
MsgBox(SocketEr ror.ToString)
End Try
End Sub
If BytesRead > 0 Then
streamRead.Writ e(readBuffer, 0, BytesRead)
If Network.CheckFo rTerminator(str eamRead.ToArray ()) =
True Then
ProcessCommand( streamRead)
End If
End If

SyncLock MainClient.GetS tream
' Start a new asynchronous read into readBuffer.
MainClient.GetS tream.BeginRead (readBuffer, 0, READ_BUFFER_SIZ E,
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.SetL ength((streamRe ad.Length -
Network.Termina tor.Length))

' get the command data
streamRead.Posi tion = 0
Dim data As Byte() = streamRead.ToAr ray()

' Convert the byte array the message was saved into, minus
one for the
' Chr(13).
strMessage = System.Text.Enc oding.ASCII.Get String(data)
RaiseEvent LineReceived(Me , strMessage)
'Maak streamreader leeg
streamRead.SetL ength(0)

Catch ex As Exception
MsgBox(ex.ToStr ing)
End Try
End Sub

MainForm part..

Private Sub DoListen()
Try
' Listen for new connections.
listener = New TcpListener(Sys tem.Net.IPAddre ss.Any,
PORT_NUM)
listener.Start( )
Do
' Create a new user connection using TcpClient
returned by
' TcpListener.Acc eptTcpClient()
Dim MFclient As New
UserConnection( listener.Accept TcpClient)

' Create an event handler to allow the UserConnection
to communicate
' with the window.
AddHandler MFclient.LineRe ceived, AddressOf
OnLineReceived
'UpdateStatus(" New connection found: waiting for
log-in")
Loop Until False
Catch ex As SocketException
MsgBox(ex.ToStr ing)
End Try
End Sub
Private Sub MainForm_Load(B yVal sender As Object, ByVal e As
System.EventArg s) Handles MyBase.Load
listenerThread = New Threading.Threa d(AddressOf DoListen)
listenerThread. Start()
UpdateStatus("L istener started")
End Sub
Thanks a million in advance!

Mike

Jul 21 '05 #5

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

Similar topics

4
2148
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 create a socket, bind it to a port, put it in listen mode, and then n sockets can connect to it. The code: Socket newSocket =...
6
6979
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
4
1592
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 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...
5
4753
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 set of sensor data per second to a socket server. The socket server will do two things: 1. write data into a file via bsddb; 2. forward the data to...
3
1708
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, sends some ping on a TCP level without sending any data to the socket directly. Because when i pull out cord from the ethernet card simulating network...
14
11880
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 doing something was really inefficient and could reduce 10 lines of code with 2, etc. For reading, I am using a TcpClient and I call ...
1
4451
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 (re)connection (hitting the submit button) then I start to recieve a an error: IOException:System.IO.IOException: Der kunne ikke læses data fra...
0
7915
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7843
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8205
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8339
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8220
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
3840
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3872
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2347
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1185
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.