CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2002
    Posts
    41

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

    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? One uses tcpclient & networkstream and the other uses socket.receive

    thanks for any advice



    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

  2. #2
    Join Date
    Jun 2001
    Location
    MO, USA
    Posts
    2,868
    I really can't say which is "better" but looks like method B is a few lines less code, so I'd go with it until you have problems or run into issues.

  3. #3
    Join Date
    Jun 2005
    Posts
    1

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

    hi there

    for me both codes looks great.

    ACTUALLY i need your help i developing a program in which i can connect through TELNET. then be able to enter user name and PWD. once i get in i need to run some commonds and have to collect the results of those command.

    on the basis of those results i have to show some out put.
    can you plz help me in authantication of user name and PWD?

    lastly i would be interested in to find out which above method puts less burdon on the server machine

    thanks and cheers

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured