我一直在查看 Oracle 教程网站来学习基本的套接字编程。我尝试在 eclipse 上上传示例,但是当我尝试编译时,括号所在的语法有错误。为什么会发生这种情况?谢谢。
try (
Socket echoSocket = new Socket(hostName, portNumber);
PrintWriter out =
new PrintWriter(echoSocket.getOutputStream(), true);
BufferedReader in =
new BufferedReader(
new InputStreamReader(echoSocket.getInputStream()));
BufferedReader stdIn =
new BufferedReader(
new InputStreamReader(System.in))
) {
String userInput;
while ((userInput = stdIn.readLine()) != null) {
out.println(userInput);
System.out.println("echo: " + in.readLine());
}
} catch (UnknownHostException e) {
System.err.println("Don't know about host " + hostName);
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to " +
hostName);
System.exit(1);
}
最佳答案
block 引用>Why is this happening?
您正在尝试进行“尝试资源”。这需要 Java 7 或更高版本。确保您的 eclipse 项目也设置为 Java 7,而不仅仅是本地命令行 JDK。
http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
关于java - 套接字编程,尝试循环遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22420201/