Java ServerSocket客户端(True)
我有一个关于While(对)的问题。通常会删除计算机中的CPU。为什么在这种情况下,它不经常运行?如果需要,我如何让它运行Java ServerSocket客户端(True),java,loops,client,serversocket,Java,Loops,Client,Serversocket,我有一个关于While(对)的问题。通常会删除计算机中的CPU。为什么在这种情况下,它不经常运行?如果需要,我如何让它运行 public void startConnection() { try { client = new Socket(ip, port); out = new PrintWriter(client.getOutputStream(), true); in = new BufferedReader(new Inp
public void startConnection() {
try {
client = new Socket(ip, port);
out = new PrintWriter(client.getOutputStream(), true);
in = new BufferedReader(new
InputStreamReader(client.getInputStream()));
out.println("Connection started from, " + client.getLocalAddress());
out.flush();
while (true) {
String recieve = in.readLine();
System.out.println(recieve);
@SuppressWarnings("resource")
Scanner scan = new Scanner(System.in);
String send = scan.nextLine();
if (send != null ) {
out.println(send);
out.flush();
}
}
} catch (Exception e) {
System.out.println(e);
JOptionPane.showMessageDialog(null, e.toString(),
"Connection-Error", JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
}
从套接字读取是一种阻塞操作(取决于套接字的配置和调用中的参数)。在这种情况下,操作系统将阻止您,直到套接字中有可读取的内容
如果需要,您可以将套接字配置为使读取操作超时。它在InputStream上的
读取行上阻塞了,如果需要,我如何让它运行?首先更正缩进,然后编译然后运行