• c# UDP通信


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Net.Sockets;
    using System.Net;
    using System.Threading;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            private UdpClient sendUdpClient;
            private UdpClient receiveUpdClient;
            public Form1()
            {
                InitializeComponent();
                IPAddress[] ips = Dns.GetHostAddresses("");
                tbxlocalip.Text = ips[0].ToString();
                int port = 51883;
                tbxlocalPort.Text = port.ToString();
                tbxSendtoIp.Text = ips[0].ToString();
               tbxSendtoport.Text = port.ToString();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                Control.CheckForIllegalCrossThreadCalls = false;
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                if (tbxMessageSend.Text == string.Empty)
                {
                    MessageBox.Show("发送内容不能为空", "提示");
                    return;
                }
    
                // 选择发送模式
                if (chkbxAnonymous.Checked == true)
                {
                    // 匿名模式(套接字绑定的端口由系统随机分配)
                    sendUdpClient = new UdpClient(0);
                }
                else
                {
                    // 实名模式(套接字绑定到本地指定的端口)
                    IPAddress localIp = IPAddress.Parse(tbxlocalip.Text);
                    IPEndPoint localIpEndPoint = new IPEndPoint(localIp, int.Parse(tbxlocalPort.Text));
                    sendUdpClient = new UdpClient(localIpEndPoint);
                }
    
                Thread sendThread = new Thread(SendMessage);
                sendThread.Start(tbxMessageSend.Text);
            }
    
    
    
            
           
    
            private void SendMessage(object obj)
            {
                string message = (string)obj;
                byte[] sendbytes = Encoding.Unicode.GetBytes(message);
                IPAddress remoteIp = IPAddress.Parse(tbxSendtoIp.Text);
                IPEndPoint remoteIpEndPoint = new IPEndPoint(remoteIp, int.Parse(tbxSendtoport.Text));
                sendUdpClient.Send(sendbytes, sendbytes.Length, remoteIpEndPoint);
    
                sendUdpClient.Close();
    
                // 清空发送消息框
                //ResetMessageText(tbxMessageSend);
            }
    
            delegate void ShowMessageforViewCallBack(ListBox listbox, string text);
            private void ShowMessageforView(ListBox listbox, string text)
            {
                if (listbox.InvokeRequired)
                {
                    ShowMessageforViewCallBack showMessageforViewCallback = ShowMessageforView;
                    listbox.Invoke(showMessageforViewCallback, new object[] { listbox, text });
                }
                else
                {
                    lstbxMessageView.Items.Add(text);
                    lstbxMessageView.SelectedIndex = lstbxMessageView.Items.Count - 1;
                    lstbxMessageView.ClearSelected();
                }
            }
    
    
            //delegate void ResetMessageCallback(TextBox textbox);
            //private void ResetMessageText(TextBox textbox)
            //{
            //    // Control.InvokeRequired属性代表
            //    // 如果控件的处理与调用线程在不同线程上创建的,则为true,否则为false
            //    if (textbox.InvokeRequired)
            //    {
            //        ResetMessageCallback resetMessagecallback = ResetMessageText;
            //        textbox.Invoke(resetMessagecallback, new object[] { textbox });
            //    }
            //    else
            //    {
            //        textbox.Clear();
            //        textbox.Focus();
            //    }
            //}
    
            private void button4_Click(object sender, EventArgs e)
            {
                // 创建接收套接字
                IPAddress localIp = IPAddress.Parse(tbxlocalip.Text);
                IPEndPoint localIpEndPoint = new IPEndPoint(localIp, int.Parse(tbxlocalPort.Text));
                receiveUpdClient = new UdpClient(localIpEndPoint);
    
    
                Thread receiveThread = new Thread(ReceiveMessage);
                receiveThread.Start();
            }
            private void ReceiveMessage()
            {
                IPEndPoint remoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
                while (true)
                {
                    try
                    {
                        // 关闭receiveUdpClient时此时会产生异常
                        byte[] receiveBytes = receiveUpdClient.Receive(ref remoteIpEndPoint);
    
                        string message = Encoding.Unicode.GetString(receiveBytes);
    
                        // 显示消息内容
                        ShowMessageforView(lstbxMessageView, string.Format("{0}[{1}]", remoteIpEndPoint, message));
                    }
                    catch
                    {
                        break;
                    }
                }
            }
    
            private void button3_Click(object sender, EventArgs e)
            {
                receiveUpdClient.Close();
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                this.lstbxMessageView.Items.Clear();
            }
        }
    }
    

      

  • 相关阅读:
    mongodb的sql例子(简单版)
    git上传github上
    git中的版本库,暂存区和工作区
    进程与线程的区别
    mysql 在linux 修改账号密码
    linux 下 yum 安装mysql
    linux 下 修改mysql账号密码
    linux 下开放端口问题
    linux 下安装tomcat
    Ubuntu 配置Tomcat环境(转载)
  • 原文地址:https://www.cnblogs.com/mengluo/p/5659260.html
Copyright © 2020-2023  润新知