Imports System.Net.Sockets
Imports System.Text
Class CTestTCPClient
Shared Sub Main()
Dim strHostName As String = "xxx.xxx.xxx.xxx"
Dim intPortNumber As Int32 = 9999
Dim tcpClient As New System.Net.Sockets.TcpClient
tcpClient.Connect(strHostName, intPortNumber)
Dim networkStream As NetworkStream = tcpClient.GetStream()
If networkStream.CanWrite And networkStream.CanRead Then
Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes("Is anybody listening...")
networkStream.Write(sendBytes, 0, sendBytes.Length)
' Read the NetworkStream into a byte buffer.
Dim bytes(tcpClient.ReceiveBufferSize) As Byte
Console.WriteLine("About to get buffer size")
' It hangs on this line:
networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
' Output the data received from the host to the console.
Dim returndata As String = Encoding.ASCII.GetString(bytes)
Console.WriteLine(("TCP Server returned: " + returndata))
Else
If Not networkStream.CanRead Then
Console.WriteLine("Could not write data to data stream")
tcpClient.Close()
Else
If Not networkStream.CanWrite Then
Console.WriteLine("Could not read data from data stream")
tcpClient.Close()
End If
End If
End If
' Pause to let the user view the console output.
Console.ReadLine()
End Sub
End Class