c#udp受信単純テスト

3341 ワード

1.コード(1).Program.cs
namespace CSLinkSys
{
    class Program
    {
        static void Main(string[] args)
        {
            UdpComm obj = new UdpComm();
            // ip+ ip+ 
            int flag = obj.startComm("172.20.10.3", "236.8.8.8", 1234);
            while(true)
            {
                System.Threading.Thread.Sleep(1000);
            }
        }
    }
}


(2).UdpComm.cs
using System;
using System.Collections.Generic;

namespace CSLinkSys
{
    class UdpComm
    {
        public iUDPFunc _udpRx = null;


        protected void HandUdpDataRx(Object sender, AsyncSocketUdpEventArgs args)
        {
            // explain data
            explainDataRx(args._session.bufRecv, args._session.recvLen);
        }

        protected int explainDataRx(byte[] data, int len)
        {
            if (len <= 0)
                return 0;
            int dtPos = 0;
            List list = new List();
            if (data[dtPos] == 00)
            {
                // list 
                list.Add(data[dtPos]);
            }
            return 0;
        }

        public int startComm(string localIp,string remoteIp,int udpPort)
        {
            _udpRx = new iUDPFunc();
            int nret = _udpRx.initUDPServer(localIp, remoteIp, udpPort, true); // ip, ip

            _udpRx._udpObj.Mulcast = true;// 
            _udpRx._udpObj.Loopback = true;// 

            if (nret <= 0)
                return nret;

            _udpRx.setRecvHander(HandUdpDataRx);
            nret = _udpRx.startUDPServer();

            if (nret <= 0)
                return nret;
            return 1;
        }
    }
}


(3).iUDPFunc.cs
using System;
namespace CSLinkSys
{
    class iUDPFunc
    {
        #region Fields
        public string _localIp = "";
        public string _remoteIp = "";
        public int _commPort = 0;
        public bool _recvData = false;

        public AsyncSocketUdpServer _udpObj = null;
        #endregion

        public int initUDPServer(string localIp,string remoteIp,int commport,bool recvData)
        {
            if (localIp == "" || remoteIp == "" || commport <= 0)
                return 0;
            _localIp = localIp;
            _remoteIp = remoteIp;
            _commPort = commport;
            _recvData = recvData;
            _udpObj = new AsyncSocketUdpServer(_localIp, _remoteIp, _commPort, _recvData);

            return 1;
        }
        public int setRecvHander(EventHandler udpRecvHander)
        {
            if (_udpObj != null && udpRecvHander != null)
            {
                _udpObj.DataReceived += udpRecvHander;
            }
            return 1;
        }

        public int startUDPServer()
        {
            _udpObj.Start();
            return 1;
        }

        public int stopUDPServer()
        {
            _udpObj.Stop();
            return 1;
        }

        public int sendData(byte[] dtArray, int dtLen)
        {
            _udpObj.Send(dtArray, dtLen);
            return 1;
        }

    }
}


2.キー追加参照iCommDevLib.dll(vs 2015 debuge x 64でデバッグし、dllをプロジェクトのbindebugeディレクトリの下に配置し、dllはアップロードされました)