zoukankan      html  css  js  c++  java
  • UDP SOCKET网络通信 C#

    接收端

    using System;
    
    using System.Net;
    
    using System.Net.Sockets;
    
    using System.Text;
    
    using System.Threading;
    
    using System.Windows.Forms;
    
     
    
    namespace UDPReceiveTest
    
    {
    
        public partial class Form1 : Form
    
        {
    
            public Thread UdpThread;
    
     
    
            public Form1()
    
            {
    
                InitializeComponent();
    
            }
    
     
    
            private void Form1_Load(object sender, EventArgs e)
    
            {
    
                string strHostIP = "";
    
                IPHostEntry oIPHost = Dns.GetHostEntry(Environment.MachineName);
    
                if (oIPHost.AddressList.Length > 0)
    
                    strHostIP = oIPHost.AddressList[0].ToString();
    
                this.txtIP.Text = strHostIP;
    
     
    
                if (UdpThread != null)
    
                {
    
                    UdpThread.Abort();
    
                    Thread.Sleep(TimeSpan.FromMilliseconds(500d));
    
                }
    
               
    
                try
    
                {
    
                    UdpThread = new Thread(new ThreadStart(UdpReciveThread));
    
                    UdpThread.Start();
    
                }
    
                catch (Exception y)
    
                {
    
                    MessageBox.Show(this, y.Message, "Error", MessageBoxButtons.OK,MessageBoxIcon.Error);
    
                    this.Dispose(true);
    
                }
    
            }
    
     
    
            private void button1_Click(object sender, EventArgs e)
    
            {
    
                txtMessage.Text = string.Empty;
    
            }
    
     
    
            delegate void SetTextCallback(IPEndPoint remoteHost, byte[] buf, string bufs);
    
            //接收数据线程
    
            void UdpReciveThread()
    
            {
    
                Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,ProtocolType.Udp);
    
                IPEndPoint iep = new IPEndPoint(IPAddress.Any, int.Parse(txtPort.Text));//接收广播信息,端口是(因为是广播端口是)
    
                socket.Bind(iep);
    
                EndPoint ep = (EndPoint)iep;
    
                Byte[] bytes = new byte[1024];
    
                while (Thread.CurrentThread.ThreadState.Equals(ThreadState.Running))
    
                {
    
                    int message = socket.ReceiveFrom(bytes, ref ep);//接收发送的信息
    
                    string bufs = Encoding.UTF8.GetString(bytes);
    
     
    
                    txtMessage.Text += (ep as IPEndPoint).Address.ToString() + "说:" +Environment.NewLine;
    
                    txtMessage.Text += bufs + Environment.NewLine + Environment.NewLine;
    
                }
    
     
    
                txtMessage.Text += "结束..." + (char)13;
    
            }
    
     
    
            private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    
            {
    
                try
    
                {
    
                    UdpThread.Abort();
    
                }
    
                catch
    
                {
    
     
    
                }
    
            }
    
        }
    
    }

    发送端

    using System;
    
    using System.Net;
    
    using System.Net.Sockets;
    
    using System.Text;
    
    using System.Windows.Forms;
    
     
    
    namespace UDPSendTest
    
    {
    
        public partial class Form1 : Form
    
        {
    
            public Form1()
    
            {
    
                InitializeComponent();
    
            }
    
     
    
            private void btnSend_Click(object sender, EventArgs e)
    
            {
    
                Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,ProtocolType.Udp);
    
                IPEndPoint iep1 = new IPEndPoint(IPAddress.Parse(txtIP.Text),int.Parse(txtPort.Text));//进行地址的广播,广播端口
    
                byte[] data = Encoding.ASCII.GetBytes(txtMessage.Text);
    
                sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
    
                sock.SendTo(data, iep1);//发送字符
    
                sock.Close();
    
            }
    
        }
    
    }
  • 相关阅读:
    crystal report 用存储过程的问题。
    新的开始—2014
    C#基础(二)——C#中的构造函数
    C#基础(一)——C#中反斜杠/n与/r的区别
    Html基础(一)
    yield关键字, default关键字, 别名关键字
    让 wpf tabcontrol 延缓初始化每个tab item content
    MVC,MVP,MVVM(补充)
    Focus scope in WPF
    Wpf ItemsControl 开启UI Virtualization 的条件
  • 原文地址:https://www.cnblogs.com/jhlong/p/5445424.html
Copyright © 2011-2022 走看看