我正在进行在线java聊天,我正在测试它,当我在其他计算机上使用它时,它不会与此PC同时工作,或者在另一台计算机上显示另一台PC上的JTextBox-es ,这是我用来将服务器传送到客户端的代码
Socket kkSocket = null;
PrintWriter out = null;
BufferedReader in = null;
try {
kkSocket = new Socket("dargon.ddns.net", 7598);
out = new PrintWriter(kkSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("not work :/");
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection");
System.exit(1);
}
这是服务器代码
public class Online {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(7598);
System.out.print("Online!");
} catch (IOException e) {
System.err.println("Could not listen on port: 7598.");
System.exit(1);
}
Socket clientSocket = null;
try {
clientSocket = serverSocket.accept();
System.out.print("Online!");
} catch (IOException e) {
System.err.println("Accept failed.");
System.exit(1);
}
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(
clientSocket.getInputStream()));
out.close();
in.close();
clientSocket.close();
serverSocket.close();
}
}