1 package edu.upv_ehu.gb.clientServerProtocol;
 2 
 3 import java.io.*;
 4 import java.net.*;
 5 
 6 public class Server {
 7 
 8     public static void main(String[] args) throws IOException {
 9         ServerSocket ss = new ServerSocket(6000);
10         do {
11             Socket s = ss.accept();
12             new ThreadedSocket(s);
13         } while (true);
14     }
15 }
16 
17 class ThreadedSocket extends Thread {
18     Socket s;
19 
20     ThreadedSocket(Socket s) {
21         this.s = s;
22         start();
23     }
24 
25     public void run() {
26         System.out.println("\n\n***Conexion establecida con: " + s);
27 
28         try {
29             ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
30             ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
31             oos.writeObject(new Protocol("servidor", "OK - conexión establecida"));
32             
33             Protocol query;
34             while ((query = (Protocol) ois.readObject()) != null) {
35                 System.out.println(query.mensaje + " //Mensaje recibido de " + query.nick);
36                 oos.writeObject(new Protocol("servidor", "OK - de acuerdo: "+query.mensaje));
37             }
38             System.out.println("***Cerrando conexion con: " + s + "\n\n");
39             s.close();
40         } catch (Exception ignore) {}
41     }
42 }
43