Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android客户端自动查找C#服务器_C#_Java_Android_Sockets_Networking - Fatal编程技术网

Android客户端自动查找C#服务器

Android客户端自动查找C#服务器,c#,java,android,sockets,networking,C#,Java,Android,Sockets,Networking,我的Android客户端正试图在网络中找到C#服务器 这是程序: 0服务器正在侦听UDP数据包 1.客户端发送UDP数据包并开始侦听响应 2.服务器接收UDP数据包,若数据包由客户端发送,则服务器正在向客户端发送新的UDP数据包 3.客户端接收UDP数据包 C#服务器代码: //receive UDP packet int port = (int)float.Parse(Variables.port_key); Ud

我的Android客户端正试图在网络中找到C#服务器

这是程序:
0服务器正在侦听UDP数据包
1.客户端发送UDP数据包并开始侦听响应
2.服务器接收UDP数据包,若数据包由客户端发送,则服务器正在向客户端发送新的UDP数据包
3.客户端接收UDP数据包

C#服务器代码:

//receive UDP packet
                    int port = (int)float.Parse(Variables.port_key);
                    UdpClient UDP_receive = new UdpClient(port);
                    IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
                    IPAddress from_addr = null;
                    Boolean gogo = false;
                    ExecuteCommand("Receiving...");
                    while (!gogo)
                    {
                        Byte[] receiveBytes = UDP_receive.Receive(ref RemoteIpEndPoint);
                        string returnData = Encoding.UTF8.GetString(receiveBytes);
                        if (returnData.ToString() == "83hcX1")
                        {
                            gogo = true;
                        }
                        from_addr = RemoteIpEndPoint.Address;
                        ExecuteCommand("Package received: " + returnData.ToString());
                    }
                    ExecuteCommand("Out of loop");
                    UDP_receive.Close();
                    ExecuteCommand("UDP_receive closed");
                    //send UDP packet
                    UdpClient UDP_send = new UdpClient();
                    IPEndPoint ipEndPoint = new IPEndPoint(from_addr, port);
                    Byte[] sendBytes = Encoding.UTF8.GetBytes("94dbF5");
                    UDP_send.Send(sendBytes, sendBytes.Length, ipEndPoint);
                    ExecuteCommand("Package sent");
                    UDP_send.Close();
                    ExecuteCommand("UDP_send closed");
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);
    }
这是结果:
接收…
收到包裹:83hcX1
循环外
UDP_接收已关闭
已发送包裹
UDP_发送已关闭

(所以…我认为服务器代码是可以的…)

现在是
android.java客户端代码:
PS.此代码在应用程序启动后自动执行

int port = SERVERPORT;
                    //send UDP packet
                    DatagramSocket UDP_send = new DatagramSocket();
                    byte[] b = "83hcX1".getBytes("UTF-8");
                    DatagramPacket outgoing = new DatagramPacket(b, b.length, getBroadcastAddress(Main.this), port);                    
                    UDP_send.send(outgoing);
                    Log.e("UDP", "Package sent");
                    UDP_send.close();
                    Log.e("UDP", "UDP_send closed");
                    //receive UDP packet
                    DatagramSocket UDP_receive = new DatagramSocket(port);
                    boolean gogo = false;
                    Log.e("UDP", "Receiving...");
                    while (!gogo) {                     
                        byte[] buffer = new byte[1000];
                        DatagramPacket incoming = new DatagramPacket(buffer, buffer.length);                    
                        UDP_receive.receive(incoming);
                        String message = new String(incoming.getData(), 0, incoming.getLength(), "UTF-8");
                         Log.e("Received", incoming.getPort() + "" + incoming.getAddress() + message);
                         if (message.equals("94dbF5")) {
                            Log.e("UDP", "Same");                           
                             gogo = true;
                         }else {
                            Log.e("UDP", "Not same!");
                         }                      
                    }
                    Log.e("UDP", "I'm out of loop");                
                    UDP_receive.close();
                    Log.e("UDP", "UDP_receive closed");
结果:
首次启动后:
07-10 22:35:04.017:E/UDP(4638):包已发送
07-10 22:35:04.027:E/UDP(4638):UDP_发送关闭
07-1022:35:04.047:E/UDP(4638):接收

如果我重新启动应用程序,我会得到以下信息:
07-10 22:45:05.327:E/收到(4638):42283/192.168.3.1883hcX1
07-10 22:45:05.327:E/UDP(4638):包已发送
07-10 22:45:05.327:E/UDP(4638):UDP_发送关闭
07-10 22:45:05.347:E/UDP(4638):不一样
07-10 22:45:05.347:E/UDP(4638):java.net.BindException:地址已在使用中

如果我重新启动应用程序和服务器:
07-10 22:47:36.447:E/收到(4638):57895/192.168.3.1883hcX1
07-10 22:47:36.467:E/UDP(4638):不一样
07-10 22:47:36.477:E/UDP(4638):包已发送
07-10 22:47:36.477:E/UDP(4638):UDP_发送关闭
07-10 22:47:36.487:E/收到(4638):61420/192.168.3.1094dbF5
07-10 22:47:36.497:E/UDP(4638):java.net.BindException:地址已在使用中
07-10 22:47:36.507:E/UDP(4638):相同
07-10 22:47:36.507:E/UDP(4638):我不在循环中
07-10 22:47:36.527:E/UDP(4638):UDP_接收关闭

我知道我在做一些与端口相关的错误(因为我得到了java.net.BindException:Address ready in use-error),但是什么呢?? 为什么只有在第二次启动应用程序和服务器后才能得到我想要的结果(61420/192.168.3.1094dbF5
这是一个工作示例:

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();
现在您可以连接到服务器地址(服务器IP)。
另外,我读到一些路由器(可能5%)阻止UDP广播,所以。。。小心点

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

编辑:

//receive UDP packet
                    int port = (int)float.Parse(Variables.port_key);
                    UdpClient UDP_receive = new UdpClient(port);
                    IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
                    IPAddress from_addr = null;
                    Boolean gogo = false;
                    ExecuteCommand("Receiving...");
                    while (!gogo)
                    {
                        Byte[] receiveBytes = UDP_receive.Receive(ref RemoteIpEndPoint);
                        string returnData = Encoding.UTF8.GetString(receiveBytes);
                        if (returnData.ToString() == "83hcX1")
                        {
                            gogo = true;
                        }
                        from_addr = RemoteIpEndPoint.Address;
                        ExecuteCommand("Package received: " + returnData.ToString());
                    }
                    ExecuteCommand("Out of loop");
                    UDP_receive.Close();
                    ExecuteCommand("UDP_receive closed");
                    //send UDP packet
                    UdpClient UDP_send = new UdpClient();
                    IPEndPoint ipEndPoint = new IPEndPoint(from_addr, port);
                    Byte[] sendBytes = Encoding.UTF8.GetBytes("94dbF5");
                    UDP_send.Send(sendBytes, sendBytes.Length, ipEndPoint);
                    ExecuteCommand("Package sent");
                    UDP_send.Close();
                    ExecuteCommand("UDP_send closed");
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);
    }
InetAddress getBroadcastAddress(上下文上下文)引发IOException{
WifiManager wifi=(WifiManager)context.getSystemService(context.wifi\u SERVICE);
DhcpInfo dhcp=wifi.getDhcpInfo();
如果(dhcp==null){
返回null;
}
int broadcast=(dhcp.ipAddress&dhcp.netmask)|~dhcp.netmask;
字节[]四元组=新字节[4];
对于(int k=0;k<4;k++)
四元组[k]=(字节)((广播>>k*8)和0xFF);
返回InetAddress.getByAddress(quads);
}

我想你指的是UDP数据包。是的,很抱歉我的英语不好。