📜  Java Java类

📅  最后修改于: 2022-05-13 01:54:43.338000             🧑  作者: Mango

Java Java类

此类用于发送和接收多播 IP 数据包。它扩展了 DatagramSocket 类并为加入组提供了额外的功能。所有加入该组的客户端都会收到发送到该组 IP 地址的消息。必须记住,为了向组发送数据包,数据报套接字不必加入组,但为了接收寻址到组的数据包,它必须加入组。该类提供了各种控制多播数据包流的方法,例如设置 ttl、要使用的网络接口等,以及加入和离开组的主要功能。
构造函数:

  • public MulticastSocket() :创建一个多播套接字。使用此构造函数时,我们必须显式设置所有字段,例如组地址、端口号等。
Syntax :public MulticastSocket() 
  • public MulticastSocket(int port) :创建一个绑定在指定端口上的多播套接字。
Syntax :public MulticastSocket(int port) 
Parameters : 
port : port number to bind this socket to
  • public MulticastSocket(SocketAddress bindaddr) :创建一个多播套接字并将其绑定到指定的套接字地址。如果地址为空,则创建一个未绑定的套接字。
Syntax :public MulticastSocket(SocketAddress bindaddr) 
Parameters : 
bindaddr : Socket address to bind this socket to

方法 :

  • setTTL(已弃用):用于设置从此多播套接字发送的数据包的生存时间,限制数据包的范围。此方法已弃用,因为它使用字节作为设置 TTL 的参数。 setTimeToLive() 被用来代替它。
Syntax :public void setTTL(byte ttl)
Parameters :
ttl : the time to live
  • setTimeToLive() :此方法用于代替上述方法,因为它使用 int 值作为参数。指定的 ttl 必须在 0<=ttl<=255 范围内,否则会抛出非法参数异常。
Syntax :public void setTimeToLive(int ttl)
Parameters :
ttl : the time to live
  • getTTL(已弃用):用于获取从此多播套接字发送的数据包的默认生存时间
Syntax :public byte getTTL()
  • getTimeToLive() :返回从此多播套接字发送的数据包的默认生存时间。
Syntax :public void getTimeToLive()
  • joinGroup() :用于加入多播组。加入组后,客户端将开始接收发送到该组播组地址的所有数据包。此方法采用要加入的组的 InetAddress。可以使用 setInterface() 和 setNetworkInterface() 方法对其行为进行进一步更改。
Syntax :public void joinGroup(InetAddress mcastaddr)
               throws IOException
Parameters :
mcastaddr : multicast address to join
Throws :
IOException : if error occurs while joining or the address is
not a multicast address.

另一种重载方法,它还指定加入组时要使用的网络接口。

Syntax :public void joinGroup(SocketAddress mcastaddr,
             NetworkInterface netIf)
               throws IOException
Parameters :
mcastaddr : multicast address to join
netIf : network interface to use while joining
Throws :
IOException : if error occurs while joining or the address is
not a multicast address.
  • leaveGroup() :用于离开多播组。离开群组后,客户端将无法接收发往该群组地址的数据包。
Syntax :public void leaveGroup(InetAddress mcastaddr)
               throws IOException
Parameters :
mcastaddr : multicast address to leave
Throws :
IOException : if error occurs while leaving

另一种用于离开组的重载方法也允许指定离开组的网络接口。

Syntax :public void leaveGroup(SocketAddress mcastaddr,
             NetworkInterface netIf)
               throws IOException
Parameters :
mcastaddr : multicast address to leave
netIf : network interface on which to leave
Throws :
IOException : if error occurs while leaving
  • setInterface() :用于设置组播网络接口。
Syntax :public void setInterface(InetAddress inf)
                  throws SocketException
Parameters :
inf : the inetaddress
Throws :
SocketException : if error in underlying protocol
  • getInterface() :返回用于多播数据包的网络接口。
Syntax :public InetAddress getInterface()
                         throws SocketException
Throws :
SocketException : if error in underlying protocol
  • setNetworkInterface() :用于指定此套接字上传出数据包的网络接口。
Syntax :public void setNetworkInterface(NetworkInterface netIf)
                         throws SocketException
Parameters :
netIf : network interface to use
Throws :
SocketException : if error in underlying protocol
  • getNetworkInterface() :返回此套接字上传出数据包的网络接口。
Syntax :public NetworkInterface getNetworkInterface()
                                     throws SocketException
Throws :
SocketException : if error in underlying protocol
  • setLoopbackMode() :用于指定组播数据包是否将被环回到本地套接字。
Syntax : public void setLoopbackMode(boolean disable)
                     throws SocketException
Parameters :
disable : true or false
  • getLoopbackMode() :返回环回模式的设置。
Syntax : public vboolean getLoopbackMode()
                     throws SocketException
  • send()(不推荐使用):用于将数据包发送到多播组。它允许为传出数据包指定 ttl,而不是使用 setTimeToLive() 方法指定的默认值。它不会更改此值,而是仅将指定值用于当前数据包。应使用以下代码行代替此方法。
    int ttl=msock.getTimeToLive();
    msock.setTimeToLive(newttl);
    msock.send(数据包);
    msock.setTimetoLive(ttl);
Syntax :public void send(DatagramPacket p,
                   byte ttl)
          throws IOException
Parameters :
p : datagram packet to send
ttl : hte time to live
Throws :
IOException : If error occurs while setting the ttl

Java实现:

Java
//Java program to illustrate various
//MulticastSocket class methods
import java.io.IOException;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.NetworkInterface;
import java.util.Enumeration;
 
public class multisock
{
    public static void main(String[] args) throws IOException
    {
         
        MulticastSocket ms = new MulticastSocket();
        InetAddress ip = InetAddress.getByName("224.168.1.124");
 
        // setTTL() method
        // this method is deprecated and instead
        // use setTimeToLive() method
        // Un-commenting below line would throw
        // a warning as the method is deprecated
        ms.setTTL((byte) 25);
 
        // setTimeToLive() method, will override
        // setting by setTTL() method
        ms.setTimeToLive(20);
 
        // getTTL() method
        // deprecated, so use getTimeToLive() method instead
        System.out.println("TTL : " + ms.getTTL());
 
        // getTimeToLive() method
        System.out.println("Time to Live : " +
                             ms.getTimeToLive());
 
        NetworkInterface nif = NetworkInterface.getByIndex(1);
        Enumeration enu = nif.getInetAddresses();
        InetAddress intadd = enu.nextElement();
 
        // setInterface() method
        ms.setInterface(intadd);
 
        // getInterface() method
        System.out.println("Interface : " + ms.getInterface());
 
        // setNetworkInterface() method
        ms.setNetworkInterface(nif);
 
        // getNetworkInterface() method
        System.out.println("Network Interface : " +
                           ms.getNetworkInterface());
 
        // setLoopbackMode() method
        ms.setLoopbackMode(true);
 
        // getLoopbackMode() method
        System.out.println("Loopback mode : " +
                            ms.getLoopbackMode());
    }
 
}


输出 :

TTL : 20
Time to Live : 20
Interface : /127.0.0.1
Network Interface : name:lo (Software Loopback Interface 1)
Loopback mode : true