19
Feb
Echo Echo Echo…
Posted by BernadetThis blog item has no other purpose than terrifying Lauren, as she will be switching to doing computer stuff in uni soon…just like me. She is now officially doomed. Here is my latest homework assignment, a multithreaded binary echo server using java (it doesn`t look very fancy because WP doesn`t indent):
MultiEchoServer.java
import java.net.*;import java.io.*;public class MultiEchoServer{ServerSocket socket;public MultiEchoServer(){//Try to open a sockettry{socket = new ServerSocket(1337);}//Close the program if it failscatch(IOException ioe){System.out.println(“Server error, program terminating…”);System.err.println(“Exception: ” + ioe);System.exit(1);}System.out.println(“Server started and listening…”);while (true){//Make a connection and start a threadtry{Socket client = socket.accept();Thread t1 = new Thread(new ClientHandler(client));t1.start();}//error handlingcatch (IOException ioe){System.err.println(“Exception: ” + ioe);}}}public static void main (String[] args){//make the servernew MultiEchoServer();}}
ClientHandler.java
import java.io.*;import java.net.*;//The class echothread is the actual thread, and is pretty much equal to the server class of the non multithreaded project.class ClientHandler implements Runnable{Socket sock;//initializing the socketpublic ClientHandler(Socket s){sock = s;}public void run(){try{//make the input and output streamInputStream input = sock.getInputStream();OutputStream output = sock.getOutputStream();while (true){//execute code while i does not equal -1do{byte b;while((b = (byte) input.read()) != -1){output.write(b);}System.out.println(“system will now be terminated..”);output.close();input.close();sock.close();}while (true);}}catch (IOException ioe){System.err.println(“Exception: ” + ioe);}}}
MultiEchoClient.java
import java.net.*;import java.io.*;public class MultiEchoClient{public static void main(String[] args){try{//make connection to server socketSocket socket = new Socket(“127.0.0.1″, 1337);OutputStream output = socket.getOutputStream();InputStream input = socket.getInputStream();//introduction messageSystem.out.println(“Hallo!”);//execute code while i does not equal -1do{byte b;while ((b = (byte) System.in.read()) != -1){output.write(b);b = (byte) input.read();String str = “”;str += (char) b;System.out.print(str);}System.out.println(“system will now be terminated..”);output.close();input.close();socket.close();}while (true);}//Error Handlingcatch (IOException ioe){System.err.println(“Exception: ” + ioe);}}}
Want to know what this does? Nothing but echo back to you what you type in the console. That`s right…you need all this code to make a computer say hi back to you after you said hi to him. BE AFRAID LAUREN, BE VERY AFRAID!!!!!
-Bernadet
Bernadet/19/The Netherlands - I was born on the 25th of september in a small town in the Netherlands. These days I study computer science and share this site with my sister...we create mayhem online together.
February 23rd, 2010 at 10:50 pm
Actually, you don’t need all that code
This will do the same (although it will be less verbose):
EchoServer.java
import java.io.*;
import java.net.*;
public class EchoServer {
private class ClientHandler impements Runnable {
private Socket socket;
public ClientHandler (Socket socket) {
this.socket = socket;
}
public void run () {
try {
int i;
while ((i = socket.getInputStream().read()) != -1) {
socket.getOutputStream().write(i);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public EchoServer () {
try {
ServerSocket socket = new ServerSocket(1337);
while (true) {
new Thread(new ClientHandler(socket.accept())).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main (String[] args) {
new EchoServer();
}
}
EchoClient.java
import java.io.*;
import java.net.*;
public class EchoClient {
public EchoClient (String host, int port) {
try {
Socket socket = new Socket(host, port);
int i;
while ((i = System.in.read()) != -1) {
socket.getOutputStream().write(i);
System.out.write(i);
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main (String[] args) {
new EchoClient("localhost", 1337);
}
}
February 27th, 2010 at 8:14 pm
True true, but that wouldn`t scare Lauren *grins*