Example of echo server in Java

try { ServerSocket s = new ServerSocket(1234); // Create server while (true) { Socket c = s.accept(); // Await connection InputStream i = c.getInputStream(); // Get input stream OutputStream o = c.getOutputStream();//Get output stream do { byte[] line = new byte[100]; // Create scratchpad i.read(line); // Read client’s message o.write(line); // Send it back } […]
Continue reading…

Enjoyed this post? Share it!

 
 

Example of Echo Client in Java

Socket s = new Socket("192.168.1.1", 123); // Create socket InputStream i = s.getInputStream(); // Get input stream OutputStream o = s.getOutputStream(); // Get output stream String str; do { byte[] line = new byte[100]; System.in.read(line); // Read from console o.write(line); // Send the message i.read(line); // Read it back str = new String(line); // Convert […]
Continue reading…

Enjoyed this post? Share it!