locked
TCP Connection on form load RRS feed

  • Question

  • I am new to the Visual Studio environment. I have managed to figure out how to send

    commands but how do you initiate a TCP connection? I have used AxWinsock in the past.

    I am trying to send string commands to a video switcher. It is basically a Telnet connection.

    Thursday, June 12, 2014 3:38 AM

Answers

  • I am new to the Visual Studio environment. I have managed to figure out how to send

    commands but how do you initiate a TCP connection? I have used AxWinsock in the past.

    I am trying to send string commands to a video switcher. It is basically a Telnet connection.

    Hi,

    I would suggest you check the .NET class library TcpClient Class (System.Net.Sockets)which provides simple methods for connecting, sending, and receiving stream data over a network in synchronous blocking mode.

    Shared Sub Connect(server As [String], message As [String])
       Try 
          ' Create a TcpClient. 
          ' Note, for this client to work you need to have a TcpServer  
          ' connected to the same address as specified by the server, port 
          ' combination. 
          Dim port As Int32 = 13000
          Dim client As New TcpClient(server, port)
    
          ' Translate the passed message into ASCII and store it as a Byte array. 
          Dim data As [Byte]() = System.Text.Encoding.ASCII.GetBytes(message)
    
          ' Get a client stream for reading and writing. 
          '  Stream stream = client.GetStream(); 
          Dim stream As NetworkStream = client.GetStream()
    
          ' Send the message to the connected TcpServer. 
          stream.Write(data, 0, data.Length)
    
          Console.WriteLine("Sent: {0}", message)
    
          ' Receive the TcpServer.response. 
          ' Buffer to store the response bytes.
          data = New [Byte](256) {}
    
          ' String to store the response ASCII representation. 
          Dim responseData As [String] = [String].Empty
    
          ' Read the first batch of the TcpServer response bytes. 
          Dim bytes As Int32 = stream.Read(data, 0, data.Length)
          responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes)
          Console.WriteLine("Received: {0}", responseData)
    
          ' Close everything.
          stream.Close()
          client.Close()
       Catch e As ArgumentNullException
          Console.WriteLine("ArgumentNullException: {0}", e)
       Catch e As SocketException
          Console.WriteLine("SocketException: {0}", e)
       End Try
    
       Console.WriteLine(ControlChars.Cr + " Press Enter to continue...")
       Console.Read()
    End Sub 'Connect

    You could refer to these samples to get better understanding.

    http://code.msdn.microsoft.com/windowsdesktop/TCP-Communication-in-VBNET-f6c48ca0

    http://www.codeproject.com/Articles/7805/TCP-IP-with-VB-NET

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Carl Cai Thursday, June 19, 2014 9:45 AM
    Friday, June 13, 2014 6:50 AM