pablohf

Untitled

Sep 25th, 2012
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.71 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package dailyadviceserver;
  6.  
  7. /**
  8.  *
  9.  * @author pablo
  10.  */
  11. public class DailyAdviceServer {
  12.  
  13.     /**
  14.      * @param args the command line arguments
  15.      */
  16.     public static void main(String[] args) {
  17.         Server server;
  18.         server = new Server();
  19.         server.go();
  20.     }
  21. }
  22. /-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-\-/\-/\-/\-/\-/-\/\-/\-/\-/\-/\-\/\-/\-/\/-\
  23. /*
  24.  * To change this template, choose Tools | Templates
  25.  * and open the template in the editor.
  26.  */
  27. package dailyadviceserver;
  28.  
  29. import java.io.IOException;
  30. import java.io.PrintWriter;
  31. import java.net.ServerSocket;
  32. import java.net.Socket;
  33.  
  34. /**
  35.  *
  36.  * @author pablo
  37.  */
  38. public class Server {
  39.     String[] adviceList = {"Morda pedaços menores", "Use o jeans apertado. Não, ele NÃO faz você parecer gorda.", "Só vou dizer uma palavra: inapropriado", "Pelo menos hoje, seja honesta. Diga a seu chefe o que realmente pensa", "Reconsidere esse corte de cabelo."};
  40.  
  41.     public void go() {
  42.  
  43.         try {
  44.             ServerSocket serverSock = new ServerSocket(4242);
  45.  
  46.             while (true) {
  47.                 Socket sock = serverSock.accept();
  48.                 String advice;
  49.                 try (PrintWriter writer = new PrintWriter(sock.getOutputStream())) {
  50.                     advice = getAdvice();
  51.                     writer.println(advice);
  52.                 }
  53.                 System.out.println(advice);
  54.             }
  55.         } catch (IOException ex) {
  56.         }
  57.     }
  58.  
  59.     private String getAdvice() {
  60.         int random = (int) (Math.random() * adviceList.length);
  61.         return adviceList[random];
  62.     }
  63.    
  64. }
Advertisement
Add Comment
Please, Sign In to add comment