(this code is running on .NET v2 Beta 2)
I have this code that reads a NetworkStream from a TcpClient:
Public Sub Read(reader as StreamReader) Dim tmp As String = String.Empty Dim tries As Integer = 0 Do While tmp = String.Empty If reader.BaseStream.CanRead = True Then Try tmp = reader.ReadLine() If tmp <> "~~START~~" Then Throw New InvalidOperationException("Message received was invalid.") End If tmp = reader.ReadLine() If Left(tmp, "Type:".Length) <> "Type:" Then Throw New InvalidOperationException("Message received was invalid.") End If _type = Mid(tmp, "Type:".Length + 1) tmp = reader.ReadLine() Do While tmp <> "~~END~~" If Me.Contents <> String.Empty Then Me.Contents &= Environment.NewLine End If Me.Contents &= tmp tmp = reader.ReadLine() Loop Catch ex As Exception tries += 1 If tries = 3 Then Throw New IOException("Timed out waiting for a response.", ex) Else My.Application.Log.WriteEntry("Timed out waiting for a response, will retry, count: " & tries, TraceEventType.Warning) Threading.Thread.Sleep(1000) End If End Try Else Threading.Thread.Sleep(1000) End If Loop End Sub |
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.

TcpClient that won't receive; self destructing
alexanderpg
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.
Nurlan Manasbaev
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>
David Conti
Here's a link to the log file: http://korn.aspxconnection.com/myfiles/tcplog.txt
Joy2u
Daniel Roth
Jurassic
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>
JG53_Jaguar