Which method to access Telnet data? Socket or TcpClient & Networkstream?

  • Thread starter Thread starter cgchris99
  • Start date Start date
C

cgchris99

Guest
I am trying to figure out which is the fastest and most efficient method to become the client to receive information from the host via port 23.

I have tested both methods and they seem to both work.
Why should I use one over the other? TcpClient & networkstream or Socket.receive.

Thanks for any help


Method A:
'-----------------------------------------------------------------------
Dim ASCII As Encoding = Encoding.ASCII
Dim StrGet As String = "GVC000"
Dim ByteGet As Byte() = ASCII.GetBytes(StrGet)
Dim RecvBytes(256) As Byte
Dim strRetPage As String = Nothing
Dim hostadd As IPAddress = IPAddress.Parse("192.168.0.3")
Dim ephost As New IPEndPoint(hostadd, 23)
Dim s As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
s.Connect(ephost)
If Not s.Connected Then
MsgBox("Unable to connect")
End If
s.Send(ByteGet, ByteGet.Length, 0)

Dim bytes As Int32 = s.Receive(RecvBytes, RecvBytes.Length, 0)
strRetPage = "Data Recvd: "
strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, bytes)
While bytes > 0
bytes = s.Receive(RecvBytes, RecvBytes.Length, 0)
strRetPage = "Data Recvd: "
strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, bytes)
End While


MethodB
'-----------------------------------------------------------------------
Dim tcpClient1 as New TcpClient()
Dim NS as NetworkStream
Dim bytes(tcpclient1.RecieveBufferSize) as byte
Dim hostaddr1 as IPAddress = IPAddress.Parse("192.168.0.3")
Dim MyEndPoint1 as New IPEndPoint(hostaddr1,23)

tcpClient1.Connect(MyEndPoint1)
NS = tcpClient1.GetStream
ReDim bytes(tcpClient1.ReceiveBufferSize)
BuffSize = CInt(tcpClient1.ReceiveBufferSize)
If ns.DataAvailable then
NS.Read(bytes, 0, BuffSize)
Dim Buff2 As String = Encoding.ASCII.GetString(bytes)
end if
 
NetworkStream is a bit easier to work with, as you don't have to worry about buffer sizes as much as you need to with Socket.Receive. This is the major benefit of all streams, which you'll discover once you get accustomed to the framework a bit more.
 
Back
Top