• fortune random server demo


     

    //server

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

    public class FortuneServer
    {
     private static final String[] fortunes = { "Buy Low and Sell High",
           "Eat Your Vegetables",
           "Good Walls Make Good Neighbors",
           "Never Underestimate Your Competition",
           "A Clean Camp is a Happy Camp",
           "Be Sure to Test Every Line of Code You Write"
          };
                                                   
     public static void main(String[] args) throws IOException {
      Socket client = null;
      ServerSocket sock = null;

      try {
        sock = new ServerSocket(6012);
        
        while (true) {
         client = sock.accept();
         System.out.println("server = " + sock);
         System.out.println("client = " + client);

         PrintWriter pout = new PrintWriter(client.getOutputStream(), true);
         pout.println(fortunes[(int)(java.lang.Math.random() * fortunes.length)] );

         pout.close();
         client.close();
       }
      }
      catch (IOException ioe) {
        System.err.println(ioe);
      }
      finally {
       if (sock != null)
        sock.close();
       if (client != null)
        client.close();
      }
     }
    }

    //client

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

    public class FortuneClient
    {
     public static void main(String[] args) throws IOException
     {
      InputStream in = null;
      BufferedReader bin = null;
      Socket sock = null;
      
      try
      {
       sock = new Socket("127.1.1.1", 6012);
       in = sock.getInputStream();
       bin = new BufferedReader(new InputStreamReader(in));
       
       String line;
       while ((line = bin.readLine()) != null)
       {
        System.out.println(line);
       }
      }
      catch (IOException ioe)
      {
       System.err.println(ioe);
      }
      finally
      {
       sock.close();
      }
     }

    }

  • 相关阅读:
    你现在的技能,是不是只剩下给别人点赞了?
    web编码(转)
    Android 官方文档:(二)应用清单 —— 2.26 <uses-permission>标签
    BackTrack5 (BT5)无线password破解教程之WPA/WPA2-PSK型无线password破解
    开源 java CMS
    EM算法原理
    OpenCV2马拉松第14圈——边缘检測(Sobel,prewitt,roberts)
    winzip15.0注冊码
    什么是积分墙?
    Bitmap工具类
  • 原文地址:https://www.cnblogs.com/seebro/p/2476557.html
Copyright © 2020-2023  润新知