Socket que atienda a varios clientes
Publicado por Ainhoa (2 intervenciones) el 05/03/2007 12:06:14
Buenas,
Quisiera saber como implementar un socket servidor que pueda escuchar peticiones de diferentes sockets cliente. Yo he intentando haciendo un bucle while(true), pero sólo me funciona la primera conexión el resto de conexiones no funcionan. Es decir, el primer socket cliente lo pilla bien y funciona, pero si realizamos más llamadas una vez acabado el socket anterior ya no funciona. Os mando el código y acepto sugerencias.
public static ServerSocket CrearSocketServidor()
{
ServerSocket socket = null;
String path = Conexiones.getPathXML ();
try
{
// Se crea un socket servidor atendiendo a un determinado puerto.
// Por ejemplo, el 35557.
socket = new ServerSocket (35557);
} catch (Exception e){
e.printStackTrace();
}
System.out.println ("Esperando cliente");
while (true) {
try {
System.out.println("Entro para aceptar clientes");
Socket cliente = socket.accept();
System.out.println ("Conectado con cliente de " + cliente.getInetAddress());
cliente.setSoLinger (true, 10);
ObjectInputStream entrada = new ObjectInputStream(cliente.getInputStream());
File fichero = (File) entrada.readObject ();
String nombre = fichero.getName ();
System.out.println ("nombre obtenido "+nombre);
System.out.println ("renombramos el fichero en "+path+nombre);
fichero.renameTo (new File(path+nombre));
System.out.println("Acabado todo con EXITO");
cliente.close();
cliente = null;
} catch (IOException ioe) {
System.out.println ("IOException: "+ioe);
} catch (ClassNotFoundException cnfe) {
System.out.println ("ClassNotFoundException: "+cnfe);
}
//socket.close();
return socket;
}
}
Y aquí el socket cliente:
public ClienteSocket(String nom){
try
{
this.s = new Socket ("localhost", 35557);
this.nombre = nom;
} catch (UnknownHostException ex){
ex.printStackTrace();
} catch (IOException ex){
ex.printStackTrace();
}
}
/**
* Crea el socket cliente y lee los datos
*/
public void run()
{
try
{
String path = Conexiones.getPathXML ();
OutputStream os = s.getOutputStream();
ObjectOutputStream salida = new ObjectOutputStream(os);
System.out.println("nombre "+this.nombre);
System.out.println("path "+path);
String fich = path+nombre;
System.out.println ("Escribimos el fichero "+fich);
File fichero = new File(fich);
salida.writeObject (fichero);
System.out.println ("CORRECTO");
salida.close ();
s.close ();
}
catch (Exception e)
{
System.out.println("Excepcion "+e);
e.printStackTrace();
}
}
Muchas gracias.
Quisiera saber como implementar un socket servidor que pueda escuchar peticiones de diferentes sockets cliente. Yo he intentando haciendo un bucle while(true), pero sólo me funciona la primera conexión el resto de conexiones no funcionan. Es decir, el primer socket cliente lo pilla bien y funciona, pero si realizamos más llamadas una vez acabado el socket anterior ya no funciona. Os mando el código y acepto sugerencias.
public static ServerSocket CrearSocketServidor()
{
ServerSocket socket = null;
String path = Conexiones.getPathXML ();
try
{
// Se crea un socket servidor atendiendo a un determinado puerto.
// Por ejemplo, el 35557.
socket = new ServerSocket (35557);
} catch (Exception e){
e.printStackTrace();
}
System.out.println ("Esperando cliente");
while (true) {
try {
System.out.println("Entro para aceptar clientes");
Socket cliente = socket.accept();
System.out.println ("Conectado con cliente de " + cliente.getInetAddress());
cliente.setSoLinger (true, 10);
ObjectInputStream entrada = new ObjectInputStream(cliente.getInputStream());
File fichero = (File) entrada.readObject ();
String nombre = fichero.getName ();
System.out.println ("nombre obtenido "+nombre);
System.out.println ("renombramos el fichero en "+path+nombre);
fichero.renameTo (new File(path+nombre));
System.out.println("Acabado todo con EXITO");
cliente.close();
cliente = null;
} catch (IOException ioe) {
System.out.println ("IOException: "+ioe);
} catch (ClassNotFoundException cnfe) {
System.out.println ("ClassNotFoundException: "+cnfe);
}
//socket.close();
return socket;
}
}
Y aquí el socket cliente:
public ClienteSocket(String nom){
try
{
this.s = new Socket ("localhost", 35557);
this.nombre = nom;
} catch (UnknownHostException ex){
ex.printStackTrace();
} catch (IOException ex){
ex.printStackTrace();
}
}
/**
* Crea el socket cliente y lee los datos
*/
public void run()
{
try
{
String path = Conexiones.getPathXML ();
OutputStream os = s.getOutputStream();
ObjectOutputStream salida = new ObjectOutputStream(os);
System.out.println("nombre "+this.nombre);
System.out.println("path "+path);
String fich = path+nombre;
System.out.println ("Escribimos el fichero "+fich);
File fichero = new File(fich);
salida.writeObject (fichero);
System.out.println ("CORRECTO");
salida.close ();
s.close ();
}
catch (Exception e)
{
System.out.println("Excepcion "+e);
e.printStackTrace();
}
}
Muchas gracias.
Valora esta pregunta


0