Skip to content

SSH.TCPListener

Andrew Lambert edited this page Jul 24, 2023 · 14 revisions

SSH.TCPListener

Class Declaration

 Protected Class TCPListener

Remarks

This class instructs the SSH server to begin listening for inbound TCP connections on the RemoteInterface and RemotePort. If RemoteInterface is the empty string then the server will listen on all of its interfaces. If RemotePort is zero then the server will select a random ephemeral port to listen on; the RemotePort property will be updated to reflect the selected port after StartListening() returns successfully.

While listening, newly established TCP connections will be passed to the ConnectionReceived() event.

Calling the StopListening() method will discontinue listening for new connections but will not disturb connections that are already established.

Example

This example is a console application that accepts connections forwarded from the server and echoes back anything sent over the connection:

Class App
Inherits ConsoleApplication
 Event Function Run(args() As String) As Integer
   Dim session As SSH.Session = SSH.Connect("ssh.example.com", 22, "username", "password")
   Dim listener As New SSH.TCPListener(session)
   AddHandler listener.ConnectionReceived, WeakAddressOf ConnectionReceivedHandler
   listener.RemotePort = 80 ' the port that the SSH server should listen on
   listener.RemoteInterface = "127.0.0.1" ' the network interface that the SSH server should listen on

   listener.StartListening()

   Do Until Not listener.IsListening
     ' we must periodically Poll the listener for activity
     ' Poll() will automatically call StopListening() if an error occurs.
     listener.Poll()
   Loop
 End Function

 Private Sub ConnectionReceivedHandler(Sender As SSH.TCPListener, Connection As SSH.TCPTunnel)
  #pragma Unused Sender
  
  ' For this example, simply echo back whatever is sent over the connection
  
  Dim data As String = Connection.Read(Connection.BytesReadable)
  Connection.Write(data)
  Connection.Close
 End Sub

End Class

Event definitions

Methods

Properties

See also

Clone this wiki locally