我正在创建一个跨平台应用程序,该应用程序使用Artnet和UDP协议与网络上的设备进行通信。 我知道Artnet也是UDP。
有效的地方:
不了解在Android和iOS上使用路由器时为什么通讯为0。我尝试了所有可以在网上找到的建议代码,并勾选了可以在Android和iOS上找到的所有功能。 Wireshark显示正在进行传输,但是我的App无法捕获数据包。
摘要:
var artnet = new ArtNetSocket(); // using System.Net.Sockets;
artnet.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
artnet.Bind(new IPEndPoint(LocalIP, 6454));
artnet.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
EndPoint localPort = new IPEndPoint(IPAddress.Any, Port);
ArtNetData data= new ArtNetData();
artnet.BeginReceiveFrom(recieveState.buffer, 0, recieveState.bufferSize, SocketFlags.None, ref localPort, new AsyncCallback(WhenRecieved), data);
private void WhenRecieved(IAsyncResult state)
{
//1.Do something when received
//2.Begin receive again
}
我如何寻找IP:
NetworkInterface[] Interfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface Interface in Interfaces)
{
if (Interface.NetworkInterfaceType != NetworkInterfaceType.Loopback )
{
UnicastIPAddressInformationCollection UnicastIPInfoCol = Interface.GetIPProperties().UnicastAddresses;
foreach (var info in UnicastIPInfoCol)
{
if (info.Address.AddressFamily == AddressFamily.InterNetwork)
{
IPsets.Add(new IPCouples(info.Address, info.IPv4Mask));
}
}
}
}
太奇怪了,可能很简单...
答案 0 :(得分:0)
好吧,因为我正在为每个IP组创建套接字,所以除非APP位于Microsoft OS设备上,否则不会在公共网络上捕获数据包。如果我创建UDPClient并将其绑定到端口而不是localIP地址,它将捕获该端口上的所有内容,而不管发送者的IP地址如何。如果您检查套接字的IP地址,它将是0.0.0.0:PortNumber。
public class Something: UdpClient
{
public int LocalPort;
public Something(int LocalPort) : base(LocalPort)
{
EnableBroadcast = true;
this.LocalPort = LocalPort;
StartListening();
}
private void StartListening()
{
//IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 0);
try
{
StateObject state = new StateObject();
state.workSocket = Client;
BeginReceive(received, state);
}
catch (SocketException e)
{
Console.WriteLine(e);
}
}
private void received(IAsyncResult ar)
{
IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
StateObject state = (StateObject)ar.AsyncState;
var message = EndReceive(ar, ref remoteEndPoint);
if (message.Length > 0)
{
var messageString = (Encoding.UTF8.GetString(message));
//Do Something
}
StartListening();
}
public SendToAll(string message)
{
var callMessage = Encoding.ASCII.GetBytes(message);
UnicastIPAddressInformationCollection UnicastIPInfoCol = Interface.GetIPProperties().UnicastAddresses;
foreach (UnicastIPAddressInformation UnicatIPInfo in UnicastIPInfoCol)
{
if (UnicatIPInfo.Address.AddressFamily == AddressFamily.InterNetwork)
{
EnableBroadcast = true;
Send(callMessage, callMessage.Length, new IPEndPoint(GetBroadcast(UnicatIPInfo.Address,UnicatIPInfo.IPv4Mask) , LocalPort));
}
}
}
private IPAddress GetBroadcast(IPAddress someAddress, IPAddress someMask)
{
//Do something to get broadscat
return Broadcast;
}
}
public class StateObject
{
public Socket workSocket = null;
public const int BufferSize = 256;
public byte[] buffer = new byte[BufferSize];
public StringBuilder sb = new StringBuilder();
}
它适用于所有平台。小心使用哪个端口。