且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Android客户端自动发现C#服务器

更新时间:2022-06-20 22:33:35

解决了!结果
这是一个工作的例子:

Solved!
This is a working example:

C#服务器

                    //receive UDP packet
                    int port = (int)float.Parse(Variables.port_key);
                    UdpCient UDP_packet = new UdpClient(port);
                    UDP_packet.EnableBroadcast = true;
                    IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
                    IPAddress from_addr = null;
                    Boolean gogo = false;
                    while (!gogo)
                    {
                        Byte[] receiveBytes = UDP_packet.Receive(ref RemoteIpEndPoint);
                        string returnData = Encoding.UTF8.GetString(receiveBytes);
                        if (returnData.ToString() == "83hcX1")
                        {
                            gogo = true;
                        }
                        from_addr = RemoteIpEndPoint.Address;
                    }
                    //send UDP packet
                    IPEndPoint ipEndPoint = new IPEndPoint(from_addr, port);
                    Byte[] sendBytes = Encoding.UTF8.GetBytes("94dbF5");
                    UDP_packet.Send(sendBytes, sendBytes.Length, ipEndPoint);
                    UDP_packet.Close();

Android客户端

                        //send UDP packet
                        DatagramSocket UDP_packet = new DatagramSocket(SERVERPORT);
                        UDP_packet.setBroadcast(true);
                        byte[] b = "83hcX1".getBytes("UTF-8");
                        DatagramPacket outgoing = new DatagramPacket(b, b.length, getBroadcastAddress(Main.this), SERVERPORT);                  
                        UDP_packet.send(outgoing);
                        //receive UDP packet
                        boolean gogo = false;
                        while (!gogo) {                     
                            byte[] buffer = new byte[1024];
                            DatagramPacket incoming = new DatagramPacket(buffer, buffer.length);    
                            UDP_packet.receive(incoming);
                            String message = new String(incoming.getData(), 0, incoming.getLength(), "UTF-8");
                             if (message.equals("94dbF5")) {
                                 gogo = true;
                                 SERVER_IP = incoming.getAddress();
                             }                  
                        }               
                        UDP_packet.close();

现在您可以连接到服务器地址(SERVER_IP)。结果
另外,我读了一些路由器(也许5%)阻止UDP广播,所以......要小心。

Now you can connect to server address (SERVER_IP).
Also, I read that some routers (maybe 5%) block UDP broadcast, so... be careful.

如果有人看到任何错误,请张贴。

If someone see any error, please post it.

编辑:

InetAddress getBroadcastAddress(Context context) throws IOException {
        WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        DhcpInfo dhcp = wifi.getDhcpInfo();
        if (dhcp == null) {
              return null;
            }
        int broadcast = (dhcp.ipAddress & dhcp.netmask) | ~dhcp.netmask;
        byte[] quads = new byte[4];
        for (int k = 0; k < 4; k++)
          quads[k] = (byte) ((broadcast >> k * 8) & 0xFF);
        return InetAddress.getByAddress(quads);
    }