Simple client/server programming based on UDP

From , 3 Years ago, written in Java, viewed 183 times.
URL https://pastebin.vip/view/b056eb15
  1. //1. 客户方程序 QuoteClient.java
  2.  
  3. import java.io.*;
  4. import java.net.*;
  5. import java.util.*;
  6.  
  7. class QuoteClient {
  8.         public static void main(String[] args) throws IOException {
  9.                 if (args.length != 1) {
  10.                         // 如果启动的时候没有给出Server的名字,那么出错退出
  11.                         System.out.println("Usage:java QuoteClient <hostname>");
  12.                         // 打印出错信息
  13.                         return; // 返回
  14.                 }
  15.  
  16.                 DatagramSocket socket = new DatagramSocket();
  17.                 // 创建数据报套接字
  18.  
  19.                 byte[] buf = new byte[256]; // 创建缓冲区
  20.                 InetAddress address = InetAddress.getByName(args[0]);
  21.                 // 由命令行给出的第一个参数默认为Server的名字,通过它得到Server的IP信息
  22.                 DatagramPacket packet = new DatagramPacket(buf, buf.length, address,
  23.                                 4445);
  24.                 // 创建DatagramPacket对象
  25.                 socket.send(packet); // 发送
  26.                 packet = new DatagramPacket(buf, buf.length);
  27.                 // 创建新的DatagramPacket对象,用来接收数据报
  28.                 socket.receive(packet); // 接收
  29.                 String received = new String(packet.getData());
  30.                 // 根据接收到的字节数组生成相应的字符串
  31.                 System.out.println("Quote of the Moment:" + received);
  32.                 // 打印生成的字符串
  33.  
  34.                 socket.close(); // 关闭套接口
  35.         }
  36. }
  37.  
  38. // 2. 服务器方程序:QuoteServer.java
  39.  
  40. class QuoteServer {
  41.         public static void main(String args[]) throws java.io.IOException {
  42.                 new QuoteServerThread().start();
  43.                 // 启动一个QuoteServerThread线程
  44.         }
  45. }
  46.  
  47. // 3. 程序QuoteServerThread.java
  48.  
  49. // 服务器线程
  50. class QuoteServerThread extends Thread {
  51.         protected DatagramSocket socket = null;
  52.         // 记录和本对象相关联的DatagramSocket对象
  53.         protected BufferedReader in = null;
  54.         // 用来读文件的一个Reader
  55.         protected boolean moreQuotes = true;
  56.  
  57.         // 标志变量,是否继续操作
  58.  
  59.         public QuoteServerThread() throws IOException {
  60.                 // 无参数的构造函数
  61.                 this("QuoteServerThread");
  62.                 // 以QuoteServerThread为默认值调用带参数的构造函数
  63.         }
  64.  
  65.         public QuoteServerThread(String name) throws IOException {
  66.                 super(name); // 调用父类的构造函数
  67.                 socket = new DatagramSocket(4445);
  68.                 // 在端口4445创建数据报套接字
  69.                 try {
  70.                         in = new BufferedReader(new FileReader(" one-liners.txt"));
  71.                         // 打开一个文件,构造相应的BufferReader对象
  72.                 } catch (FileNotFoundException e) { // 异常处理
  73.                         System.err
  74.                                         .println("Could not open quote file. Serving time instead.");
  75.                         // 打印出错信息
  76.                 }
  77.         }
  78.  
  79.         public void run() // 线程主体
  80.         {
  81.                 while (moreQuotes) {
  82.                         try {
  83.                                 byte[] buf = new byte[256]; // 创建缓冲区
  84.                                 DatagramPacket packet = new DatagramPacket(buf, buf.length);
  85.                                 // 由缓冲区构造DatagramPacket对象
  86.                                 socket.receive(packet); // 接收数据报
  87.                                 String dString = null;
  88.                                 if (in == null)
  89.                                         dString = new Date().toString();
  90.                                 // 如果初始化的时候打开文件失败了,
  91.                                 // 则使用日期作为要传送的字符串
  92.                                 else
  93.                                         dString = getNextQuotes();
  94.                                 // 否则调用成员函数从文件中读出字符串
  95.                                 buf = dString.getBytes();
  96.                                 // 把String转换成字节数组,以便传送
  97.  
  98.                                 InetAddress address = packet.getAddress();
  99.                                 // 从Client端传来的Packet中得到Client地址
  100.                                 int port = packet.getPort(); // 和端口号
  101.                                 packet = new DatagramPacket(buf, buf.length, address, port);
  102.                                 // 根据客户端信息构建DatagramPacket
  103.                                 socket.send(packet); // 发送数据报
  104.                         } catch (IOException e) { // 异常处理
  105.                                 e.printStackTrace(); // 打印错误栈
  106.                                 moreQuotes = false; // 标志变量置false,以结束循环
  107.                         }
  108.                 }
  109.                 socket.close(); // 关闭数据报套接字
  110.         }
  111.  
  112.         protected String getNextQuotes() {
  113.                 // 成员函数,从文件中读数据
  114.                 String returnValue = null;
  115.                 try {
  116.                         if ((returnValue = in.readLine()) == null) {
  117.                                 // 从文件中读一行,如果读到了文件尾
  118.                                 in.close(); // 关闭输入流
  119.                                 moreQuotes = false;
  120.                                 // 标志变量置false,以结束循环
  121.                                 returnValue = "No more quotes. Goodbye.";
  122.                                 // 置返回值
  123.                         } // 否则返回字符串即为从文件读出的字符串
  124.                 } catch (IOException e) { // 异常处理
  125.                         returnValue = "IOException occurred in server";
  126.                         // 置异常返回值
  127.                 }
  128.                 return returnValue; // 返回字符串
  129.         }
  130. }
  131.  

Reply to "Simple client/server programming based on UDP"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website