Dim tcpClient As New TcpClient()
' Uses the GetStream public method to return the NetworkStream.
Try
Dim networkStream As NetworkStream = tcpClient.GetStream()
If networkStream.CanWrite Then
Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes("Is anybody there?")
networkStream.Write(sendBytes, 0, sendBytes.Length)
Else
Console.WriteLine("You cannot write data to this stream.")
tcpClient.Close()
Return
End If
If networkStream.CanRead Then
' Reads the NetworkStream into a byte buffer.
Dim bytes(tcpClient.ReceiveBufferSize) As Byte
' Read can return anything from 0 to numBytesToRead.
' This method blocks until at least one byte is read.
networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
' Returns the data received from the host to the console.
Dim returndata As String = Encoding.ASCII.GetString(bytes)
Console.WriteLine(("This is what the host returned to you: " + returndata))
Else
Console.WriteLine("You cannot read data from this stream.")
tcpClient.Close()
Return
End If
Catch e As Exception
Console.WriteLine(e.ToString())
End Try