import java.lang.ClassNotFoundException; import java.net.ServerSocket; import java.net.InetAddress; import java.net.Socket; import java.io.IOException; import java.net.UnknownHostException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; //javac SocketLocalClient2.java public class SocketLocalClient2 { public static void main(String[] args) throws ClassNotFoundException, InterruptedException{ try { InetAddress host = InetAddress.getLocalHost(); Socket tcpSocket = null; ObjectOutputStream objOutPutStream = null; ObjectInputStream objInPutStream = null; for(int i=0; i<5;i++){ //tcpSocket connection to server tcpSocket = new Socket(host.getHostName(), 9876); //exception ObjectOutputStream objOutPutStream = new ObjectOutputStream(tcpSocket.getOutputStream()); System.out.println("Sending request to Socket Server"); if(i==4)objOutPutStream.writeObject("terminate"); else objOutPutStream.writeObject(""+i); //exception : server response message objInPutStream = new ObjectInputStream(tcpSocket.getInputStream()); // exception ClassNotFoundException; String message = (String) objInPutStream.readObject(); System.out.println("Client: " + message); //close resources objInPutStream.close(); objOutPutStream.close(); //exception InterruptedException Thread.sleep(100); } } catch(IOException e) { } } }