Oi pessoal do GUJ.
Estou com uma dúvida em relação ao execício sobre Sockets no livro Use a Cabeça: Java. Estou tentando criar um aplicativo que dê conselhos aleatórios quando o usuário (client) se conecta ao servidor (host/server). Mas quando eu rodo o aplicativo, nada acontece, não recebo conselho algum! Aqui está o meu código:
package app.sockettest;
import java.io.*;
import java.net.*;
public class SocketApp {
String[] listaConselhos = {"Coma devagar e mastigue mais.", "Viva o presente!",
"Durma bem.", "Pratique um exercício físico."};
public void configureSocket() {
try {
ServerSocket sSock = new ServerSocket(3636);
while(true) {
Socket sock = sSock.accept();
PrintWriter writer = new PrintWriter(sock.getOutputStream());
String conselho = getConselho();
writer.close();
System.out.println(conselho);
}
} catch(IOException e) {
e.printStackTrace();
}
}
public String getConselho() {
int random = (int) (Math.random() * listaConselhos.length);
return listaConselhos[random];
}
public static void main(String[] args) {
SocketApp app = new SocketApp();
app.configureSocket();
}
}
Não há nenhum envio de StackTrace, então isso mostra que ele conectou. (?)