-4

I'm very new to using sockets and TCP and I've made a mess of trying to determine whether or not the connection between my client and server has closed.

My current method is to change the first element of my byte array (b(10) simply for writing to and reading from the stream) to 0x0 server side when I end the connection, and then checking if the first byte of the array is 0x0 server side to see if the client is closed.

I'm very unhappy with this and ideally would like to just be able to check it's connection but I can't find any method of doing so.

Right now this is just used to send strings of a few characters from the client and print them out on the server window, but ideal I'd like to proceed from this when I understand more what I'm doing.

Server:

While True
    client.GetStream.Read(b, 0, 10)
    If b(0) <> Convert.ToByte(0) Then
        For i = 0 To 10
            Console.Write(Convert.ToChar(b(i)))
        Next i
        Console.Write(vbCrLf)
    Else
        Exit While
    End If
End While

Client:

If aString = "y" Then
    'Create a TcpClient.
    client = New TcpClient(localAddr.ToString, port)
    Do
        Array.Clear(b, 0, 10)
        b(0) = 1
        Console.Write("Ready to quit? (y/message)")
        aString = Console.ReadLine()
        If aString <> "y" Then
            For i = 0 To Len(aString.ToCharArray()) - 1
                Console.WriteLine(i)
                b(i) = Convert.ToByte(aString.ToCharArray.GetValue(i))
            Next i
            client.GetStream.Write(b, 0, 10)
        End If
    Loop Until aString = "y"
    b(0) = Convert.ToByte(0)
    client.GetStream.Write(b, 0, 10)
    client.GetStream.Close()
    client.Close()
End If

Can anyone provide details on how to do this in a less messy fashion, or point me to some resources that could help?

Nick is tired
  • 6,860
  • 20
  • 39
  • 51
  • 2
    `TcpClient.Connected` will return True/False if your connection has been closed using `TcpClient.Close()` (and no internet problems has occurred during the close). I also recommend reading [**this answer**](http://stackoverflow.com/a/35240061/3740093) when you are more into TCP. It provided a reliable way of handling messages. – Visual Vincent May 19 '16 at 05:22
  • I tried TcpClient.Connected but I thought returns true if the connection was ever made, it didn't matter if it was closed, turns out I needed to try to send data to the client as it only updates every io operation. – Nick is tired May 19 '16 at 12:00

1 Answers1

-1

Looking at the answers for How to check the connection state of a TCP Server (Socket) with TCP Client in VB.NET I have added the following function:

Public Function IsConnected(ByRef client As TcpClient) As Boolean
    Try
        Return Not (client.Client.Poll(-1, SelectMode.SelectRead) _
                 AndAlso client.Available = 0)
    Catch ex As Exception
        Return False
    End Try
End Function

Adjusted the server to:

While True
    If IsConnected(client) Then
        client.GetStream.Read(b, 0, 10)
        For i = 0 To 10
            Console.Write(Convert.ToChar(b(i)))
        Next i
        Console.Write(vbCrLf)
    Else
        Exit While
    End If
End While

And the client to:

If aString = "y" Then
    'Create a TcpClient.
    client = New TcpClient(localAddr.ToString, port)
    Do
        Array.Clear(b, 0, 10)
        Console.Write("Ready to quit? (y/message)")
        aString = Console.ReadLine()
        If aString <> "y" Then
            For i = 0 To Len(aString.ToCharArray()) - 1
                Console.WriteLine(i)
                b(i) = Convert.ToByte(aString.ToCharArray.GetValue(i))
            Next i
            client.GetStream.Write(b, 0, 10)
        End If
    Loop Until aString = "y"
    client.GetStream.Close()
    client.Close()
End If

And this is now working fine.

Community
  • 1
  • 1
Nick is tired
  • 6,860
  • 20
  • 39
  • 51