zoukankan      html  css  js  c++  java
  • 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();
      }
     }

    }

  • 相关阅读:
    284. Peeking Iterator 光是看看下一个值的遍历
    339. Nested List Weight Sum 339.嵌套列表权重总和
    341. Flatten Nested List Iterator展开多层数组
    springcloud之配置中心服务化和高可用
    springcloud之配置中心git
    springcloud之熔断监控Hystrix Dashboard和Turbine
    springcloud之熔断器Hystrix
    springcloud之服务提供与调用
    springcloud之注册中心Eureka
    springcloud之大话springcloud
  • 原文地址:https://www.cnblogs.com/seebro/p/2476557.html
Copyright © 2011-2022 走看看