我在我的应用程序中使用套接字通信,我需要帮助解决我遇到的问题。
我正在使用套接字向设备发送数据,在解析数据后我需要将结果返回给服务器设备,但是我发送响应的函数崩溃了。以下是我正在使用的功能:
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();
实际上,在我将第一个字节[]发送到客户端之后应用程序崩溃,并且他在解析byte []后尝试向我发送响应。所以例外是:
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
答案 0 :(得分:0)
正如@ user370305所述,问题是返回为IP "/192.168.***.**"
的字符串。删除字符串开头的/
后,一切正常。