1

我在我的应用程序中使用套接字通信,我需要一些帮助来解决我面临的问题。

我正在使用套接字向设备发送数据,在解析该数据后,我需要将结果返回给服务器设备,但我发送响应的函数崩溃了。以下是我正在使用的功能:

通过 Socket 发送:

public static void sendRequest(final  String ip,final int port,final byte[] message){

    Thread thread =  new Thread(new Runnable() {
        public void run() {
            try {
                socket = new Socket(ip, port);
                try {
                    OutputStream target = socket.getOutputStream();
                    target.write(message);
                    target.flush();

                    Log.e("SocketConnectionHandler", "S: Send");

                    socket.close();
                    Log.e("SocketConnectionHandler", "S: Closed.");
                } catch (Exception e) {
                    Log.e("SocketConnectionHandler", "S: Error", e);
                }

            } catch(Exception e){
                e.printStackTrace();
            }
        }
    });
    thread.start();
}

从套接字读取:

thread =  new Thread(new Runnable() {
        public void run() {
            try {
                serverSocket = new ServerSocket(JmDNSHandler.usedPort);
                while(state){
                        client = serverSocket.accept();
                        client.setKeepAlive(true);

                        // LOGS
                        Log.d("","receivedBufferSize : "+serverSocket.getReceiveBufferSize());
                        Log.d("","is connected : "+client.isConnected());
                        Log.d("","port : "+client.getPort());
                        Log.d("","local port : "+client.getLocalPort());
                        Log.d("","ipadress : "+client.getInetAddress().toString());

                        DNSUtils.clientIP = client.getInetAddress().toString();
                        DNSUtils.clientPort = client_getPort();

                        InputStream is = client.getInputStream();

                        Log.d("","is Size : "+is.available());

                        byte[] bytes = toByteArray(is);
                        for(int i=0;i<bytes.length;i++){
                            Log.d("","bytes["+i+"] : "+bytes[i]);
                        }

                        if(bytes!=null)
                            getPacketType(bytes[4], bytes);

                 }

                 client.close();
                 serverSocket.close();
                 Log.d("","client socket : "+client.isClosed() + " serverSocket : "+serverSocket.isClosed());

             } catch (UnknownHostException e) {
                 e.printStackTrace();
             } catch (IOException e) {
                 e.printStackTrace();
             }
        }
    });
    thread.start();

实际上,在我将第一个字节 [] 发送给客户端后,应用程序崩溃了,他试图在解析字节 [] 后向我发送响应。所以例外是:

    08-14 11:50:03.421: W/System.err(18303): java.net.UnknownHostException: Unable to resolve host "/192.168.***.**": No address associated with hostname
08-14 11:50:03.421: W/System.err(18303):    at java.net.InetAddress.lookupHostByName(InetAddress.java:436)
08-14 11:50:03.421: W/System.err(18303):    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:245)
08-14 11:50:03.421: W/System.err(18303):    at java.net.InetAddress.getAllByName(InetAddress.java:220)
08-14 11:50:03.431: W/System.err(18303):    at java.net.Socket.tryAllAddresses(Socket.java:108)
08-14 11:50:03.431: W/System.err(18303):    at java.net.Socket.<init>(Socket.java:177)
08-14 11:50:03.431: W/System.err(18303):    at java.net.Socket.<init>(Socket.java:149)
08-14 11:50:03.431: W/System.err(18303):    at com.stampii.stampii.remoteservices.GetActivatedCollections$1.run(GetActivatedCollections.java:56)
08-14 11:50:03.431: W/System.err(18303):    at java.lang.Thread.run(Thread.java:864)
08-14 11:50:03.441: W/System.err(18303): Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
08-14 11:50:03.441: W/System.err(18303):    at libcore.io.Posix.getaddrinfo(Native Method)
08-14 11:50:03.441: W/System.err(18303):    at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:55)
08-14 11:50:03.441: W/System.err(18303):    at java.net.InetAddress.lookupHostByName(InetAddress.java:421)
08-14 11:50:03.441: W/System.err(18303):    ... 7 more
4

1 回答 1

0

正如@user370305 提到的,问题是作为 IP 返回的字符串"/192.168.***.**"。删除/字符串开头的 后一切正常。

于 2012-08-17T11:36:39.543 回答