2011-12-12 45 views
5

Tôi đang viết một lớp bằng Java được sử dụng để đơn giản hóa rất nhiều quá trình phát đa hướng. Tuy nhiên, tôi có hai vấn đề lớn với nó:Java Multicast Gửi dữ liệu, không nhận được

  1. Lớp này gửi dữ liệu (Tôi có thể xác minh điều này với màn hình mạng, Wireshark) nhưng không nhận được dữ liệu của bất kỳ người nào khác trong cùng một nhóm.
  2. Trên một số máy, TTL gói tin được vượt quá quá cảnh (một lần nữa, theo Wireshark).

Có ai vui lòng giúp tôi không? Tôi đã thử và tìm kiếm câu trả lời trong nhiều giờ, và có vẻ như mã của tôi tuân theo tất cả các thủ tục cơ bản để kết nối, nối, gửi và nhận dữ liệu từ một máy chủ đa hướng.

Dưới đây là một đoạn trong phần có liên quan của lớp:

Multicaster lớp: sử dụng

public class Multicaster { 
    public int port = 5540; 

    protected String IPAddress; 

    private MulticastSocket msConn; 
    private InetAddress netAddr; 

    public Multicaster(String IPAddress) { 
    this.IPAddress = IPAddress; 
    } 

    public String recieveData() { 
    byte[] buf = new byte[1000]; 
    DatagramPacket pack = new DatagramPacket(buf, buf.length); 

    try { 
     this.msConn.receive(pack); 
     new Message(pack); 
     String out = new String(pack.getData()); 
     return out.substring(0, pack.getLength()); 
    } catch (IOException e) { 
     return new String(""); 
    } 
    } 

    public void joinGroup() { 
    try { 
     this.msConn.joinGroup(this.netAddr); 
    } catch (IOException e) { 
    //This error shouldn't occur since it would caught by the connect() method during initial connection to the host 
    } 
    } 

    public void connect() throws MulticasterInitException { 
    //Try to create a multicast connection on the given IP address and port 
    try { 
     try { 
     //Create a multicast connection on a given port, throws UnknownHostException 
     this.msConn = new MulticastSocket(this.port); 

     //If all goes well, then create a connection to a given IP address using the above port number, throws IOException and SecurityException 
     this.netAddr = InetAddress.getByName(this.IPAddress); 
     } 
    } 

    /** 
    * Here all of the possible exceptions that are thrown above 
    * are caught and handled here. This works just fine. 
    */ 
    } 

    public void sendData(String data) throws MulticasterSendException { 
    DatagramPacket packet = new DatagramPacket(data.getBytes(), data.length(), this.netAddr, this.port); 

    try { 
     this.msConn.send(packet); 
    } catch (IOException e) { 
     throw new MulticasterSendException("Java could not communicate with the server. Please check your network connections.", e); 
    } 
    } 
} 

mẫu để gửi dữ liệu:

Multicaster multicast = new Multicaster("239.0.0.0"); 

try { 
    multicast.connect(); 
} catch (MulticasterInitException e) { 
    //Handle exception... 
} 

multicast.joinGroup(); 

try { 
    multicast.sendData("Hi"); 
} catch (MulticasterSendException e) { 
    //Handle exception... 
} 

sử dụng mẫu để nhận dữ liệu:

Multicaster multicast = new Multicaster("239.0.0.0"); 

try { 
    multicast.connect(); 
} catch (MulticasterInitException e) { 
    //Handle exception... 
} 

multicast.joinGroup(); 
System.out.print(multicast.recieveData()); 
+0

Bạn có thể tham khảo nhận xét của tôi vì sự cố có liên quan đến [http://stackoverflow.com/questions/8471447/java-multicast-sample-program-is-unable-to-deliver-packets-within-lan- over-ho] (http://stackoverflow.com/questions/8471447/java-multicast-sample-program-is-unable-to-deliver-packets-within-lan-across-ho) – ecle

Trả lời

6

Tôi đã gặp phải các vấn đề tương tự trước đây và phải đảm bảo rằng NetworkInterface được chỉ định ở phía bên nhận.

SocketAddress socketAddress = new SocketAddress(groupIp, groupPort); 
NetworkInterface networkInterface = NetworkInterface.getByName(interfaceName); 

socket.joinGroup(socketAddress, networkInterface); 

đâu interfaceName là một trong những tên giao diện hiển thị khi chạy ifconfig trên Linux hoặc Mac.

+0

Bạn có thể vui lòng cho tôi đề xuất về https://stackoverflow.com/questions/45162552/multicast-receiver-unable-to-capture-the-data vấn đề của tôi? – kit

Các vấn đề liên quan