C# UDP发送和接收数据类

UDP发送和接收数据类

using System;
using System.Net;
//本段代码中需要新增加的命名空间
using System.Net.Sockets;
using System.Threading;
using System.Collections.Generic;
using System.Windows.Forms;

namespace CM2.CentreWin
{
class AppUDP
{
///
/// 用于UDP发送的网络服务类
///
private UdpClient udpcSendRecv;
///
/// 用于UDP接收的网络服务类
///
// private UdpClient udpcRecv;
///
/// 开关:在监听UDP报文阶段为true,否则为false
///
bool IsUdpcRecvStart = false;
///
/// 线程:不断监听UDP报文
///
Thread thrRecv;
byte[] SendintArr = new byte[64];
byte[] RecvintArr = new byte[64];
IPEndPoint RecvIpPort;

    List RecvintArr_list  = new List();
    int RecvintArr_cnt;
    public void SendReceiveMessage(object SendintArrobj, object RecvintArrobj, ref string RecvIpEndPort)
    {
        string lst_HostIPInfo;
        string lst_HostIPInfo1;
        lst_HostIPInfo = null;
        string name = Dns.GetHostName();
        lst_HostIPInfo1 = me.AddressList[0].ToString();
         Random rand = new Random();
        // 匿名发送
        // 实名发送
        IPEndPoint localIpep = new IPEndPoint(IPAddress.Parse(lst_HostIPInfo1), 62730+ rand.Next(1000)); // 本机IP,指定的端口号
        udpcSendRecv = new UdpClient(localIpep);
        Thread thrSend = new Thread(SendMessage);
        byte[] sendbytes = (byte[])SendintArrobj;
        RecvintArr_list = (List)RecvintArrobj;
        for (int i = 0; i < 64; i++)
        {
            SendintArr[i] = sendbytes[i];
        }
        RecvintArr_cnt = 0;
        IsUdpcRecvStart = false;
        thrRecv = new Thread(ReceiveMessage);
        thrRecv.IsBackground = false;
        thrRecv.Start();

        thrSend.Start(SendintArr);
        for (int i = 0; i < 50; i++)
        {
            Thread.Sleep(100);
            Console.WriteLine("主进程结束");
            if (IsUdpcRecvStart == false)
            {

                Console.WriteLine("IsUdpcRecvStart == false");
            }
            else
            {
                Console.WriteLine("IsUdpcRecvStart == trun");
                break;

            }
        }

        thrRecv.Abort(); // 必须先关闭这个线程,否则会异常
        udpcSendRecv.Close();
        Console.WriteLine("关闭子线程");
    }

    public void SendReceiveMessage_recx(object SendintArrobj, object RecvintArrobj, ref string RecvIpEndPort)
    {
        string lst_HostIPInfo;
        string lst_HostIPInfo1;
        lst_HostIPInfo = null;
        string name = Dns.GetHostName();
        IPHostEntry me = Dns.GetHostByName(name);
        lst_HostIPInfo1 = me.AddressList[0].ToString();
        IPEndPoint localIpep = new IPEndPoint(IPAddress.Parse(lst_HostIPInfo1), 62730); // 本机IP,指定的端口号
        udpcSendRecv = new UdpClient(localIpep);
        Thread thrSend = new Thread(SendMessage_recx);
        byte[] sendbytes = (byte[])SendintArrobj;
        RecvintArr_list = (List)RecvintArrobj;
        for (int i = 0; i < 64; i++)
        {
            SendintArr[i] = sendbytes[i];
        }
        RecvintArr_cnt = 0;
        thrSend.Start(SendintArr);
        Thread.Sleep(100);
        IsUdpcRecvStart = false;
        thrRecv = new Thread(ReceiveMessage_recx);
        thrRecv.Start();
        Thread.Sleep(500);
        if (IsUdpcRecvStart == false)
        {
        }
        else
        {
        }
        thrRecv.Abort(); // 必须先关闭这个线程,否则会异常
        udpcSendRecv.Close();
    }
    /// 
    /// 发送信息
    /// 
    /// 
    public void SendMessage(object obj)
    {
        byte[] sendbytes = (byte[])obj;
        IPEndPoint remoteIpep = new IPEndPoint(IPAddress.Parse("255.255.255.255"), 60000); // 发送到的IP地址和端口号

        Console.WriteLine(BitConverter.ToString(sendbytes, 0).Replace(" ", string.Empty).ToLower());

        udpcSendRecv.Send(sendbytes, 64, remoteIpep);
        Console.WriteLine("发送OK");
    }

    /// 
    /// 接收数据
    /// 
    /// 
    public void ReceiveMessage(object obj)
    {
        IPEndPoint remoteIpep = new IPEndPoint(IPAddress.Any, 0);
        while (true)
        {
            try
            {
                RecvintArr = udpcSendRecv.Receive(ref RecvIpPort);
                RecvintArr_list.Add(RecvintArr);
                IsUdpcRecvStart = true;
                Console.WriteLine(BitConverter.ToString(RecvintArr, 0).Replace(" ", string.Empty).ToLower());
                break;
            }
            catch (Exception ex)
            {
                break;
            }
        }
        Console.WriteLine("子线程关闭子线程");
    }
    /// 
    /// 发送信息
    /// 
    /// 
    public void SendMessage_recx(object obj)
    {
        byte[] sendbytes = (byte[])obj;
        IPEndPoint remoteIpep = new IPEndPoint(IPAddress.Parse("255.255.255.255"), 60000); // 发送到的IP地址和端口号
        udpcSendRecv.Send(sendbytes, 64, remoteIpep);
    }

    /// 
    /// 接收数据
    /// 
    /// 
    public void ReceiveMessage_recx(object obj)
    {
        IPEndPoint remoteIpep = new IPEndPoint(IPAddress.Any, 0);
        while (true)
        {
            try
            {
                RecvintArr = udpcSendRecv.Receive(ref RecvIpPort);
                IsUdpcRecvStart = true;
                RecvintArr_list.Add(RecvintArr);
            }
            catch (Exception ex)
            {
                break;
            }
        }
    }
}

}

你可能感兴趣的:(C#,udp,c#,网络协议)