multitimeclient.java

来自「java3D game engine design of the source 」· Java 代码 · 共 39 行

JAVA
39
字号

// MultiTimeClient.java
// Andrew Davison, June 2003, dandrew@ratree.psu.ac.th

/* Listen for packets coming from the specified multicast group.
   The packets should hold the current date.

   The client runs forever, so doesn't use leaveGroup().
*/

import java.net.*;
import java.io.*;

public class MultiTimeClient
{
  private static final String MHOST = "228.5.6.7";
  private static final int PORT = 6789;

  public static void main(String args[]) throws IOException
  {
     InetAddress address = InetAddress.getByName(MHOST);        
     MulticastSocket msock = new MulticastSocket(PORT);
     msock.joinGroup(address);

     byte[] buf = new byte[1024];
     DatagramPacket packet = new DatagramPacket(buf, buf.length);
     String dateStr;
     while(true){                                
       // buf = new byte[1024];
       // packet = new DatagramPacket(buf, buf.length);
       msock.receive(packet); 
       dateStr = new String( packet.getData() ).trim();
       System.out.println(packet.getAddress() + " : " + dateStr);
     }
   }  // end of main()
         
}  // end of MultiTimeClient class

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?