You are on page 1of 2

package javaapplication50; import java.io.*; import java.net.

*; public class Main { public static void main(String[] args) { try { new Main().startClient(); } catch (Exception e) { System.out.println("Something falied: " + e.getMessage() ); e.printStackTrace(); } } public void startClient() throws IOException { Socket socket = null; PrintWriter out = null; BufferedReader in = null; InetAddress host = null; BufferedReader stdIn = null; try { //host = InetAddress.getLocalHost(); socket = new Socket("192.168.0.1",1234); out = new PrintWriter(socket.getOutputStream(), true); in = new BufferedReader(new InputStreamReader(socket.get InputStream())); stdIn = new BufferedReader(new InputStreamReader(System. in)); String fromServer; String fromUser; //Read from socket and write back the response to server . while ((fromServer = in.readLine()) != null) { System.out.println("Server - " + fromServer); if (fromServer.equals("exit")) break; fromUser = stdIn.readLine(); if (fromUser != null) { System.out.println("Client - " + fromUse r); out.println(fromUser); } } } catch (UnknownHostException e) { System.err.println("Cannot find the host: " + host.getHo stName()); System.exit(1); } catch (IOException e) { System.err.println("Couldn't read/write from the connect ion: " + e.getMessage()); System.exit(1); } finally { //Make sure we always clean up out.close(); in.close();

stdIn.close(); socket.close(); } } }

You might also like