使用Java制作多点发送程序

2016-02-19 15:58 12 1 收藏

人生本是一个不断学习的过程,在这个过程中,图老师就是你们的好帮手,下面分享的使用Java制作多点发送程序懂设计的网友们快点来了解吧!

【 tulaoshi.com - 编程语言 】


  IP协议是Internet上所有信息的传播手段,UDP(User Datagram Protocol,用户数据报协议)数据报被封装在IP包中,发送到网络上适当的机器。众所周知,大多数IP使用单点发送,即从一个主机发送一个包到另一个主机上。 !-- frame contents -- !-- /frame contents -- 然而,IP协议也具有多点发送的能力,若要使用多点发送时,一个报文标有一组目标主机地址,当报文发出后,整个组都能收到。为支持多点发送,一定范围的IP地址被单独划分出来。这些IP地址是D类地址,其范围是224.0.0.0至239.255.255.255。
  
   IP多点发送(或多点广播)是一种相当新的技术,它是对简单广播的改进。多点广播功能类似于一个单一信息发送到多个接收者的广播,但仅发送给那些等待它的接收者。其思想就是设置一组网络地址作为多点广播地址,其范围是225.0.0.0到239.255.255.255。每一个多点广播地址都被看作一个组。当用户需要从某个地址信息时,加入到该组即可。例如,可能为其股票报价系统设置了地址225.23.32.36作为多点广播地址,假如一个想要接收股票查询报价程序,必须加入到225.23.32.36组中。
  
  制作多点发送的基本知识
  
  在使用Java进行多点发送时,它的MulticastSocket类是实现这一功能的要害。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。进入讨论组讨论。
  制作多点发送应用程序
  
   !-- frame contents -- !-- /frame contents --   下面就运用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 " + "    can be one of 224.x.x.x " + "- 239.x.x.x");
  
  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:" + 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.
  
   }
  
    }进入讨论组讨论。

来源:https://www.tulaoshi.com/n/20160219/1610797.html

延伸阅读
标签: ASP
  我现在在做ASP的安装程序,这样可以方便用户的使用。我使用的InstallShield版本是InstallShield Professional—Standard Edition6.3。   我没有用过这个东东,问题如下: 问题一   通过Project Wizard-generated script 做好了COPY打包工作。可是6.3版本的好象不支持主体Program...Endprogram,因为我看COPY打包...
标签: ASP
  /////////////////////////////////////////////////////////////////////////////// // // FUNCTION: OnMaintUIAfter // // EVENT: MaintUIAfter event is sent after file transfer, when end user runs // installation that has already been installed on the machine. Usually // this happens through Add/Remove Programs app...
标签: ASP
  ////////////////////////////////////////////////////////////////////////////// // //  FUNCTION:  OnFirstUIAfter // //  EVENT:      FirstUIAfter event is sent after file transfer, when installation //        &nbs...
标签: ASP
  6.3版好象是用事件驱动的: ////////////////////////////////////////////////////////////////////////////// // // 函数: OnFirstUIBefore // // 事件: FirstUIBefore event is sent when installation is run for the first // time on given machine. In the handler installation usually displays // UI allowing end user to ...
1、不可以用一个本地类型(如int float)来替换泛型 2、运行时类型检查,不同类型的泛型类是等价的(Pair与Pair是属于同一个类型Pair),这一点要非凡注重,即假如a instanceof Pair==true的话,并不代表a.getFirst()的返回值是一个String类型 3、泛型类不可以继续Exception类,即泛型类不可以作为异常被抛出 4、不可以...

经验教程

325

收藏

78
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部