Checking client closed in tcp connection
<p>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.</p>

<p>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.</p>

<p>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.</p>

<p>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.</p>

<p>Server:</p>

<pre><code>While True
    client.GetStream.Read(b, 0, 10)
    If b(0) &lt;&gt; 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
</code></pre>

<p>Client:</p>

<pre><code>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 &lt;&gt; "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
</code></pre>

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