Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Homework help with echoserver and echoclient

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Homework help with echoserver and echoclient

    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).

    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);
            }
        }
    }
    and the client code is:
    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);
            }
     
       }
    }
    The while loop in client is just so I could make sure it would echo the multiple messages (and not just one)

    I keep getting the following error: java.net.BindException: Address already in use

    Thank you in advance. Any help is much appreciated.


  2. #2
    Member
    Join Date
    Mar 2012
    Location
    United States
    Posts
    118
    My Mood
    Inspired
    Thanks
    1
    Thanked 33 Times in 31 Posts

    Default Re: Homework help with echoserver and echoclient

    Is that the entire error message?

  3. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Homework help with echoserver and echoclient

    this is all it says in the output window (using NetBeans IDE 7.4 on Ubuntu):
    java.net.BindException: Address already in use
    BUILD SUCCESSFUL (total time: 0 seconds)

  4. #4
    Member
    Join Date
    Mar 2012
    Location
    United States
    Posts
    118
    My Mood
    Inspired
    Thanks
    1
    Thanked 33 Times in 31 Posts

    Default Re: Homework help with echoserver and echoclient

    Ok, sorry for the delay in response. There are a few things to look at.
    1) You may need to manually close port 8011.

    Server Code:
    1) You only need to create the variable pout once.
    2) Checking client.isClosed() will not give you what you expect it to. Instead, check to see if what is recieved from the client has a value.
    3) One of the while loops is not needed.

    Client Code:
    1) Nothing technically wrong here, fix the server code and I think you will see what I mean.

  5. The Following User Says Thank You to KucerakJM For This Useful Post:

    GregBrannon (March 2nd, 2014)

  6. #5
    Junior Member
    Join Date
    Mar 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Homework help with echoserver and echoclient

    Thank you very, very, very much. Your help is appreciated KucerakjM

  7. #6
    Junior Member
    Join Date
    Mar 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Homework help with echoserver and echoclient

    I revised my code and am getting very frustrated because now I getting different error and can't seem to fix it. My revised code is below, any help is greatly appreciated.
    Server Code:
    public class EchoServer {
        public static void main (String[] args)throws IOException{
            try{
                ServerSocket server= new ServerSocket(3777);
                server.getLocalSocketAddress();
                Socket client=server.accept();
                InputStream in=client.getInputStream();
                PrintWriter out=new PrintWriter(client.getOutputStream(),true);
                BufferedReader bin=new BufferedReader(new InputStreamReader(in));
                String line;
                do{
                    line=bin.readLine();
                    out.println(line.toString());
                } while(line.isEmpty()==false);
                server.close();
                client.close();
            }
            catch(IOException ioe) {
                System.err.println(ioe);
            }
        }
    }

    and my client code:
    public class EchoClient {
        public static void main (String[] args){
            try{
                Socket sock=new Socket("127.0.0.1",37777);
                PrintWriter out=new PrintWriter(sock.getOutputStream(),true);
                String message="Hello there!";
                out.println(message.toString());
                sock.close();
            }
            catch(IOException ioe) {
                System.err.println(ioe);
            }
     
       }
    }

    but now i keep getting this error message:
    java.net.BindException: Address already in use
    BUILD SUCCESSFUL (total time: 0 seconds)
    and when i debug the debugger window shows this message:
    Listening on 57950
    User program running
    User program finished

    Thinking maybe it's something to do with the linux, I get even more frustrated because when i go on to my netbeans on windows machine and type in the EXACT SAME code on line 16 where my catch begins, it says <identifier> expected so it wont' run however my teacher will be running it on linux so thats what i would like it working on. PLEASE HELP

  8. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Homework help with echoserver and echoclient

    The error messages would be more complete if the code called the printStackTrace() method in the catch blocks.
    but now i keep getting this error message:
    java.net.BindException: Address already in use
    Where is that error message coming from?
    That would happen if the program was still running and had a socket open at that address. Make sure the program has exited.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #8
    Member
    Join Date
    Mar 2012
    Location
    United States
    Posts
    118
    My Mood
    Inspired
    Thanks
    1
    Thanked 33 Times in 31 Posts

    Default Re: Homework help with echoserver and echoclient

    Take a look at your port numbers in the two classes.
    Also, you may need to manually kill the process, and definitely take Norm's advice on printStackTrace() it is quite nice.
    One last bit, using the toString() on a String, as far as I know, isn't needed...ever but perhaps some obscure case.
    Last edited by KucerakJM; March 4th, 2014 at 09:24 PM.

  10. #9
    Junior Member
    Join Date
    Mar 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Homework help with echoserver and echoclient

    Thanks so much for all the help. Its finally working rite (at least its echoing back the clients first message), i'll post the server code below. My question is: at the end of my do-while loop where I say do ...until (line=inWhatever)!=null,does the readLine() return a null when it reads the first string? If so, how the heck can I keep the loop reading in the messages until the client stops sending? I tried testing using line.isEmpty (which didn't work). Am I going to have to make a exit String to which a match is the condition for breaking the loop?

    public class EchoServer {
        public static void main (String[] args)throws IOException{
            try{
                ServerSocket server= new ServerSocket(65000);
                server.getLocalSocketAddress();
                Socket client=server.accept();
                InputStream in=client.getInputStream();
                PrintWriter out=new PrintWriter(client.getOutputStream(),true);
                BufferedReader bin=new BufferedReader(new InputStreamReader(in));
                String line;
                do{
                    line=bin.readLine();
                    out.println(line);
                } while((line=bin.readLine())!=null);
                server.close();
                client.close();
            }
            catch(IOException ioe) {
                System.err.println(ioe);
            }
        }
    }

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Homework help with echoserver and echoclient

    Read the API doc for the readLine() method to see when it returns a null.
    The code has two calls to the readLine() method. Each one will read a line. Only the line read by the first readLine() is used.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #11
    Member
    Join Date
    Mar 2012
    Location
    United States
    Posts
    118
    My Mood
    Inspired
    Thanks
    1
    Thanked 33 Times in 31 Posts

    Default Re: Homework help with echoserver and echoclient

    As far as I know, yes you will need to make an exit string if you want to indefinitely recieve data. So, unless you have a timeout on the connection the client has to "tell" the server that it is done sending or perhaps disconnecting.

Similar Threads

  1. Homework help
    By todoratanasovv in forum Java Theory & Questions
    Replies: 1
    Last Post: December 5th, 2013, 07:03 AM
  2. homework help
    By andrag3k in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 15th, 2013, 09:39 PM
  3. HELP WITH HOMEWORK!!
    By jmillernc01 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 10th, 2012, 11:13 AM
  4. Homework help
    By hockey15 in forum Object Oriented Programming
    Replies: 3
    Last Post: September 23rd, 2012, 11:28 PM
  5. Homework
    By jdonaldson in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 9th, 2011, 11:09 AM

Tags for this Thread