This is a homework assignment so i don't want any code, just maybe a little hint as to what the heck i'm doing wrong. The assignment is simply to write a EchoClient and EchoServer where the server echos back everything the client sends (txt) until the client socket is closed. My code follows (please don't laugh, i'm beginner).
and the client code is:import java.io.*; import java.net.*; import java.lang.*; public class EchoServer { public static void main (String[] args){ try{ ServerSocket sock; sock = new ServerSocket(8011); while(true) { Socket client=sock.accept(); InputStream in=client.getInputStream(); String line; BufferedReader bin=new BufferedReader(new InputStreamReader(in)); do{ line=bin.readLine(); PrintWriter pout=new PrintWriter(client.getOutputStream(),true); pout.println(line); } while(client.isClosed()!=true); sock.close(); client.close(); } } catch(IOException ioe) { System.err.println(ioe); } } }
The while loop in client is just so I could make sure it would echo the multiple messages (and not just one)import java.io.*; import java.net.*; import java.lang.*; public class EchoClient { public static void main (String[] args){ try{ Socket s=new Socket("127.0.0.1",8011); InputStream in=s.getInputStream(); BufferedReader buff=new BufferedReader(new InputStreamReader(in)); String message="Hello there! Please work!"; PrintWriter out=new PrintWriter(s.getOutputStream(),true); int i=0; while(i<4) { out.println(message); message=buff.readLine(); i++; } s.close(); } catch(IOException ioe) { System.err.println(ioe); } } }
I keep getting the following error: java.net.BindException: Address already in use
Thank you in advance. Any help is much appreciated.