嗨,我想遍历n
我拥有的端口数量列表,并为每个端口创建一个 DatagramSocket:
for(int i = 0; i < portList.size(); i++) {
DatagramSocket socket[i] = new DatagramSocket();
socket[i].connect(InetAddress.getLocalHost(), portList.get(i));
}
我知道我不应该使用socket[i]
. 只是为了表达我的意思,上面会生成:
DatagramSocket socket1 = new DatagramSocket();
socket1.connect(InetAddress.getLocalHost(), 2000);
DatagramSocket socket2 = new DatagramSocket();
socket2.connect(InetAddress.getLocalHost(), 2001);
DatagramSocket socket3 = new DatagramSocket();
socket3.connect(InetAddress.getLocalHost(), 2002);
DatagramSocket socket4 = new DatagramSocket();
socket4.connect(InetAddress.getLocalHost(), 2003);
我不太擅长 Java,所以这可能是一个愚蠢的问题:P