如何在服务器表单文本框中显示数据? [英] How to display the data on server form textbox?

查看:78
本文介绍了如何在服务器表单文本框中显示数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我运行我的服务器succsessfully,但当我打印我以前在控制台屏幕上打印的数据,以解决我正在发生的事情,我想在文本框上显示所有,但它不显示,当我关闭连接时客户端显示所有信息。为什么会发生。?



这是我的代码;



< pre lang =c#> public void GetData()
{
Form1 f = new Form1();
string ipadd = getip();

IPAddress ipAd = IPAddress.Parse( 192.168.0.15); // 使用本地m / c IP地址,并在客户端中使用相同的
< span class =code-comment> // IPAddress ip = IPAddress.Parse(ipadd);
txtip.Text = ipAd.ToString ();
txtport.Text = 3030;
/ * 初始化监听器* /
TcpListener myList = new TcpListener(ipAd, 3030 );

/ * 在指定端口开始收听* /

myList.Start();

txtdata.Text = 服务器在端口3030运行...;
txtdata.Text = txtdata.Text + Environment.NewLine + 本地终点是: + myList.LocalEndpoint;
txtdata.Text = txtdata.Text + Environment.NewLine + 正在等待连接..... ;



Socket s = myList.AcceptSocket();

txtdata.Text = txtdata.Text + Environment.NewLine + 接受 + s.RemoteEndPoint;
// txtdata.Text =接受连接+ s.RemoteEndPoint;

}





当我在控制台上写数据时看看上面的代码它是否有效但是相同我想在txtdata(文本框)上打印,但它不打印,直到客户端连接关闭。

解决方案

你应该在后台线程中运行通信部分使用invoke方法更新Form。请参阅从另一个线程更新表单,而无需为每种类型创建代表更新 [ ^ ]。

I run my server succsessfully but when i prints data which i previously printed in console screen to address me what is going on , i want to display all that on text box , but it does not display, and when i close connection from client it displays all that information. why it happens .?

Here is my code ;

public void GetData()
 {
   Form1 f = new Form1();
   string ipadd = getip();

   IPAddress ipAd = IPAddress.Parse("192.168.0.15"); //use local m/c IP   address, and use the same in the client
   // IPAddress ip = IPAddress.Parse(ipadd);
   txtip.Text = ipAd.ToString();
   txtport.Text = "3030";
   /* Initializes the Listener */
   TcpListener myList = new TcpListener(ipAd, 3030);

   /* Start Listeneting at the specified port */

   myList.Start();

   txtdata.Text = "The server is running at port 3030...";
   txtdata.Text = txtdata.Text + Environment.NewLine + "The local End point  is :" + myList.LocalEndpoint;
   txtdata.Text = txtdata.Text + Environment.NewLine + "Waiting for a   connection.....";



   Socket s = myList.AcceptSocket();

    txtdata.Text = txtdata.Text + Environment.NewLine +"Connection accepted from " + s.RemoteEndPoint;
   // txtdata.Text = "Connection accepted from " + s.RemoteEndPoint;

   }



Look at the code above when i write data on console it works but the same i want to print on txtdata (textbox) but it doesn't print , till connection close by client.

解决方案

You should run the communication part in a background thread and use the invoke method to update the Form. See Updating Your Form from Another Thread without Creating Delegates for Every Type of Update[^].


这篇关于如何在服务器表单文本框中显示数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆