/** * TCPIPClient.java * * Quelle: Java Buch (Schader) * * Version $Id$ */ import java.io.*; import java.net.*; public class TCPIPClient { TCPIPClient(String hostname, int port) { try { Socket sock= null; BufferedReader sockin= null; PrintWriter sockout= null; BufferedReader in= null; String zeile; sock = new Socket(hostname, port); System.out.println("Client gebunden an lokalen Port: " + sock.getLocalPort() ); sock.setSoTimeout(100); sockin = new BufferedReader( new InputStreamReader(sock.getInputStream() ) ); sockout= new PrintWriter(sock.getOutputStream(), true); antwort(sockin); in = new BufferedReader( new InputStreamReader(System.in) ); while( !(zeile = in.readLine() ).startsWith("ende") ) { sockout.println(zeile); antwort(sockin); } sock.close(); } catch (UnknownHostException ux) { System.err.println(hostname + "ist dem DNS nicht bekannt."); } catch (IOException ioe) { ioe.printStackTrace(); } } private static void antwort(BufferedReader sockin) throws IOException { String str; try { while ((str = sockin.readLine() ) != null) System.out.println(str); } catch (InterruptedIOException iioe) { } System.out.print("> "); System.out.flush(); } public static void main(String[] argv) { if (argv.length == 2) new TCPIPClient(argv[0], Integer.parseInt(argv[1]) ); else System.err.println("Syntax: java TCPIPClient "); } }