Dart.Ssh Namespace > Ssh Class > Marshal Method : Marshal(Data,SessionStream,String,Object) Method |
'Declaration Public Overloads Sub Marshal( _ ByVal data As Data, _ ByVal session As SessionStream, _ ByVal message As String, _ ByVal state As Object _ )
'Usage Dim instance As Ssh Dim data As Data Dim session As SessionStream Dim message As String Dim state As Object instance.Marshal(data, session, message, state)
This method is used to marshal data from a worker thread back to the UI thread for typical display purposes. It calls the OnData method, which raises the Data event.
private void button1_Click(object sender, System.EventArgs e) { //Connect and log into the server. Can be executed on a worker //thread to not block the UI; executed synchronously here for simplicity connectAndLogin(myServerHostname, myServerPort, myUsername, myPassword); //Execute the specified 'receiveData' method on a worker thread to not block the UI ssh1.Start(receiveData, null); } private void connectAndLogin(string hostname, int port, string username, string password) { //Connect to the server ssh1.Connection.RemoteEndPoint.HostNameOrAddress = hostname; ssh1.Connection.RemoteEndPoint.Port = port; //Usually 22 ssh1.Connect(); //Authenticate SshLoginData loginDetails = new SshLoginData(); loginDetails.Username = username; loginDetails.Password = password; ssh1.Authenticate(loginDetails); } /// <summary> /// Receives data from the server and marshals it to the UI thread for display. /// </summary> /// <param name="notUsed">Not used in this snippet</param> private void receiveData(object notUsed) { //Establish a shell SessionStream session = ssh1.StartShell(); //Receive data when it is sent by the remote host //Marshal it to the UI for display while (true) { byte[] buf = new byte[1024]; Data data = session.Read(buf); //If data.Count is 0, the connection has been closed, so break out of the loop. if (data.Count == 0) break; ssh1.Marshal(data, session, "", null); } } /// <summary> /// Raised on the UI thread, when ssh1.Marshal is called in receiveData. /// </summary> private void ssh1_Data(object sender, SshDataEventArgs e) { textBox1.AppendText(e.Data.ToString()); }
Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) 'Connect and log into the server. Can be executed on a worker 'thread to not block the UI; executed synchronously here for simplicity connectAndLogin(myServerHostname, myServerPort, myUsername, myPassword) 'Execute the specified 'receiveData' method on a worker thread to not block the UI ssh1.Start(AddressOf receiveData, Nothing) End Sub Private Sub connectAndLogin(ByVal hostname As String, ByVal port As Integer, ByVal username As String, ByVal password As String) 'Connect to the server ssh1.Connection.RemoteEndPoint.HostNameOrAddress = hostname ssh1.Connection.RemoteEndPoint.Port = port 'Usually 22 ssh1.Connect() 'Authenticate Dim loginDetails As New SshLoginData() loginDetails.Username = username loginDetails.Password = password ssh1.Authenticate(loginDetails) End Sub ''' <summary> ''' Receives data from the server and marshals it to the UI thread for display. ''' </summary> ''' <param name="notUsed">Not used in this snippet</param> Private Sub receiveData(ByVal notUsed As Object) 'Establish a shell Dim session As SessionStream = ssh1.StartShell() 'Receive data when it is sent by the remote host 'Marshal it to the UI for display Do Dim buf(1023) As Byte Dim data As Data = session.Read(buf) 'If data.Count is 0, the connection has been closed, so break out of the loop. If data.Count = 0 Then Exit Do End If ssh1.Marshal(data, session, "", Nothing) Loop End Sub ''' <summary> ''' Raised on the UI thread, when ssh1.Marshal is called in receiveData. ''' </summary> Private Sub ssh1_Data(ByVal sender As Object, ByVal e As SshDataEventArgs) Handles ssh1.Data textBox1.AppendText(e.Data.ToString()) End Sub