© 2014 Firstsoft Technologies (P) Limited. login
Hi 'Guest'
Home SiteMap Contact Us Disclaimer
enggedu
Quick Links
Easy Studies

Home Lab Exercise Network Lab Exercise Programs Java Client Server Program Using Byte Stream▼


Java Client Server Program Using Byte Stream:

/*** SimpleEchoClient.java ***/ import java.net.*; import java.io.*; public class SimpleEchoClient { public static void main(String[] args) { try { Socket s = new Socket("127.0.0.1", 9999); InputStream i = s.getInputStream(); OutputStream o = s.getOutputStream(); String str; do { byte[] line = new byte[100]; System.in.read(line); o.write(line); i.read(line); str = new String(line); System.out.println(str.trim()); } while ( !str.trim().equals("bye") ); s.close(); } catch (Exception err) { System.err.println(err); } } } /******* SimpleEchoServer.java ****/ /*client program*/ import java.io.*; import java.net.*; public class SimpleEchoServer { public static void main(String[] args) { try { ServerSocket s = new ServerSocket(9999); String str; while (true) { Socket c = s.accept(); InputStream i = c.getInputStream(); OutputStream o = c.getOutputStream(); do { byte[] line = new byte[100]; i.read(line); o.write(line); str = new String(line); } while ( !str.trim().equals("bye") ); c.close(); } } catch (Exception err) { System.err.println(err); } } }

SAMPLE INPUT OUTPUT:

Hai
Hai
How are you
How are you
bye
bye
Press any key to continue . . .

 
SLogix Student Projects
bottom