Honestly I may be missing something entirely. It isn't an area of expertise for me. But here is an example of just the send side using tcp/ip.
Credit: Rimmel©
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
PublicClass Form1
' These vars can be formwide or local (in procedure) depending on how you want to use them.
Dim remoteIPAddress As IPAddress
Dim ep As IPEndPoint
Dim tnSocket As Socket
PrivateSub cmdSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSend.Click
SendCommands("127.0.0.1", "23")
EndSub
PrivateSub SendCommands(ByVal PIPAddress AsString, ByVal PPort AsString)
' LCommand: Actual command we are going to send through telnet
' Quite often this is the password or login (or both)
Dim Command AsString = "99"' Example command only - you need to use your own
' LRecvString: data returned from the telnet socet
Dim RecvString AsString = String.Empty
' NumBytes: Number of bytes return from telnet socket (count)
Dim NumBytes AsInteger = 0
' Get the IP Address and the Port and create an IPEndpoint (ep)
remoteIPAddress = IPAddress.Parse(PIPAddress.Trim)
ep = New IPEndPoint(remoteIPAddress, CType(PPort.Trim, Integer))
' Set the socket up (type etc)
tnSocket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
' Convert the ASCII command into bytes, adding a line termination on (vbCrLf)
Dim SendBytes As [Byte]() = Encoding.ASCII.GetBytes(Command & vbCrLf)
' Create a byte array for recieving bytes from the telnet socket
Dim RecvBytes(255) As [Byte]
Try
' Connect
tnSocket.Connect(ep)
Catch oEX As SocketException
' error
' You will need to do error cleanup here e.g killing the socket
' and exiting the procedure.
ExitSub
EndTry
' If we get to here then all seems good (we are connected)
Try
' Wait a few seconds (3) (depending on connection) telnet can be slow.
Wait(3000)
' Double check we are connected
If tnSocket.Connected Then
' Send the command
tnSocket.Send(SendBytes, SendBytes.Length, SocketFlags.None)
' -------------------------------------------------------------------
' The below "do loop" is not actually needed. This loop is used to
' Provide feedback for the commands you issue. e.g. like a hyperterm
' window. If you simply want to send command with no feedback rem it out
' -------------------------------------------------------------------
' loop getting 256 bytes of data from telnet socket at a time
Do
' RecvBytes with contain 256 bytes if data returned
' numbytes with have the count of bytes returned
NumBytes = tnSocket.Receive(RecvBytes, RecvBytes.Length, 0)
RecvString = RecvString + Encoding.ASCII.GetString(RecvBytes, 0, NumBytes)
LoopWhile NumBytes = 256 ' if less than 256 we are at the end of the recv stream
' Send recieved bytes to the output text box
txtRecv.Text = RecvString
' -------------------------------------------------------------------
Wait(1000)
' Disconnect
tnSocket.Disconnect(False)
EndIf
Catch oEX As Exception
' Error cleanup etc needed
EndTry
' Cleanup
remoteIPAddress = Nothing
ep = Nothing
tnSocket = Nothing
Command = Nothing
RecvString = Nothing
EndSub
PrivateSub Wait(ByVal PMillseconds AsInteger)
' Function created to replace thread.sleep()
' Provides responsive main form without using threading.
Dim TimeNow As DateTime
Dim TimeEnd As DateTime
Dim StopFlag AsBoolean
TimeEnd = Now()
TimeEnd = TimeEnd.AddMilliseconds(PMillseconds)
StopFlag = False
WhileNot StopFlag
TimeNow = Now()
If TimeNow > TimeEnd Then
StopFlag = True
EndIf
Application.DoEvents()
EndWhile
' Cleanup
TimeNow = Nothing
TimeEnd = Nothing
EndSub