Windows Forms Receive and Display Data Code Example

The following example demonstrates receiving 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 Emulation for .NET

 

private void button1_Click(object sender, EventArgs e)
{
    //Receive data on a worker thread
    telnet1.Start(receiveData, null);
}

private void receiveData(object notUsed)
{
    //Receive data when it is sent by the remote host
    //Marshal it to the UI for display
    byte[] buffer = new byte[1024];
    while (telnet1.State != ConnectionState.Closed)
        telnet1.Marshal(telnet1.Read(buffer, 0, 1024), ""null);
}

private void telnet1_Data(object sender, DataEventArgs e)
{
    //Whenever data is received, display it
    if (e.Data != null) vt1.Write(e.Data.Buffer, 0, e.Data.Count);
}