我有以下用于向服务器发送字符串的客户端套接字。服务器没有收到消息。可能是什么问题?
public void startClient() throws IOException {
Socket socket = null;
PrintWriter out = null;
BufferedReader in = null;
InetAddress host = null;
BufferedReader stdIn = null;
try {
host = InetAddress.getByName("172.16.2.97");
socket = new Socket(host, 52000);
out = new PrintWriter(socket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
stdIn = new BufferedReader(new InputStreamReader(System.in));
String fromServer;
String fromUser = null;
//ClientHelper.unpackISO();
fromUser = ClientHelper.createISO();
if (fromUser != null) {
//System.out.println("Client - " + fromUser);
out.write(fromUser);
System.out.println("Sent message");
}
while ((fromServer = in.readLine()) != null) {
System.out.println("Server - " + fromServer);
if (fromUser != null) {
//System.out.println("Client - " + fromUser);
out.println(fromUser);
}
}
} catch (ISOException ex) {
Logger.getLogger(ClientDemo.class.getName()).log(Level.SEVERE, null, ex);
} catch (UnknownHostException e) {
System.err.println("Cannot find the host: " + host.getHostName());
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't read/write from the connection: " + e.getMessage());
System.exit(1);
} finally { //Make sure we always clean up
out.close();
in.close();
stdIn.close();
socket.close();
}
}
ClientHelper.createISO() 方法返回一个应该发送到服务器的字符串。不幸的是,服务器没有得到任何字符串。问题可能是代理设置。如果是这样怎么解决。还是我的代码有另一个问题?我的代码有什么问题?