Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package dailyadviceserver;
- /**
- *
- * @author pablo
- */
- public class DailyAdviceServer {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- Server server;
- server = new Server();
- server.go();
- }
- }
- /-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-\-/\-/\-/\-/\-/-\/\-/\-/\-/\-/\-\/\-/\-/\/-\
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package dailyadviceserver;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.net.ServerSocket;
- import java.net.Socket;
- /**
- *
- * @author pablo
- */
- public class Server {
- 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."};
- public void go() {
- try {
- ServerSocket serverSock = new ServerSocket(4242);
- while (true) {
- Socket sock = serverSock.accept();
- String advice;
- try (PrintWriter writer = new PrintWriter(sock.getOutputStream())) {
- advice = getAdvice();
- writer.println(advice);
- }
- System.out.println(advice);
- }
- } catch (IOException ex) {
- }
- }
- private String getAdvice() {
- int random = (int) (Math.random() * adviceList.length);
- return adviceList[random];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment