TcpClient that won't receive; self destructing

Hi,
(this code is running on .NET v2 Beta 2)
I have this code that reads a NetworkStream from a TcpClient:


PublicSub Read(readeras StreamReader)
Dim tmpAsString =String.Empty
Dim triesAsInteger = 0
DoWhile tmp =String.Empty
If reader.BaseStream.CanRead =TrueThen
Try
tmp = reader.ReadLine()
If tmp <> "~~START~~"Then
ThrowNew InvalidOperationException("Message received was invalid.")
EndIf
tmp = reader.ReadLine()
If Left(tmp, "Type:".Length) <> "Type:"Then
ThrowNew InvalidOperationException("Message received was invalid.")
EndIf
_type = Mid(tmp, "Type:".Length + 1)
tmp = reader.ReadLine()
DoWhile tmp <> "~~END~~"
IfMe.Contents <>String.EmptyThen
Me.Contents &= Environment.NewLine
EndIf
Me.Contents &= tmp
tmp = reader.ReadLine()
Loop
Catch exAs Exception
tries += 1
If tries = 3Then
ThrowNew IOException("Timed out waitingfor a response.", ex)
Else
My.Application.Log.WriteEntry("Timed out waitingfor a response, will retry, count: " & tries, TraceEventType.Warning)
Threading.Thread.Sleep(1000)
EndIf
EndTry
Else
Threading.Thread.Sleep(1000)
EndIf
Loop
EndSub


Every time it runs, the first call throws an IOException, and that is why it has the Try...Catch ex as Exception which then tries three times. Sometimes it throws a second exception and then works, sometimes it works after the first one. The server, which also uses this code, works OK every time without throwing an exception.
If it gets past that, it (the .NET framework) then disconnects the client, so I can't send a response back to the server.
[3625 byte] By [someguy] at [2008-1-12]
# 1
Hi,

What is your server app. Is it a TCP app or a Remote http server?
Also, can you please get us a log of the system.net code by putting the following configuration in [appname].exe.config. This will help us see whats happening and the reason for the IO Exceptions

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.diagnostics>
<sources>
<source name="System.Net" switchValue="Verbose">
<listeners>
<add name="SocketListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="System.Net.Sockets.Log" />
</listeners>
</source>
</sources>
</system.diagnostics>
</configuration>

mahjayar at 2007-8-21 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...
# 2
Thanks for your response!
The server is a TcpListener. For some reason, neither of the programs are generating anything in the log files. The client creates the log file but never writes anything to it, and the server never even creates the log file.
someguy at 2007-8-21 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...
# 3
Hi,
I made a mistake in the config file. The source name should be System.Net.Sockets
The correct config file is

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.diagnostics>
<sources>
<source name="System.Net.Sockets" switchValue="Verbose">
<listeners>
<add name="SocketListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="System.Net.Sockets.Log" />
</listeners>
</source>
</sources>
</system.diagnostics>
</configuration>

mahjayar at 2007-8-21 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...
# 4
I worked out what the problem was from looking at what information was outputted to the immediate window (the logs, strangely, didn't get anything, but I could see the information flying between the client and the server). The problem was that the server was taking a while generating an encryption key and so the client was timing out. Now the client can't send anything back to the server because an InvalidOperationException (The operation is not allowed on non-connected sockets.) is occuring. Do you have any ideas?
Here's a link to the log file: http://korn.aspxconnection.com/myfiles/tcplog.txt
someguy at 2007-8-21 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...
# 5
Can you increase the timeout for the client?

Daniel Roth

DanielRoth at 2007-8-21 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...
# 6
Increasing the timeout fixed the issue. Thanks for your help!
someguy at 2007-8-21 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...

.NET Development

Site Classified