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:



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.


Answer this question

TcpClient that won't receive; self destructing

  • Graham H

    Can you increase the timeout for the client

    Daniel Roth

  • SimonDowling

    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.

  • FrozenCow

    Increasing the timeout fixed the issue. Thanks for your help!

  • Mark Pearl

    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>


  • not-bob

    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



  • Krop

    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>

  • TcpClient that won't receive; self destructing