2011-12-12 38 views
5

Sto scrivendo una classe in Java che viene utilizzata per semplificare notevolmente il processo di multicasting. Tuttavia, sto avendo due grossi problemi con esso:Java Multicast Invio di dati, non ricezione

  1. La classe invia i dati (posso verificare questo con il mio monitor rete, Wireshark), ma i dati non viene ricevuto da tutti gli altri nello stesso gruppo.
  2. Su alcune macchine, il pacchetto di invio TTL viene superato in transito (di nuovo, secondo Wireshark).

Qualcuno potrebbe aiutarmi? Ho cercato e cercato le risposte per ore, e sembra che il mio codice segua tutte le procedure di base per connettersi, unire, inviare e ricevere dati da un host multicast.

Ecco un frammento delle porzioni rilevanti della classe: class

Multicaster: utilizzo

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); 
    } 
    } 
} 

di esempio per inviare i dati:

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... 
} 

utilizzo di esempio per ricevere i dati:

Multicaster multicast = new Multicaster("239.0.0.0"); 

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

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

È possibile fare riferimento ai miei commenti poiché il problema è relativo a [http://stackoverflow.com/questions/8471447/java-multicast-sample-program-is-unable-to-deliver-packets-within-lan- across-ho] (http://stackoverflow.com/questions/8471447/java-multicast-sample-program-is-unable-to-deliver-packets-within-lan-across-ho) – ecle

risposta

6

Ho avuto problemi simili in precedenza e ho dovuto accertarmi che NetworkInterface fosse specificato sul lato ricevente.

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

socket.joinGroup(socketAddress, networkInterface); 

Dove interfaceName è uno dei nomi di interfaccia indicati durante l'esecuzione ifconfig su Linux o Mac.

+0

Puoi per favore darmi un suggerimento su https://stackoverflow.com/questions/45162552/multicast-receiver-unable-to-capture-the-data il mio problema? – kit

Problemi correlati