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 5 of 5

Thread: Simple server client echo issues

  1. #1
    Junior Member Kakashi's Avatar
    Join Date
    Oct 2009
    Posts
    29
    My Mood
    Confused
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Simple server client echo issues

    What I want to do is have the server send back what the client send it, but it does not do anything, what did I do wrong with the code here?
    Here is the client code
    import java.io.*;
    import java.net.*;
    import java.util.Scanner;
     
    /**
     *
     * @author Kenshin
     */
    public class Client {
        public static void main(String[] args) throws IOException
        {
        final int port = 8189;
        String s = "";
     
     
        try{
            Socket client = new Socket(s, port);
            DataOutputStream socketOut = new DataOutputStream(client.getOutputStream());
            InputStream inStream = client.getInputStream();
            Scanner in = new Scanner(inStream);
             while (in.hasNextLine())
                {
                   String line = in.nextLine();
                   System.out.println(line);
                }
            client.close();
            }catch(UnknownHostException e)
          { System.err.println(": unknown host."); }
     
     
     
        }
     
    }
    and here is the server code:
    import java.io.*;
    import java.net.*;
    import java.util.*;
     
    /**
       This program implements a multithreaded server that listens to port 8189 and echoes back
       all client input.
       @author Cay Horstmann
       @version 1.20 2004-08-03
    */
    public class ThreadedEchoServer
    {
       public static void main(String[] args )
       {
          try
          {
             int i = 1;
             ServerSocket s = new ServerSocket(8189);
     
             while (true)
             {
                Socket incoming = s.accept();
                System.out.println("Spawning " + i);
                Runnable r = new ThreadedEchoHandler(incoming);
                Thread t = new Thread(r);
                t.start();
                i++;
             }
          }
          catch (IOException e)
          {
             e.printStackTrace();
          }
       }
    }
     
    /**
       This class handles the client input for one server socket connection.
    */
    class ThreadedEchoHandler implements Runnable
    {
       /**
          Constructs a handler.
          @param i the incoming socket
          @param c the counter for the handlers (used in prompts)
       */
       public ThreadedEchoHandler(Socket i)
       {
          incoming = i;
       }
     
       public void run()
       {
          try
          {
             try
             {
                InputStream inStream = incoming.getInputStream();
                OutputStream outStream = incoming.getOutputStream();
     
                Scanner in = new Scanner(inStream);
                PrintWriter out = new PrintWriter(outStream, true /* autoFlush */);
     
                out.println( "Hello! Enter BYE to exit." );
     
                // echo client input
                boolean done = false;
                while (!done && in.hasNextLine())
                {
                   String line = in.nextLine();
                   out.println("Echo: " + line);
                   if (line.trim().equals("BYE"))
                      done = true;
                }
             }
             finally
             {
                incoming.close();
             }
          }
          catch (IOException e)
          {
             e.printStackTrace();
          }
       }
     
       private Socket incoming;
    }
    Thanks for any help


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Simple server client echo issues

    It echo's fine for me.
    Make sure you start the Server before any clients.

    Side note: Is the Horstmann's material you're looking at fairly dated? Just curious as the
    latest book of Big Java I own has a slightly better example than that.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. The Following User Says Thank You to newbie For This Useful Post:

    Kakashi (March 3rd, 2011)

  4. #3
    Junior Member Kakashi's Avatar
    Join Date
    Oct 2009
    Posts
    29
    My Mood
    Confused
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Simple server client echo issues

    Im using Core Java Vl 2 8th ed I though it was the newest one.
    Thnak you for testing it for me, I must be running it wrong. What I do is set the main project in netbeans as the server then run it, then i set the main project as the client and then run that. Is there a better way?

  5. #4
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Simple server client echo issues

    You can right click both .java files, and choose run, as both files contain a main method.
    Alternatively you could just use command prompt, but that would be unnecessary.
    Your Core Java might very well be up-to-date, I was just comparing his Big Java example it it .
    Is your issue now solved? If so, please mark this thread as 'Solved' or specifiy a problem you might be having along with details.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  6. #5
    Junior Member Kakashi's Avatar
    Join Date
    Oct 2009
    Posts
    29
    My Mood
    Confused
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Simple server client echo issues

    I looked for how to switch it to solved but I can find that option nay where, where is it?

Similar Threads

  1. simple ftp server and ftp client
    By simontkk2005 in forum Java Networking
    Replies: 4
    Last Post: January 26th, 2011, 10:29 AM
  2. client/server socket
    By Java_dude_101 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: January 18th, 2011, 01:16 AM
  3. Client/Server
    By Dillz in forum Paid Java Projects
    Replies: 2
    Last Post: June 2nd, 2010, 05:19 AM
  4. Replies: 0
    Last Post: December 12th, 2009, 10:09 PM
  5. Simple server-client trouble
    By DC200 in forum Java Networking
    Replies: 3
    Last Post: November 12th, 2009, 08:16 AM