我有多个广播服务器不断广播消息,例如“IM AVAILABLE AND MY IP ADDRESS IS 192.168.X.XXX”
我可以使用.net 客户端成功接收此消息。我已经使用.net 中的套接字创建了客户端,但现在我想要的是如果我有一些想要从这些.net 获取广播消息的 java 基本客户端或 android 基本客户端服务器那么有可能吗?
我已经在 .net 中实现了这个广播和接收器应用程序,但是对于 .net 服务器和 java 或不同平台的客户端,我不知道是否可能,如果是,那么如何,因为我是 java.so 新手。所以请任何人指导我这个。
以下是我的服务器端广播代码
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
public class Broadcst
{
public static void Main()
{
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
int portnumber = 9050;
IPEndPoint iep1 = new IPEndPoint(IPAddress.Broadcast, portnumber);
IPEndPoint iep2 = new IPEndPoint(IPAddress.Parse("192.168.1.255"), portnumber);
string hostname = Dns.GetHostName();
byte[] data = Encoding.ASCII.GetBytes(hostname);
sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
sock.SendTo(data, iep1);
sock.SendTo(data, iep2);
// portnumber--;
sock.Close();
}
}
为了从多个 .net 服务器接收广播消息,我制作了一个接收器程序,如下所示..
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
public class RecvBroadcst
{
public static void Main()
{
Socket sock = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp);
// int port_no=Convert.ToInt32(sock.RemoteEndPoint);
int portnumber = 9050;
sock = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp);
IPEndPoint iep = new IPEndPoint(IPAddress.Any,portnumber);
sock.Bind(iep);
EndPoint ep = (EndPoint)iep;
Console.WriteLine("Ready to receive...");
// int port_no = Convert.ToInt32(sock.RemoteEndPoint);
while (true)
{
byte[] data = new byte[1024];
int recv = sock.ReceiveFrom(data, ref ep);
string stringData = Encoding.ASCII.GetString(data, 0, recv);
Console.WriteLine("received: {0} from: {1}",
stringData, ep.ToString());
//data = new byte[1024];
//recv = sock.ReceiveFrom(data, ref ep);
//stringData = Encoding.ASCII.GetString(data, 0, recv);
//Console.WriteLine("received: {0} from: {1}",
// stringData, ep.ToString());
data = null;
}
//portnumber--;
sock.Close();
}
}
所以我想要做的是我在.net中制作的接收器程序,作为上面给出的代码,我想要从我的android设备中的多个.net服务器接收广播消息......我怎么能做到这一点?