日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

UDP接收实例

發布時間:2023/12/18 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 UDP接收实例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.通訊類 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading; namespace UDPDataPack
{
?? public class UDPCommunication
??? {
??????? public UDPCommunication(int port,int timeOut)
??????? {
??????????? this.LocalReceivePort = port;
??????????? udpClient = new UdpClient(port);
??????????? udpClient.Client.ReceiveTimeout = timeOut;
??????????? receiveTimeOut = timeOut;
??????? } public UdpClient udpClient=null;
???? public string LocalIP = null;
???? public int LocalReceivePort;
???? public int receiveTimeOut;
???? public byte[] DataByte;
????? public bool Flag=false;
???? public byte[] receive()//接收
???? { if (udpClient.Client == null)
???????? {
???????????? udpClient = new UdpClient(LocalReceivePort);
???????????? udpClient.Client.ReceiveTimeout = receiveTimeOut;
???????? }
???????????? UdpClient udpClientTemp = udpClient;
???????????
???????????? IPEndPoint ipeEndPoint = null;
???????????? try
???????????? {
????????????????
???????????????? byte[] bytes = udpClientTemp.Receive(ref ipeEndPoint);
???????????????? DataByte = bytes;
???????????????? udpClientTemp.Close();
???????????????? //if (!Analyse.DecideCompletePack(bytes))
???????????????? //{
???????????????? //return DataByte=new byte[0];
???????????????? //}
???????????????? return DataByte; }
???????????? catch
???????????? { return new byte[0];
???????????? }??????
????????
???????
???? }
???? public? void start()
???? {
???????
??????? receive();
??????
????
???? }
?????? public void stop()
?????? {
?????????? Flag = true;
?????????
?????? } }
??
}

2.調用


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.Configuration;
using System.Net;
using System.Threading;
namespace UDPDataPack
{
??? public partial class UDPTestForm : Form
??? {
??????? public UDPTestForm()
??????? {
??????????? InitializeComponent();
??????????? string vertionStr = ConfigurationManager.AppSettings["ID"] + "????? " + ConfigurationManager.AppSettings["Description"];
??????????? this.Text = vertionStr;
??????????? this.LocalIPAdress.Text = GetIP();
?????????? textBoxPlayEvent = textBoxWrite;
?????????? //textBoxPlayEvent2 = textBoxWrite2;
??????????? Control.CheckForIllegalCrossThreadCalls = false; } public Analyse analyse = new Analyse(DataMemoryModel.LittleEndian);
?????? public event textBoxCallBack textBoxPlayEvent;
?????? //public event textBoxCallBack2 textBoxPlayEvent2;
??????? public Thread th;
??????? public bool stopFlag=false;
??????? UDPCommunication udpCommnication = null;

public static string GetIP()//獲取主機IP
??????? {
??????????? string strAddr = "";
??????????? try
??????????? {
??????????????? string strHostName = System.Net.Dns.GetHostName();
??????????????? System.Net.IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName);
??????????????? foreach (IPAddress _ipaddress in ipEntry.AddressList)
??????????????? {
??????????????????? if (_ipaddress.AddressFamily.ToString().ToLower() == "internetwork")
??????????????????? {
??????????????????????? strAddr = _ipaddress.ToString();
??????????????????????? break;
??????????????????? }
??????????????? }
??????????? }
??????????? catch (System.Exception e)
??????????? {
????????????? throw e;
??????????? } return strAddr;
??????? } private void buttonSingleReceive_Click(object sender, EventArgs e)//單次接收
??????? {
???????????
??????? } private void buttonStart_Click(object sender, EventArgs e)//循環接收
??????? {
??????????? stopFlag = false;
??????????? int receivePort = Convert.ToInt32(this.LocaclReceivePort.Text);
??????????? int receiveTimeOut = Convert.ToInt32(this.textBoxReceiveTimeOut.Text);
??????????? if(udpCommnication==null)
??????????? {
??????????? udpCommnication =new UDPCommunication(receivePort, receiveTimeOut);
??????????? } this.th = new Thread(threadFuction);
????????????? th.Start(); //udpCommnication.udpClient.Close();
??????? }
??????? //public void threadRun()
??????? //{
??????? //??? MethodInvoker In = new MethodInvoker(threadFuction);
??????? //?? this.BeginInvoke(In);
???????
??????? //}
??????? public void threadFuction()
??????? {
??????????? while (!stopFlag)
??????????? {?????????????
??????????????? if (udpCommnication == null)
??????????????? {
??????????????????? int receivePort = Convert.ToInt32(this.LocaclReceivePort.Text);
??????????????????? int receiveTimeOut = Convert.ToInt32(this.textBoxReceiveTimeOut.Text);
??????????????????? udpCommnication = new UDPCommunication(receivePort, receiveTimeOut);
??????????????? }
??????????????? try
??????????????? {
??????????????????? byte[] receiv = udpCommnication.receive();
??????????????????? //string content = Encoding.Default.GetString(receiv);
??????????????????? //this.textBoxPlayEvent(receiv);
??????????????????? textBoxWrite(receiv); //this.textBoxPlayEvent2(content );
??????????????? }
??????????????? catch
??????????????? {
??????????????? }
??????????? } } public delegate void textBoxCallBack(byte[] content);
??????? public delegate void textBoxCallBack2(string content);
??????? public void textBoxWrite2(string content)
??????? { this.textBoxContent.AppendText(content? + Environment.NewLine);
??????? }

總結

以上是生活随笔為你收集整理的UDP接收实例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。