±à¼ÍƼö: |
±¾ÎÄÖ÷Òª½²½âÁËUDP»ù±¾Ó¦Óᢶª°üºÍÂÒÐòÎÊÌâ¡¢½«Êý¾Ý½ÓÊÕ°üװΪʼþµÈÏà¹ØÄÚÈÝ¡£
±¾ÎÄÀ´×ÔÓÚ²©¿ÍÔ°£¬ÓÉ»ðÁú¹ûAnna±à¼ÍƼö |
|
Ò»¡¢¸ÅÊö
UDPºÍTCPÊÇÍøÂçͨѶ³£ÓõÄÁ½¸ö´«ÊäÐÒ飬C#Ò»°ã¿ÉÒÔͨ¹ýSocketÀ´ÊµÏÖUDPºÍTCPͨѶ£¬ÓÉÓÚ.NET¿ò¼Üͨ¹ýUdpClient¡¢TcpListener
¡¢TcpClientÕ⼸¸öÀà¶ÔSocket½øÐÐÁË·â×°£¬Ê¹ÆäʹÓøü¼Ó·½±ã£¬ ±¾ÎľÍͨ¹ýÕ⼸¸ö·â×°¹ýµÄÀི½âÒ»ÏÂÏà¹ØÓ¦Óá£
¶þ¡¢UDP»ù±¾Ó¦ÓÃ
ÓëTCPͨÐŲ»Í¬£¬UDPͨÐÅÊDz»·Ö·þÎñ¶ËºÍ¿Í»§¶ËµÄ£¬Í¨ÐÅË«·½ÊǶԵȵġ£ÎªÁËÃèÊö·½±ã£¬ÎÒÃǰÑͨÐÅË«·½³ÆÎª·¢ËÍ·½ºÍ½ÓÊÕ·½¡£
·¢ËÍ·½£º
Ê×ÏÈ´´½¨Ò»¸öUDP¶ÔÏó£º
string locateIP
= "127.0.0.1"; //±¾»úIP
int locatePort = 9001; //·¢ËÍ¶Ë¿Ú IPAddress locateIpAddr = IPAddress.Parse(locateIP); IPEndPoint locatePoint = new IPEndPoint(locateIpAddr,
locatePort); UdpClient udpClient = new UdpClient(locatePoint); |
·¢ËÍÊý¾Ý£º
string remoteIP
= "127.0.0.1"; //Ä¿±ê»úÆ÷IP
int remotePort = 9002; //½ÓÊÕ¶Ë¿Ú IPAddress remoteIpAddr = IPAddress.Parse(remoteIP); IPEndPoint remotePoint = new IPEndPoint(remoteIpAddr,
remotePort); byte[] buffer = Encoding.UTF8.GetBytes(¡°hello¡±); udpClient.Send(buffer, buffer.Length, remotePoint); |
ÒÔÉϾÍÍê³ÉÁËÒ»¸ö·¢ËÍÈÎÎñ£¬Ò»¸ö½ÏÍêÕûµÄ·¢ËÍ´úÂëÈçÏ£º
public partial
class FormServer : Form
{
private UdpClient udpClient = null;
private void btnConnect_Click(object sender,
EventArgs e)
{
string locateIP = "127.0.0.1";
int locatePort = 9001;
IPAddress locateIpAddr = IPAddress.Parse(locateIP);
IPEndPoint locatePoint = new IPEndPoint(locateIpAddr,
locatePort);
udpClient = new UdpClient(locatePoint); this.groupWork.Enabled = true;
} private void Send_Click(object sender, EventArgs
e)
{
string text = this.txtSend.Text.Trim();
string remoteIP = "127.0.0.1";
int remotePort = 9002;
byte[] buffer = Encoding.UTF8.GetBytes(text); if (udpClient != null)
{
IPAddress remoteIp = IPAddress.Parse(remoteIP);
IPEndPoint remotePoint = new IPEndPoint(remoteIp,
remotePort);
udpClient.Send(buffer, buffer.Length, remotePoint);
} Debug.WriteLine("Send OK");
}
} |
½ÓÊÕ¶Ë£º
Ê×ÏÈ´´½¨Ò»¸öUDP¶ÔÏó£º
string locateIP
= "127.0.0.1";
int locatePort = 9002; IPAddress locateIpAddr = IPAddress.Parse(locateIP); IPEndPoint locatePoint = new IPEndPoint(locateIpAddr,
locatePort); UdpClient udpClient = new UdpClient(locatePoint); |
½ÓÊÕÊý¾Ý£º
IPEndPoint remotePoint
= new IPEndPoint(IPAddress.Parse("1.1.1.1"),
1);
var received = udpClient.Receive(ref remotePoint); string info = Encoding.UTF8.GetString(received); string from=$¡± {remotePoint.Address}:{remotePoint.Port}¡±; |
×¢ÒâÁ½µã£º
1¡¢remotePointÊÇ»ñµÃ·¢ËÍ·½µÄIPÐÅÏ¢£¬¶¨Òåʱ¿ÉÒÔÊäÈëÈκκϷ¨µÄIPºÍ¶Ë¿ÚÐÅÏ¢£»
2¡¢Receive·½·¨ÊÇ×èÈû·½·¨£¬ËùÒÔÐèÒªÔÚеÄÏß³ÌÄÚÔËÐУ¬³ÌÐò»áÒ»Ö±µÈ´ý½ÓÊÕÊý¾Ý£¬µ±½ÓÊÕµ½Ò»°üÊý¾Ýʱ³ÌÐò¾Í·µ»Ø£¬Òª³ÖÐø½ÓÊÕÊý¾ÝÐèÒªÖØ¸´µ÷ÓÃReceive·½·¨¡£
Ò»¸ö½ÏÍêÕûµÄ½ÓÊÕ¶Ë´úÂëÈçÏ£º
public partial
class FormClent : Form
{
private UdpClient udpClient = null;
private void btnConnect_Click(object sender,
EventArgs e)
{
string locateIP = "127.0.0.1";
int locatePort = 9002;
IPAddress locateIpAddr = IPAddress.Parse(locateIP);
IPEndPoint locatePoint = new IPEndPoint(locateIpAddr,
locatePort);
udpClient = new UdpClient(locatePoint);
IPEndPoint remotePoint = new IPEndPoint(IPAddress.Parse("1.1.1.1"),
1); Task.Run(() =>
{
while (true)
{
if (udpClient != null)
{
var received = udpClient.Receive(ref remotePoint);
string info = Encoding.UTF8.GetString(received);
string from=$¡± {remotePoint.Address}:{remotePoint.Port}¡±;
}
}
});
}
} |
Èý¡¢¶ª°üºÍÂÒÐòÎÊÌâ
µ±·¢ËͶ˷¢ËÍÒ»°üÊý¾Ýʱ£¬²»¹Ü¶Ô·½ÊÇ·ñ½ÓÊÕ¶¼ÊÇ·¢Ëͳɹ¦µÄ£¬UDPÐÒé±¾Éí²¢²»»á¶Ô·¢Ë͵Ŀɿ¿ÐÔ½øÐÐÑéÖ¤¡££¨ÕâÀïµÄ¿É¿¿ÐÔÊÇÖ¸ÊÇ·ñ½ÓÊÕµ½£¬Èç¹û¶Ô·½½ÓÊÕµ½Êý¾Ý°ü£¬ÆäÄÚÈÝ»¹Êǿɿ¿µÄ£¬Õâ¸öÔÚÁ´Â·²ã½øÐÐÁ˱£Ö¤¡££©Í¬Ê±£¬ÓÉÓÚÍøÂçÑÓʱµÈÒòËØ£¬ÏÈ·¢Ë͵İü²¢²»ÄÜÈ·¶¨Ïȱ»½ÓÊÕµ½£¬ËùÒÔÓÉÓÚÕâÁ½¸öÔÒò£¬UDPͨÐÅ´æÔÚ¶ª°üºÍÂÒÐòµÄÇé¿ö¡£
ijЩҵÎñ³¡¾°Ï£¬±ÈÈçʵʱ״̬¼à¿Ø£¬¿ÉÄܶԶª°üºÍÂÒÐòÇé¿ö²¢²»Ãô¸Ð£¬ ¿ÉÒÔ²»Óô¦Àí£¬µ«´ó²¿·ÖÇé¿öÏ»¹ÊǽéÒⶪ°üµÄ£¬¼òµ¥µÄ´¦Àí°ì·¨¾ÍÊǰѰüµÄÍ·²¿¹Ì¶¨³¤¶ÈµÄ¿Õ¼äÄóöÀ´´æ·ÅºË¶ÔÐÅÏ¢£¬±ÈÈç°ü±àºÅ£¬Èç¹ûÓÐȱʧ£¬¿ÉÒÔÒªÇó·¢ËÍ·½ÖØ·¢£¬Ò²¿ÉÒÔ½øÐÐÅÅÐò¡£
ËÄ¡¢½«Êý¾Ý½ÓÊÕ°üװΪʼþ
ÎÒÃǶÔUdpClentÓÖ½øÐÐÒ»´Î·â×°£¬ÆôÓÃÒ»¸öÏ߳̽øÐнÓÊÕÊý¾Ý£¬½«½ÓÊÕµ½µÄÊý¾Ý°üͨ¹ýʼþ·¢²¼³öÀ´£¬ÕâÑùʹÓÃÆðÀ´¾Í¸ü·½±ãÁË¡£
namespace Communication.UDPClient
{
public class UdpStateEventArgs : EventArgs
{
public IPEndPoint remoteEndPoint;
public byte[] buffer = null;
}
public delegate void UDPReceivedEventHandler(UdpStateEventArgs
args); public class UDPClient
{
private UdpClient udpClient;
public event UDPReceivedEventHandler UDPMessageReceived; public UDPClient(string locateIP, int locatePort)
{
IPAddress locateIp = IPAddress.Parse(locateIP);
IPEndPoint locatePoint = new IPEndPoint(locateIp,
locatePort);
udpClient = new UdpClient(locatePoint); //¼àÌý´´½¨ºÃºó£¬´´½¨Ò»¸öỊ̈߳¬¿ªÊ¼½ÓÊÕÐÅÏ¢
Task.Run(() =>
{
while (true)
{
UdpStateEventArgs udpReceiveState = new UdpStateEventArgs(); if (udpClient != null)
{
IPEndPoint remotePoint = new IPEndPoint(IPAddress.Parse("1.1.1.1"),
1);
var received = udpClient.Receive(ref remotePoint);
udpReceiveState.remoteEndPoint = remotePoint;
udpReceiveState.buffer = received;
UDPMessageReceived?.Invoke(udpReceiveState);
}
else
{
break;
}
}
});
}
}
} |
¾ßÌåʹÓð취£º
private void
btnConnect_Click(object sender, EventArgs e)
{
string locateIP = "127.0.0.1";
int locatePort = 9002;
UDPClient udpClient = new UDPClient(locateIP,
locatePort);
udpClient.UDPMessageReceived += UdpClient_UDPMessageReceived;
}
private void UdpClient_UDPMessageReceived(UdpStateEventArgs
args)
{
var remotePoint = args.remoteEndPoint;
string info = Encoding.UTF8.GetString(args.buffer);
} |
ÏÞÓÚÆª·ù£¬ÎÒÃÇÖ»·â×°ÁËÊý¾Ý½ÓÊÕ£¬Ê±¼äʹÓÃʱÐèÒª°Ñ·¢Ë͹¦ÄÜÒ²·â×°½øÈ¥£¬Ê¹Õâ¸öÀàͬʱ¾ß±¸·¢ËͺͽÓÊÕ¹¦ÄÜ£¬·¢Ë͹¦Äܵķâ×°±È½Ï¼òµ¥¾Í²»Ìù´úÂëÁË¡£ |