使用Java制作多点发送程序

发布时间:2024-02-25 13:54:06

使用Java制作多点发送程序                 2001-03-23· 林建刚 ·yesky

  IP协议是Internet上所有信息的传播手段,UDP(User Datagram Protocol,用户数据报协议)数据报包装在IP包中,发送到网络上的适当机器。众所周知,大多数IP使用单点发送,即从一个主机发送一个包到另一个主机。然而,IP协议也有多点发送的能力。如果要使用多点发送,一个报纸标记有一组目标主机地址。当报纸发布时,整个小组都可以收到它。为了支持多点发送,将一定范围的IP地址分开划分。这类IP地址为D类地址,范围为224.0.0.0至239.255.255.2555.255。

   IP多点发送(或多点广播)是一项相当新的技术,是对简单广播的改进。多点广播功能类似于向多个接收者发送单一信息的广播,但只发送给等待它的接收者。其思想是设置一组网络地址作为多点广播地址,其范围为225.0.0.0到239.255.255.2555.255。每个多点广播地址都被视为一个组。当用户需要从地址信息中添加到该组时。例如,225.23.32.36作为多点广播地址,可以为其股票报价系统设置地址。如果要接收股票查询报价程序,必须加入225.23.32.36组。

  一、制作多点发送的基础知识

  它的MulticastSocket类在使用Java进行多点发送时,是实现此功能的关键。Multicastsocket允许用户发送和接收多点发送IP的数据报告。如果要发送或接收多点广播数据,首先要创建多点广播套接字(多点广播套接字类似于数据报套接字,其实Multicastsocket是Datagramsocket的子类)。如果要发送数据报告,可以使用缺失的端口号创建多点广播套接字,也可以在构造器指定的端口号创建多点广播套接字:

    public MulticastSocket()throws IOException

    public MulticastSocket(int portNumber)throws IOException

  为了添加一个多点广播地址,可以使用jionGroup方法;如果你想离开一个组,使用leaveGroup方法:

    public void jionGroup(InetAddress multicastAddr)throws IOException

    public void leaveGroup(InetAddress multicastAddr)throws IOException

  在某些系统中,可能有多个网络接口。这可能会给多点广播带来问题,因为用户需要在指定的接口上进行调查。通过调用setinterface,可以选择多点广播套接字使用的接口:

    public void setInterface(InetAddress interface)throws SocketException

  多点广播套接字的接口可以通过调用getinterface来查询:

    public InetAddress getInterface()throws SocketException

  用户使用send方法发送多点发送的数据:

    public synchronized void send(DatagramPacket packet, byte timeTolive)throws IOException

TTL值指定数据包应跨越多少个网络。当TTL为0时,指定数据包应停留在本地主机上;当TTL值为1时,指定数据包应发送到本地网络;当TTL值为32时,仅发送到本网站的网络;当TTL为64时,数据包应保留在本地区;当TTL值为128时,这意味着数据应该保留在本大洲;当TTL为255时,这意味着数据包应该发送到所有地方;如果使用缺失的send方法,TTL值为1。

  二、制作多点发送应用程序

  使用Java制作多点发送应用程序。其中程序1(MulticastCastSender.java)它是一个将数据发送到指定的多点发送IP地址的程序。该程序运行时使用两个参数:一个是指定的多点发送IP地址,另一个是指定侦听应用程序的UDP端口。其中,Main()方法保证了这些参数收到后,实例化为Multicastsender对象。此外,构造器使用多点发送IP地址的String对象,创建Inetaddress实例,然后在动态分配端口上创建MulticastSocket,然后构造器进入while循环,从标准输入逐步输入。该程序将每行前512个字节包装到标有地址的DatagramPacket中,并通过MulticastSocket发送数据报告。

  程序Multicastsender.java的源代码如下:

  import java.net.*; // Import package names used.

  import java.io.*;

  class MultiCastSender {

    private static final byte TTL = 1;

    private static final int DATAGRAM_BYTES = 1024;

    private int mulcastPort;

    private InetAddress mulcastIP;

    private BufferedReader input;

    private MulticastSocket mulcastSocket;

    public static void main(String[] args) {

     // This must be the same port and IP address used by the receivers.

     if (args.length != 2) {

      System.out.print("Usage: MultiCastSender " + " \n\t                can be one of 224.x.x.x " + "- 239.x.x.x\n");

      System.exit(1);

    }

    MultiCastSender send = new MultiCastSender(args);

    System.exit(0);

   }

  public MultiCastSender(String[] args) {

    DatagramPacket mulcastPacket; // UDP datagram.

    String nextLine; // Line from STDIN.

    byte[] mulcastBuffer; file:// Buffer for datagram.

    byte[] lineData; // The data typed in.

    int sendLength; file:// Length of line.

    input = new BufferedReader(new InputStreamReader(System.in));

    try {

      // Create a multicasting socket.

      mulcastIP = InetAddress.getByName(args[0]);

      mulcastPort = Integer.parseInt(args[1]);

      mulcastSocket = new MulticastSocket();

      } catch(UnknownHostException excpt) {

         System.err.println("Unknown address: " + excpt);

         System.exit(1);

         } catch(IOException excpt) {

           System.err.println("Unable to obtain socket: " + excpt);

           System.exit(1);

             }

     try {

       file:// Loop and read lines from standard input.

       while ((nextLine = input.readLine()) != null) {

         mulcastBuffer = new byte[DATAGRAM_BYTES];

         file:// If line is longer than your buffer, use the length of the buffer available.

         if (nextLine.length() > mulcastBuffer.length) {

          endLength = mulcastBuffer.length;

          // Otherwise, use the line's length.

          }          else {

          sendLength = nextLine.length();

           }

         // Convert the line of input to bytes.

         lineData = nextLine.getBytes();

         // Copy the data into the blank byte array          file://which you will use to create the DatagramPacket.

         for (int i = 0; i < sendLength; i++) {

           mulcastBuffer[i] = lineData[i];

          }

         mulcastPacket=new DatagramPacket (mulcastBuffer, mulcastBuffer.length,                              mulcastIP, mulcastPort);

         // Send the datagram.

      try {

        System.out.println("Sending:\t" + nextLine);

        mulcastSocket.send(mulcastPacket,TTL);

        } catch(IOException excpt) {

          System.err.println("Unable to send packet: " + excpt);

          }

        }

       } catch(IOException excpt) {

         System.err.println("Failed I/O: " + excpt);

         }

       mulcastSocket.close(); file:// Close the socket.

     }

    }  程序2(MultiCastReceiver.java)发送者通过接收多点发送的数据报告来实现。该应用程序有两个参数,必须对应IP地址和激活Multicastsender的端口。Main()方法首先检查命令的参数,然后创建Multicastreceiver对象。该对象的构造器用于激活应用程序的端口,创建Inetadress和Multicastsocket。将多点发送者添加到包含在Inetaddress中的地址中,然后进入循环。该对象的构造器从一套接字中接收数据报告,并打印包含在数据报告中的数据,包括发送数据包的计算机和端口。

   程序2(MultiCastReceiver)源代码如下:

    import java.net.*; // Import package names used.

    import java.io.*;

    class MultiCastReceiver {

     private static final int DATAGRAM_BYTES = 1024;

     private int mulcastPort;

     private InetAddress mulcastIP;

     private MulticastSocket mulcastSocket;

     private boolean keepReceiving = true;

     public static void main(String[] args) {

      // This must be the same port and IP address used by the sender.

      if (args.length != 2) {

       System.out.print("Usage: MultiCastReceiver \n\t can be one of " + "224.x.x.x - 239.x.x.x\n");

       System.exit(1);

      }

     multiCastReceiver send = new MultiCastReceiver(args);

       System.exit(0);

       }

     public MultiCastReceiver(String[] args) {

      DatagramPacket mulcastPacket; // Packet to receive.

      byte[] mulcastBuffer; // byte[] array buffer

      InetAddress fromIP; file:// Sender address.

      int fromPort; // Sender port.

      String mulcastMsg; // String of message.

     try {

       // First, set up your receiving socket.

      mulcastIP = InetAddress.getByName(args[0]);

      mulcastPort = Integer.parseInt(args[1]);

      mulcastSocket = new MulticastSocket(mulcastPort);

      // Join the multicast group.

      mulcastSocket.joinGroup(mulcastIP);

      } catch(UnknownHostException excpt) {

       System.err.println("Unknown address: " + excpt);

       System.exit(1);

      } catch(IOException excpt) {

       System.err.println("Unable to obtain socket: " + excpt);

       System.exit(1);

      }

      while (keepReceiving) {

      try {

       // Create a new datagram.

       mulcastBuffer = new byte[DATAGRAM_BYTES];

       mulcastPacket = new DatagramPacket(mulcastBuffer, mulcastBuffer.length);

       // Receive the datagram.

       mulcastSocket.receive(mulcastPacket);

       fromIP = mulcastPacket.getAddress();

       fromPort = mulcastPacket.getPort();

       mulcastMsg = new String(mulcastPacket.getData());

       // Print out the data.

       System.out.println("Received from " + fromIP + " on port " + fromPort + ": " +                   mulcastMsg);

       } catch(IOException excpt) {

      system.err.println("Failed I/O: " + excpt);

      }

     }

    try {

     mulcastSocket.leaveGroup(mulcastIP); file:// Leave the group.

      } catch(IOException excpt) {

     System.err.println("Socket problem leaving group: " + excpt);

     }

     mulcastSocket.close();    // Close the socket.

    }

   public void stop() {

    if (keepReceiving) {

     keepReceiving = false;

     }

    }

   }

  如果要使用该应用程序,首先编译Multicastsender和Multicastreceiver,然后将Multicastreceiver传输到其他机器,以示多个参与者接收报告。最后,Java解释器操作应用程序。例如,向225.22.32.6组在端口111上发送多点发送报文,应按以下方法进行:

   -/class ->java 225.2.32.6 1111

   Here is just to test multicast message.

   Sending: Here is just to test multicast message.

   Do you received it?

   Sending: Do you received it?

为了接收这些报纸,Multicastreceiver应用程序应该在一个或多个系统上运行,这些系统应该添加相同的多点发送组225.2.32.6,并在同一端口1111上侦听:

   -/class ->java MultiCastReceiver 225.2.32.6 1111

   Received from 225.106.36.32 on port 32911: Here is just to test multicast message.

   Received from 225.106.36.32 on port 32911: Do you received it?

上一篇 用Java编写邮件发送程序
下一篇 JDK1.3 中的本地绘制支持

文章素材均来源于网络,如有侵权,请联系管理员删除。

标签: Java教程Java基础Java编程技巧面试题Java面试题