I don't quite understand exactly how a few of the features are shared when a
Dim server As New TcpListener(localAddr, port)
Dim client As TcpClient = server.AcceptTcpClient()
Dim client As New TcpClient
The TCP protocol does not know what a
TcpClient is. This is a .NET concept. TCP does not reference .NET concepts at all. For that reason no objects will be sent across the wire.
The only thing that is sent is the bytes you explicitly write.
Each side has it's own isolated objects. Both sides use their own
TcpClient object which acts like a handle to the TCP connection.
This is not the proper shutdown sequence. The first line is redundant to the second and incomplete. Close should never be called. The best way to do it is to wrap the client in
using. The second best way is to call
Dispose on the client. The
Close methods in the BCL are historic accidents and should be ignored. They do the same thing that Dispose does in all cases that I ever looked at.
Don't touch the buffer sizes. They control how much memory the kernel uses to buffer data on your end of the connection. The kernel is capable of managing this by itself.
Also don't look at the buffer sizes in your code. They are meaningless. Also don't use the
DataAvailable property because if it returns
false/0 this does not mean that no data can be read.
Connected property is not necessarily synchronized on both sides. If the network goes down there can be no synchronization. Never look at the
Connected property. If it says
true the next nanosecond it could be
false. So it's not possible to make decisions based on that property. You do not need to test anything. Just Read/Write and handle the exceptions by aborting.
Regarding packets, you are not sending packets when you
Write. TCP has a boundaryless stream of bytes. The kernel packetizes your data internally. You do not need to split data into specific sizes. Just use a fairly big buffer size such as 8K (or more on fast networks). The write size is only about saving CPU time by being less chatty (assuming nagling is enabled).