473,769 Members | 2,346 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 4436

"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
2168
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 = listeningSocket.Accept(); returns a socket. I can communicate on newSocket, and listeningSocket goes
6
6998
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
1601
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 weeks now (debugging with an emulator is terribly slow..) but I'm not getting it...
5
4770
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 a GUI written in wxpython. I am thinking the code should work as follow (not sure it is feasible)...
3
1724
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 falure, client in about 10-15 seconds determines that connection lost!! (pretty cool)
14
11922
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 NetworkStream ns = client.GetStream(); to get a stream stream.Read(buffer, 0, buffer.Length);
1
4470
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 transportforbindelsen (LPF: could not read data from the transport
0
9586
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10043
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9990
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9861
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8869
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5298
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3956
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

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.