ServerSocket per Socket
import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.net.ServerSocket; import java.net.Socket; public class LoopingSocketServer { public static void main(String args[]) throws Exception { ServerSocket servSocket; Socket fromClientSocket; int cTosPortNumber = 1777; String str; servSocket = new ServerSocket(cTosPortNumber); System.out.println("Waiting for a connection on " + cTosPortNumber); fromClientSocket = servSocket.accept(); System.out.println("fromClientSocket accepted"); ObjectOutputStream oos = new ObjectOutputStream(fromClientSocket.getOutputStream()); ObjectInputStream ois = new ObjectInputStream(fromClientSocket.getInputStream()); while ((str = (String) ois.readObject()) != null) { System.out.println("The message from client: " + str); if (str.equals("bye")) { oos.writeObject("bye bye"); break; } else { str = "Server returns " + str; oos.writeObject(str); } } oos.close(); fromClientSocket.close(); } }