import java.net.*; import java.io.*; import java.util.*; class AgentSenderThread extends Thread { private Socket socket; private ObjectInputStream in; private ObjectOutput out; private String id; public AgentSenderThread(int num) { id = "Agent Sender " + num; try { socket = new Socket("127.0.0.1", 2001); System.out.println("Si iscrive agente " + id + "\npresso " + socket); System.out.flush(); } catch(IOException e) { System.err.println("Socket failed"); } try { // IMPORTANTE LE DUE SEGUENTI ISTRUZIONI DEVONO ESSERE FATTE // IN QUESTO PRECISO ORDINE !! out = new ObjectOutputStream(socket.getOutputStream()); in = new ObjectInputStream(socket.getInputStream()); start(); } catch(IOException e) { try { socket.close(); } catch(IOException e2) { System.err.println("Socket not closed"); } } } public void run() { try { Agent agent = new AgentPrinter ("Sono un agente che stampa"); out.writeObject(agent); //Agent agent = new AgentDoublePrinter ("Sono un agente che stampa","In modo piu` raffinato!"); //out.writeObject(agent); } catch(IOException e) { System.err.println("IO Exception" + e); } catch (Exception err) { System.err.println("Error: " + err); } finally { try { socket.close(); } catch(IOException e) { System.err.println("Socket not closed"); } } } } public class MultiAgentSender { static final int MAX_CLIENTS = 1; public static void main(String[] args) throws IOException, InterruptedException { for (int i=1; i<=MAX_CLIENTS; i++) new AgentSenderThread(i); } }