欢迎来到天天文库
浏览记录
ID:27908703
大小:183.00 KB
页数:13页
时间:2018-12-07
《c#网络编程简单实现通信小例子》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、-1、主界面 .---2、源程序 SendpublicpartialclassformUdpSend:Form{//声明一个UdpClient对象UdpClientudpClient;publicformUdpSend(){InitializeComponent();//实例化UdpClient对象ud
2、pClient=newUdpClient();}privatevoidbtnSend_Click(objectsender,EventArgse){//获取发送信息http://mz.qqtop1.comstringmessage=this.txtMessage.Text;//获取发送数据byte[]data=System.Text.Encoding.UTF8.GetBytes(message);udpClient.Connect(IPAddress.Parse(this.txtIP.Text),12345);udpClient
3、.Send(data,data.Length);this.txtMessage.Text="";}}ReceivepublicpartialclassformUdpReceive:Form.---{UdpClientudpClient;Threadthread;publicformUdpReceive(){InitializeComponent();//不捕获对错误线程的调用CheckForIllegalCrossThreadCalls=false;udpClient=newUdpClient(12345);}//设置监听pub
4、licvoidListen(){IPEndPointiep=null;while(true){stringdata=System.Text.Encoding.UTF8.GetString(udpClient.Receive(refiep));this.lbReceive.Items.Add(data);}}privatevoidbtnReceive_Click(objectsender,EventArgse){thread=newThread(newThreadStart(Listen));thread.IsBackground
5、=true;thread.Start();}http://mingzi.78name.comprivatevoidformUdpReceive_Load(objectsender,EventArgse){}}1、主界面 .---2、源代码 ClientpublicpartialclassFormClient:F
6、orm{//声明一个SocketpublicSocketnewClient;//判断是否连接成功publicboolisConnected;//定义一个线程publicThreadmyThread;//定义一个委托publicdelegatevoidMyInvoke(stringstr);publicFormClient(){InitializeComponent();}.---privatevoidConnection(){//IPAddressip=null;//intport=888;byte[]data=newbyte[
7、1024];//创建一个新的SocketnewClient=newSocket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);//获取IP地址#regionGetIP//if(txtIP.Text==string.Empty)//{//MessageBox.Show("远程IP不能为空");//}//else//{//stringpattrn=@"((?:(?:25[0-5]
8、2[0-4]d
9、((1d{2})
10、([1-9]?d))).){3}
11、(?:25[0-5]
12、2[0-4]d
13、((1d{2})
14、([1-9]?d))))";//if(System.Text.RegularExpressions.Regex.IsMatch(txtIP.Text.Trim(),pattrn))//{//ip=IP
此文档下载收益归作者所有