📄 multicastserver2.java
字号:
/** * @author wangmj *可以通过这个方法将服务器收到的信息广播给大家。 * 首先 java MulticastServer2 * 然后 java MulticastClient * 服务器最终会把从文件中读出的信息广播给大家。 * MulticastServer2.java */import java.io.BufferedReader;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.InetAddress;import java.net.UnknownHostException;import java.util.Date;public class MulticastServer2 { public static void main(String[] args) throws java.io.IOException{ new MulticastServerThread().start(); }}class MulticastServerThread extends QuoteServerThread2{ private long FIVE_SECONDS = 5000; //public MulticastServerThread public MulticastServerThread() throws IOException{ super("MulticastServerThread"); } boolean moreQuotes = true; String dString = null; public void run(){ while(moreQuotes){ try{ byte[] buf = new byte[256]; //construct quote 引用, 引证, 提供, 提出, 报(价) Date date = null; date = new Date(); if(dString == null){ dString = new String(date.toString()); } else { dString = getNextQuote(); } buf = dString.getBytes(); //send it InetAddress group = null; try { group = InetAddress.getByName("230.0.0.1"); } catch (UnknownHostException e) { e.printStackTrace(); } DatagramPacket packet = new DatagramPacket(buf,buf.length,group,4446); socket.send(packet); //sleep for a while try{ sleep((long)(Math.random()*FIVE_SECONDS)); }catch(InterruptedException e){ e.getMessage(); } }catch(IOException e){ e.printStackTrace(); moreQuotes = false; } } socket.close(); }}class QuoteServerThread2 extends Thread{ protected DatagramSocket socket = null; protected BufferedReader in = null; protected boolean moreQuotes = true; public QuoteServerThread2() throws IOException{ this("QuoteServerThread"); } public QuoteServerThread2(String name) throws IOException{ super(name); socket = new DatagramSocket(4446); try{ in = new BufferedReader(new FileReader("one-liners.txt")); }catch(FileNotFoundException e){ System.err.println("Could not open quote file.Serving time instead."); } } public void run(){ while(moreQuotes){ try{ byte[] buf = new byte[256]; //receive reques DatagramPacket packet = new DatagramPacket(buf,buf.length); socket.receive(packet); //figure out response String dString = null; if(in == null){ dString = new Date().toString(); }else{ dString = getNextQuote(); } buf = dString.getBytes(); //send the response to the client at "address" and "potr" InetAddress address = packet.getAddress(); int port = packet.getPort(); packet = new DatagramPacket(buf,buf.length,address,port); socket.send(packet); }catch(IOException e){ e.printStackTrace(); moreQuotes = false; } } socket.close(); } protected String getNextQuote(){ String value = null; try{ if((value = in.readLine())==null){ in.close(); moreQuotes = false; value="No more quotes.Goodbye."; } }catch(IOException e){ value = "IOException occurred in server"; }finally{ System.out.println("getNextQuote "+value); } return value; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -