@Java / Android Socket无法读取消息

时间:2014-04-06 15:47:06

标签: java android sockets tcp

我遇到了Java套接字问题。

我需要通过本地网络连接服务器和客户端,因为连接到路由器的设备可能超过两台,客户端必须找到服务器的地址。

我知道解决此问题的唯一方法是获取客户端IP的第三个数字(v4) - 地址并循环其他254个可能的IP中的每一个。 (我知道这种方式很慢,可能会导致许多问题。如果你知道另一种选择,我会很高兴的)

实际上客户端是Android智能手机,所以我可以获得DHCP-Info。

问题是,检查设备是否是服务器的读取命令将永远持续。

如果你需要一些代码,这里就是!
代码:
onCreate:

    final WifiManager manager = (WifiManager) super.getSystemService(WIFI_SERVICE);
    final DhcpInfo dhcp = manager.getDhcpInfo();
    final String address = intToIp(dhcp.ipAddress);
    String addresspart=address.substring(0, address.lastIndexOf('.')+1);
    ArrayList<HashMap<String, String>> l = null;
    Log.d("Keyboard","initiating search");
    try {
        l = new checkConnections().execute(addresspart).get();
    } catch (InterruptedException e1) {
        e1.printStackTrace();
    } catch (ExecutionException e1) {
        e1.printStackTrace();
    }    

checkConnections:

    ArrayList<HashMap<String,String>> l=new ArrayList<HashMap<String,String>>();
        for(int i=1;i<=255;i++){
        try {
            worksocket=new Socket(addresspart[0]+i,61927);
            workout=new BufferedOutputStream(worksocket.getOutputStream());
            workin=new BufferedInputStream(worksocket.getInputStream());
            byte[] buffer=new byte[6];
            workin.read(buffer);//at this point the app freezes until you stop the serverside program
            String answer=new String(buffer,"UTF-8");
            Log.i("Keyboard","Welcome Message: "+answer);
            if(answer.equalsIgnoreCase("sdk on")){
                HashMap<String,String> hm=new HashMap<String,String>();
                hm.put("address",addresspart[0]+i);
                l.add(hm);
                workout.write(intToBytes(8));
                workout.write("closing".getBytes("UTF-8"));
                worksocket.close();
                continue;
            }
            else{
                Log.d("Keyboard","No SDK-Programm detected");
                worksocket.close();
                continue;
            }
        } catch (UnknownHostException e) {
            Log.d("Keyboard",addresspart[0]+i+" doesn't exists");
            continue;
        } catch ( InterruptedIOException e){
            Log.w("System.warn",e.getCause()+e.getLocalizedMessage());
            Log.d("Keyboard","timeout");
            continue;
        } catch (IOException e) {
            Log.d("Keyboard",addresspart[0]+i+" doesn't exists");
            e.printStackTrace();
            continue;
        }
        }
        return l;   

服务器的代码:

    ServerSocket serverSocket = new ServerSocket(61927);
        System.out.println("Socket initiated");
        Socket client = serverSocket.accept();
        BufferedInputStream in=new BufferedInputStream(client.getInputStream());
        BufferedOutputStream out=new BufferedOutputStream(client.getOutputStream());
        System.out.println("client found");
        byte[] buffer=new byte[11];
        out.write("sdk on".getBytes("UTF-8"));
        in.read(buffer);
        String s=new String(buffer,"UTF-8");
        if(!s.equals("got info")){
            System.out.println("No SDK Client");
            client.close();
            serverSocket.close();
            new Main();
    }

1 个答案:

答案 0 :(得分:0)

呃,我想我应该ping Broadcast-IP而不是听取答案...
Android的Linux运行时允许它从哪个IP读取答案。