1) create your your WinForm - application with 1 Button, a ListBox and a Backgroundworker
2) Add the Button_click-Event, the BackgroundWorker1DoWork-Event, the BackgroundWorker1RunWorkerCompleted-Event and the BackgroundWorker1ProgressChanged-Event
3) add this code:
void Button1Click(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
}
void BackgroundWorker1DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
int Port = 8088;
TcpListener server = new TcpListener(IPAddress.Any, Port);
server.Start();
byte[] bytes = new byte[1024];
string data;
backgroundWorker1.ReportProgress(25);
TcpClient client = server.AcceptTcpClient();
backgroundWorker1.ReportProgress(50);
NetworkStream stream = client.GetStream();
int i;
i = stream.Read(bytes, 0, bytes.Length);
data = Encoding.UTF8.GetString(bytes, 0, i);
data = data.Substring(2, data.Length - 2);
client.Close();server.Close();
e.Result=data;
}
void BackgroundWorker1RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
{
listBox1.Items.Add(String.Format("Data Received: {0}", (string)e.Result));
}
void BackgroundWorker1ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
{
switch (e.ProgressPercentage) {
case 25:
listBox1.Items.Add("Waiting for Client connection ");
break;
case 50:
listBox1.Items.Add("Client Connected");
break;
}
}