A pesar de ser un ejercicio de socket es un problema de java no de socket como tal.Tengo un socket cliente servidor por usuario password y una vez meto lo datos para que continúe la ejecución y me diga login correcto debo de dar un intro en la consola como hago para no tener que hacerlo?
Clase servidor
public class JS15 {
private int currentTot;
ServerSocket serversocket;
protected static boolean listening = true;
Socket client;
int bytesRead;
Conexion c = new Conexion();
public void start() throws IOException {
System.out.println("Connection Starting on port:" + c.getPort());
//make connection to client on port specified
serversocket = new ServerSocket(c.getPort());
//accept connection from client
System.out.println("Waiting for connection from client");
while (listening) {
new Hilo(serversocket.accept()).start();
System.out.println("conectado");
}
}
public static void main(String[] args) {
JS15 server = new JS15();
try {
server.start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Hilo
public class Hilo extends Thread {
private Socket client;
private BufferedReader input;
private PrintWriter output;
Conexion c = new Conexion();
public Hilo(Socket socket) {
super("EchoThread");
this.client = socket;
}
public void logInfo() throws Exception {
//open buffered reader for reading data from client
input = new BufferedReader(new InputStreamReader(client.getInputStream()));
String username = input.readLine();
System.out.println("SERVER SIDE " + username);
String password = input.readLine();
System.out.println("SERVER SIDE " + password);
//open printwriter for writing data to client
for (int i = 0; i < c.getLogin().size(); i++) {
output = new PrintWriter(new OutputStreamWriter(client.getOutputStream()));
if (username.equals(c.getLogin().get(i).getUsuario()) && password.equals(c.getLogin().get(i).getClave())) {
output.println("Login correcto");
} else if (i == 3) {
output.println("Login Failed");
client.close();
}
}
output.flush();
}
@Override
public void run() {
InputStream is = null;
try {
logInfo();
} catch (Exception e) {
}
try {
PrintWriter out = new PrintWriter(client.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
out.println("Server: " + inputLine);
out.println(inputLine);
if (inputLine.equals("Adiós.")) {
break;
}
}
out.close();
in.close();
client.close();
} catch (IOException e) {
System.err.println("Problemas de comunicacion con el servidor");
System.exit(1);
}
}
}
Clase conexión
private static List<UsersYClaves> log;
private int PORT = 9090;
private String HOSTNAME = "localhost";
public Conexion() {
log = new ArrayList();
cargarArray();
}
public int getPort() {
return this.PORT;
}
public String gethostName() {
return this.HOSTNAME;
}
public List<UsersYClaves> getLogin() {
return log;
}
public void setLogin(List<UsersYClaves> login) {
this.log = login;
}
public static void cargarArray() {
UsersYClaves a = new UsersYClaves("grego", "grego1234");
UsersYClaves b = new UsersYClaves("alba", "alba1234");
UsersYClaves c = new UsersYClaves("pedro", "pedro1234");
log.add(a);
log.add(b);
log.add(c);
}
}
Clase cliente
public class JC15 {
public static void main(String[] args) throws IOException {
String FILENAME = null;
Conexion c = new Conexion();
Socket socket;
BufferedReader in;
PrintWriter out;
String serverHostname = "127.0.0.1";
if (args.length > 0) {
serverHostname = args[0];
}
socket = new Socket(c.gethostName(), c.getPort());
//create printwriter for sending login to server
out = new PrintWriter(socket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String username = JOptionPane.showInputDialog(null, "Enter User Name:");
//send user name to server
out.println(username);
//prompt for password
String password = JOptionPane.showInputDialog(null, "Enter Password");
//send password to server
out.println(password);
out.flush();
BufferedReader stdIn = new BufferedReader(
new InputStreamReader(System.in));
String userInput;
out.println("Escribe (\"Adiós.\" para cerrar)");
out.print("Mensaje: ");
while ((userInput = stdIn.readLine()) != null) {
out.println(userInput);
if (userInput.equals("Adiós.")) {
break;
}
System.out.println(""+ in.readLine());
System.out.print("Mensaje: ");
}
out.close();
in.close();
stdIn.close();
socket.close();
}
}
El problema es en el cliente una vez este el login para que salga esto out.println("Escribe (\"Adiós.\" para cerrar)");
debo de dar un intro y para que el servidor me ponga login
correcto que sale antes de eso también debo de haberle dado y quiero eliminarlo.