//申明委托 public delegate void mydelegate(); //添加调用控制的方法 public void outtext() { textBox1.Text = Read_str;
} public void start_server() { while (true) { //接收从远程主机发送到本地8090端口的数据 byte recData = server.Receive(ref receivePoint); ASCIIEncoding encode = new ASCIIEncoding(); //获得客户端请求数据 Read_str = encode.GetString(recData); //提取客户端的信息,存放到定义为temp的字符串数组中 //string temp = Read_str.Split("/".ToCharArray()); //在线程中调用对象; mydelegate mydelegate = new mydelegate( outtext ); //BeginInvoke/invoke(同步或异步) 方法来将调用封送到适当的线程 textBox1.BeginInvoke(mydelegate, new object { });
} } public void run() { //利用本地8080端口号来初始化一个UDP网络服务 server = new UdpClient(port); receivePoint = new IPEndPoint(new IPAddress(ip), port); //开一个线程 startServer = new Thread(new ThreadStart(start_server)); //启动线程 startServer.Start();