本机发送UDP数据,单给本机发送,那本机就接收不到,不管是```127.0.0.1```还是我本机的局域网ip,就是不行,但是给其他机子发送,带上本机的地址,那就可以接收到。
UdpClient udp = null;
private void ReceiveMsg()
{
udp = new UdpClient(35796, AddressFamily.InterNetwork);
Thread thread = new Thread(HandleUdpMsg);
thread.IsBackground = true;
IsHandle = true;
thread.Start();
}
private void HandleUdpMsg()
{
while (IsHandle)
{
IPEndPoint endPoint = null;
byte[] buffer = udp.Receive(ref endPoint);
string content = Encoding.UTF8.GetString(buffer);
chatContentText.AppendText(endPoint.Address + ":\t" + content);
}
}
ConcurrentQueue<byte[]> udpQueue = new ConcurrentQueue<byte[]> ();
private static void ReceiveMessage(object obj)
{
// 获取网络设备
var devices = CaptureDeviceList.Instance;
//从列表中提取数据
ICaptureDevice device = devices[0];
int readTimeoutMilliseconds = 1000;
//打开捕获设备
device.Open(DeviceMode.Promiscuous, readTimeoutMilliseconds);
device.Filter = "ip and udp";
Packet packet = null;
string port = string.Empty;
string s = string.Empty;
while (true)
{
try
{
if (device != null)
{
var rawCaptire = device.GetNextPacket();
if (rawCaptire != null)
{
try
{
packet = Packet.ParsePacket(rawCaptire.LinkLayerType, rawCaptire.Data);
}
catch (Exception)
{
throw;
}
var udp = packet.Extract(typeof(UdpPacket));
var Address = UdpPacket.GetEncapsulated(packet);
port = Address.DestinationPort.ToString();
if(port == "35796"){
udpQueue.Enqueue(udp.PayloadData);
}
}}
}
catch (Exception ex)
{
continue;
}
}
}
private void MsgSendBtn_Click(object sender, EventArgs e)
{
string text = sendMsgText.Text;
if (string.IsNullOrEmpty(text))
{
return;
}
byte[] bytes = Encoding.UTF8.GetBytes(text + "\n");
IPAddress ip = IPAddress.Parse("172.16.20.110");
int port = 35796;
IPEndPoint remote = new IPEndPoint(ip, port);
UdpClient udp = new UdpClient();
udp.Connect(remote);
int sendLength = udp.Send(bytes, bytes.Length);
SendResult.Text = "已向\t" + ip.ToString() + "\t发送\t" + sendLength + "\t字节。";
chatContentText.AppendText("我:"+text + "\n");
sendMsgText.Clear();
}
我用你的代碼監聽的到喔
我也只有一張網卡,但CaptureDeviceList.Instance 卻取到6個
逐個測試才找到正確的那一個
你在本機debug 看看取到幾個,再確認是否監聽錯device 了
明天我测试下,不应该啊,不光我本机,我们这一开发小组都是一样的
你发的是内网地址还是127.0.0.1
局域网地址,emmm
ICaptureDevice device = devices[0];
我这里devices
的Count = 1
,哇~
@小草上飞飞:
static void Main(string[] args)
{
// 获取网络设备
var devices = CaptureDeviceList.Instance;
//从列表中提取数据
ICaptureDevice device = devices[4];
int readTimeoutMilliseconds = 1000;
//打开捕获设备
device.Open(DeviceMode.Promiscuous, readTimeoutMilliseconds);
device.Filter = "ip and udp";
Packet packet = null;
string port = string.Empty;
string s = string.Empty;
while (true)
{
try
{
if (device != null)
{
var rawCaptire = device.GetNextPacket();
if (rawCaptire != null)
{
try
{
packet = Packet.ParsePacket(rawCaptire.LinkLayerType, rawCaptire.Data);
}
catch (Exception)
{
throw;
}
var udp = packet.Extract(typeof(UdpPacket));
Console.WriteLine(Encoding.UTF8.GetString(udp.PayloadData));
//var Address = ((UdpPacket)packet);
//port = Address.DestinationPort.ToString();
//if (port == "35796")
//{
// Console.WriteLine(udp.PayloadData);
//}
}
}
}
catch (Exception ex)
{
throw;
}
}
}
static void Main(string[] args)
{
string text = "Test";
byte[] bytes = Encoding.UTF8.GetBytes(text + "\n");
IPAddress ip = IPAddress.Parse("127.0.0.1");
int port = 35796;
IPEndPoint remote = new IPEndPoint(ip, port);
UdpClient udp = new UdpClient();
udp.Connect(remote);
int sendLength = udp.Send(bytes, bytes.Length);
}
@RosonJ:
换
var udp = packet.Extract(typeof(UdpPacket));
var Address = UdpPacket.GetEncapsulated(packet);
试试可行不
@RosonJ: 算了,不麻烦你了,我这边还是不行
@RosonJ: 我都用的是你的代码。。。
@小草上飞飞:
你用我的接收代碼,直接執行,放著看一下有沒有接收到請求
因為我的代碼沒有過濾port,應該會監聽到很多請求
@小草上飞飞:
我的會像這樣持續監聽到請求
@RosonJ: 我这边udp从本地发给本地的udp消息好像不过网卡,因为wireShark都监听不到
@小草上飞飞:
我的了解本機請求是會過網卡的,WireShark 監聽不到應該屬於異常
你們公司是不是有特別對網路做設定?
@RosonJ: 我内网、外网都测试了,发给本机的能收到消息,WireShark抓不到包
@RosonJ: 应该没对网络做其他的处理
@RosonJ:
发现个这个东西,是不是因为这个原因?
@小草上飞飞:
那應該也監聽的到...吧,要研究一下
@RosonJ: 找了一圈,没有发现什么有帮助的东西
@RosonJ: 不过网络,发给本机的udp,就算你拔了网线,也是接收得到的,就算你发的地址不是127.0.0.1
@小草上飞飞:
沒錯,但你監聽的是網卡,是網線的前一關
沒過網線應該還是要過網卡,應該還是要被監聽到