0

我有这些课程:

import java.io.BufferedReader;
import java.io.InputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Arrays;

public class Test {

public final static int DEFAULT_PORT = 9; 
public final static int MAX_PACKET_SIZE = 65507;

public static void main(String[] args) {
    byte[] buffer = new byte[MAX_PACKET_SIZE];
    try {
        DatagramSocket udpSocket = new DatagramSocket(DEFAULT_PORT); 
        DatagramPacket packet = new DatagramPacket(buffer, buffer.length); 
        int i = 0;
        while (true) {
            udpSocket.receive(packet);                
            System.out.println(i++ +" "+ new String(packet.getData(), 0, packet.getData().length));
            DatagramPacket theOutput = new DatagramPacket(packet.getData(), packet.getData().length, packet.getAddress(), packet.getPort()); 
            udpSocket.send(theOutput);
            packet.setLength(buffer.length); 
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
} 
}

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class Test2 {

public final static int DEFAULT_PORT = 10; 
public final static int MAX_PACKET_SIZE = 65507;

public static void main(String[] args) {

    byte[] buffer = new byte[MAX_PACKET_SIZE];
    try {
        DatagramSocket udpSocket = new DatagramSocket(DEFAULT_PORT); 
        DatagramPacket packet = new DatagramPacket(buffer, buffer.length); 
        udpSocket.send(new DatagramPacket("prova".getBytes(), "prova".getBytes().length, InetAddress.getByName("localhost"), 9));
        int i = 0;
        while (true) {
            udpSocket.receive(packet);                
            System.out.println(i++ +" "+ new String(packet.getData(), 0, packet.getData().length));
            DatagramPacket theOutput = new DatagramPacket(packet.getData(), packet.getData().length, packet.getAddress(), packet.getPort()); 
            udpSocket.send(theOutput);
            packet.setLength(buffer.length); 
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
}

现在,在 Windows 上,如果我运行第一类“Test”,然后运行“Test2”类,第二个将数据包发送到第一个,然后启动一个循环,在该循环中连续交换数据包。问题是,如果我在 OS X 上运行相同的类,第一个数据包到达“Test”类但“Test2”类似乎没有收到响应。

4

0 回答 0