PowerTCP SSH and SFTP for .NET
Marshal(Data,SessionStream,String,Object) Method
Example 




The data to be marshaled.
The SessionStream the data was read from.
Text to be marshalled.
State information to be marshaled.
Marshals data to the UI thread.
Syntax
'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)
public void Marshal( 
   Data data,
   SessionStream session,
   string message,
   object state
)
public: void Marshal( 
   Data* data,
   SessionStream* session,
   string* message,
   Object* state
) 
public:
void Marshal( 
   Data^ data,
   SessionStream^ session,
   String^ message,
   Object^ state
) 

Parameters

data
The data to be marshaled.
session
The SessionStream the data was read from.
message
Text to be marshalled.
state
State information to be marshaled.
Remarks

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.

Example
The example below demonstrates how to receive data without blocking the UI.
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
See Also

Reference

Ssh Class
Ssh Members
Overload List


PowerTCP SSH and SFTP for .NET Documentation Version 7.0
© 2023 Dart Communications. All Rights Reserved.
Send comments on this topic