You see, I have my Connection class which has everything set up, and my client class and my Server class.
Server:
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class Servidor extends Conexion //Se hereda de conexión para hacer uso de los sockets y demás
{
public Servidor() throws IOException{super("servidor");} //Se usa el constructor para servidor de Conexion
public void startServer()//Método para iniciar el servidor
{
try
{
System.out.println("Esperando..."); //Esperando conexión
cs = ss.accept(); //Accept comienza el socket y espera una conexión desde un cliente
System.out.println("Cliente en línea");
//Se obtiene el flujo de salida del cliente para enviarle mensajes
salidaCliente = new DataOutputStream(cs.getOutputStream());
//Se le envía un mensaje al cliente usando su flujo de salida
salidaCliente.writeUTF("Petición recibida y aceptada");
//Se obtiene el flujo entrante desde el cliente
BufferedReader entrada = new BufferedReader(new InputStreamReader(cs.getInputStream()));
while((mensajeServidor = entrada.readLine()) != null) //Mientras haya mensajes desde el cliente
{
if( mensajeServidor.equals("salir")){
break;
}
//Se muestra por pantalla el mensaje recibido
System.out.println(mensajeServidor);
}
System.out.println("Fin de la conexión");
ss.close();//Se finaliza la conexión con el cliente
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
}
And my Client class:
import java.io.DataOutputStream;
import java.io.IOException;
public class Cliente extends Conexion
{
public Cliente() throws IOException{super("cliente");} //Se usa el constructor para cliente de Conexion
public void startClient() //Método para iniciar el cliente
{
try
{
//Flujo de datos hacia el servidor
salidaServidor = new DataOutputStream(cs.getOutputStream());
//Se enviarán dos mensajes
for (int i = 0; i < 2; i++)
{
//Se escribe en el servidor usando su flujo de datos
salidaServidor.writeUTF("Este es el mensaje número " + (i+1) + "\n");
}
cs.close();//Fin de la conexión
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
}
From two Test classes (TestServidor and TestCliente) I did the tests, I ran TestServidor first and the server started and waited for the client. Then I run TestCliente and it shows the messages it should show and it closes the connection and that's it. If I want to test it again I have to run TestServer and TestClient again. These tests are on localhost.
How do I make it so that the server is always listening and only run TestServer once and TestClient as many times as I want?
How can I enable it to receive more than one request? For example, that he is always upstairs listening to the server and I can reach him from another computer with another IP?
For the server to accept multiple connections, it is usual to follow a structure like this
Where ThreadToDealTheClient would be a class that inherits from Thread and does everything it has to do with the socket to deal with the client.