对socket通信的流程不太了解、、遇到一个很脑残的问题:client连接到server,然后写了个OutputStream给server,然后client就等着InputStream,但是server一直不能读取client写入的流、、后来发现参考的例子还有一句:mSocket.shutdownOutput(),意思是当client写了流之后要把输出关闭、要不然server就读取不到流
client.shutdownOutput();重点
Socket client = null;
DataOutputStream out = null;
DataInputStream in = null;
try {
client = new Socket("192.168.1.105", 5555);
client.setSoTimeout(10000);
out = new DataOutputStream((client.getOutputStream()));
String query = "GB";
byte[] request = query.getBytes();
out.write(request);
out.flush();
client.shutdownOutput();
in = new DataInputStream(client.getInputStream());
byte[] reply = new byte[40];
in.read(reply);
System.out.println("Time: " + new String(reply, "utf-8"));
in.close();
out.close();
client.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}