Windows Forms Receive and Display Data Code Example
The following example demonstrates receiving session data in a Windows Forms environment. Data is read asynchronously, as it is available, without blocking the UI. The data is marshaled to the UI thread and written to the display.
< Back to all samples for PowerTCP SSH and SFTP for .NET
private void button1_Click(object sender, System.EventArgs e)
{
//Receive data on a worker thread
ssh1.Start(receiveData, null);
}
private void receiveData(object notUsed)
{
//Start a session
SessionStream session = ssh.StartShell();
//Receive data when it is sent by the remote host
//Marshal it to the UI for display
int count = -1;
byte[] buffer = new byte[1024];
while (count ! = 0)
{
count = session.Read(buffer, 0, buffer.Length);
ssh.Marshal(new Data(buffer, 0, count, ssh.Encoding), session, null);
}
}
private void ssh1_Data(object sender, SshDataEventArgs e)
{
//Whenever data is received, display it
//This event is raised on the UI thread
textBox1.AppendText(System.Text.Encoding.Default.GetString(e.Data.Buffer, 0, e.Data.Count));
}