Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have console application which gets the data from the client using sockets, now i want display that data in the text box or label in the windows form application.
So this the code
i like to display new windows form applications that will display the data in the form control like text box or label..etc.,

 int Port = 8088;
 TcpListener server = new TcpListener(IPAddress.Any, Port);
 server.Start(); 
 byte[] bytes = new byte[1024];
 string data;
 Console.Write("Waiting for Client connection ");
  TcpClient client = server.AcceptTcpClient();
  Console.WriteLine("\n Client Connected");
   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);
 Console.WriteLine(string.Format("Data Received :{0}", data));
  client.Close();
Posted

1 solution

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:
C#
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;
    //Console.Write("Waiting for Client connection ");
    backgroundWorker1.ReportProgress(25);

    TcpClient client = server.AcceptTcpClient();
    //Console.WriteLine("\n Client Connected");
    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;
    }
}
 
Share this answer
 
v6
Comments
Jason Gleim 15-Apr-15 13:56pm    
You might want to also hook the form closing event and call backgroundWorker1.Abort so that the app doesn't hang when the user closes it.
Florian Braun 16-Apr-15 2:08am    
right, good Idea
Srikanth59 16-Apr-15 2:32am    
This is good idea but am getting errors
1. void Button1Click(object sender, EventArgs e)
{
backgroundWorker1.DoWork();
}
Error is The Event ComponentModel.BackgroundWorker.DoWork can appear on the left hand side of += or -=
2.void BackgroundWorker1RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
{
listBox1.Items.Add("Data Received :{0}", (string)e.Result);
}
}

Error is NO Overload for Method Add takes 2 arguments.
Florian Braun 16-Apr-15 3:07am    
also you need to set the property of the backgroundworker "WorkerReportsProgress" and "WorkerSUpportsCancellation" both to true
Srikanth59 16-Apr-15 3:33am    
yes i set both the property to true but am getting some more errors in the listbox
An exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll but was not handled in user code
switch (e.ProgressPercentage)
{
case 25:
listBox1.Items.Add("Waiting for Client connection ");
break;
case 50:
listBox1.Items.Add("Client Connected");
break;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900