如何从多个.net服务器接收广播到Android基础客户端?

时间:2012-10-01 09:27:13

标签: android asp.net sockets broadcastreceiver broadcasting

我有多个广播服务器,持续广播该消息,例如“我可用,我的IP地址是192.168.X.XXX”

我可以用.net客户端成功接收此消息。我已经在.net中使用套接字创建了客户端但是现在我想要的是如果我有一些java基本客户端或者想要从中获取该广播消息的基本客户端这些.net服务器有可能吗?

我已经在.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服务器的广播消息...我怎样才能实现是什么?

0 个答案:

没有答案