您好,我对下面的代码有一些问题。try 和 catch 块在我的 IDE 中的 try 行有一个红色的 X,告诉我遇到了“(”,但预期的是“{”。我错过了什么吗?我的问题可能是什么?
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.StandardSocketOptions;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousServerSocketChannel;
import java.nio.channels.AsynchronousSocketChannel;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
/** * * @author Apress */
public class FutureForm
{
public static void main(String[] args)
{
final int DEFAULT_PORT = 5555;
final String IP = "127.0.0.1";
// v-This parenthesis should be a bracket it says??
try( AsynchronousServerSocketChannel asynchronousServerSocketChannel = AsynchronousServerSocketChannel.open())
{
if (asynchronousServerSocketChannel.isOpen())
{
asynchronousServerSocketChannel.bind(new InetSocketAddress(IP, DEFAULT_PORT));
System.out.println("Waiting for connections ...");
while (true)
{
Future<AsynchronousSocketChannel> asynchronousSocketChannelFuture = asynchronousServerSocketChannel.accept();
try (AsynchronousSocketChannel asynchronousSocketChannel = asynchronousSocketChannelFuture.get())
{
System.out.println("Incoming connection from: " + asynchronousSocketChannel.getRemoteAddress());
final ByteBuffer buffer = ByteBuffer.allocateDirect(1024);
while (asynchronousSocketChannel.read(buffer).get() != -1)
{
buffer.flip();
asynchronousSocketChannel.write(buffer).get();
if (buffer.hasRemaining())
buffer.compact();
else
buffer.clear();
}
System.out.println(asynchronousSocketChannel.getRemoteAddress() + " was successfully served!");
}
catch (IOException || InterruptedException || ExecutionException ex)
{
System.err.println(ex);
}
}
}
else
{
System.out.println("The asynchronous server-socket channel cannot be opened!");
}
}
catch (IOException ex)
{
System.err.println(ex);
}
}
}