• C# UDPCLIENT多线程实例


     //创建一个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();
            }
            private void ReceiveMessage()
            {
                //在本机指定的端口接收
                udpReceive = new UdpClient(8001);
                //将套接字加入组播组
                udpReceive.JoinMulticastGroup(IPAddress.Parse("127.0.0.1"), 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 BtnSend_Click(object sender, EventArgs e)
            {
                //初始化UdpClient
                udpSend = new UdpClient();
                //实际使用时应将127.0.0.1改为服务器的远程IP
                IPAddress remoteIPAddress = IPAddress.Parse("127.0.0.1");
                IPEndPoint remoteIPEndPoint = new IPEndPoint(remoteIPAddress, 8002);
                //将发送内容转换为字节数组
                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 myUdpClient_FormClosing(object sender, FormClosingEventArgs e)
            {
                //关闭连接
                udpReceive.Close();
                udpSend.Close();
                //终止线程
                thread1.Abort();
            }
        }
    }

  • 相关阅读:
    解决hadoop中 bin/hadoop fs -ls ls: `.': No such file or directory问题
    ERROR namenode.NameNode: Failed to start namenode. java.lang.IllegalArgument
    org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: file [/Users/lonecloud/tomcat/apache-tomcat-7.0.70 2/webapps/myproject/WEB-INF/classes/cn/lone
    创建Maven web工程不能解析EL表达式的解决办法
    mac中的myeclipse的控制台中文乱码问题解决办法
    Java采用内部构造器Builder模式进行对类进行构建
    java定时器的使用(Timer)
    传统的线程技术
    线程的理解
    Ibatis学习总结7--SqlMapClient 执行 SQL 语句
  • 原文地址:https://www.cnblogs.com/vic_lu/p/1951310.html
Copyright © 2020-2023  润新知