zoukankan      html  css  js  c++  java
  • 利用UDP进行信息传输

    页面中控件两个textBox,(txtBox1,TbxMessage)一个button(btnSend)

    //创建一个Thread实例  
            private Thread thread1;
            //创建一个UdpClient实例
            private UdpClient udpReceive;
            private UdpClient udpSend;
            private byte[] bytes;
            //private DialogResult result;
                   
            public myUdpClient()
            {
                InitializeComponent();
            }
            private void myUdpClient_Load(object sender, EventArgs e)
            {
                thread1 = new Thread(new ThreadStart(ReceiveMessage));
                thread1.Start();
                this.textBox1.Text = "221.221.146.63";
            }
            private void BtnSend_Click(object sender, EventArgs e)
            {
                //初始化UdpClient
                udpSend = new UdpClient();
                //实际使用时应将127.0.0.1改为服务器的远程IP
                IPAddress remoteIPAddress = IPAddress.Parse(this.textBox1.Text.Trim());
                IPEndPoint remoteIPEndPoint = new IPEndPoint(remoteIPAddress, 8001);
                //将发送内容转换为字节数组
                byte[] bytes = System.Text.Encoding.UTF8.GetBytes(TbxMessage.Text);
                //设置重传次数
                int retry = 0;
                while (true)
                {
                    try
                    {
                        udpSend.Send(bytes, bytes.Length, remoteIPEndPoint);
                        break;
                    }
                    catch (SocketException se)
                    {
                        if (retry < 3)
                        {
                            retry++; continue;
                        }
                        else
                        {
                            DialogResult result = MessageBox.Show("发送失败!");
                            break;
                        }
                    }
                }
                //清空TbxMesage中的内容
                TbxMessage.Clear();
                //光标还原
                TbxMessage.Focus();
                //关闭UdpClient
            }
            private void ReceiveMessage()
            {
                //在本机指定的端口接收
                udpReceive = new UdpClient(8001);
                //将套接字加入组播组
                udpReceive.JoinMulticastGroup(IPAddress.Parse("224.100.0.10"), 50);
                //接收从远程主机发送过来的信息
                IPEndPoint iep= new IPEndPoint(IPAddress.Any, 0);
               while (true)
                {
                    //ref表示引用类型的 IPPoint实例接收消息
              try
              {
                      bytes = udpReceive.Receive(ref iep);
              }
              catch(SocketException e)
              {
               DialogResult result = MessageBox.Show("发送失败!");
              }
                    string str = Encoding.UTF8.GetString(bytes, 0, bytes.Length);
                    string message = "来自" + iep.ToString() + "的消息";
                    //显示消息 并以message为消息的标题
                    DialogResult res = MessageBox.Show(str, message);
                }
            }
         

            private void myUdpClient_FormClosing(object sender, FormClosingEventArgs e)
            {
                //关闭连接
                udpReceive.Close();
                udpSend.Close();
                //终止线程
                thread1.Abort();
            }

  • 相关阅读:
    被放弃的概率权,机器下围棋不理会沉没成本
    百位性感女明星三围大曝光,体型测试设计
    斯坦福大学机器学习,EM算法求解高斯混合模型
    Javascript图片预加载详解
    使用马尔可夫模型自动生成文章
    18种女粉引流方法、效果、评估
    既然认准了这条路,就不必打听要走多久!
    新媒体运营10个大坑,思维导图版
    谷歌发布"自动机器学习"技术 AI可自我创造
    Centos7下PHP的卸载与安装nginx
  • 原文地址:https://www.cnblogs.com/wenming205/p/1317879.html
Copyright © 2011-2022 走看看