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

Thread: Simple server-client trouble

  1. #1
    Junior Member
    Join Date
    Nov 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Simple server-client trouble

    Hi, I'm new to these forums. I've been trying for the past two hours to figure out why this piece of code isn't working. The client is supposed to receive a message ("two") from the server, but the client screen stays completely blank.

    This is the first time I'm working with sockets, so any help on getting this thing working would be deeply appreciated. Then I can finally get to messing around with the code in order to understand how it actually works.

    Client
    public class SimpleClient
          {
             public static void main (String [] args) throws IOException
                   {
                      final int APP_PORT = 3816;
                      Socket s = new Socket ("localhost", APP_PORT);
                      InputStream instream = s.getInputStream ();
                      OutputStream outstream = s.getOutputStream ();
                      Scanner in = new Scanner (instream);
                      PrintWriter out = new PrintWriter (outstream);
     
    				  out.print ("one");
     
                      String response = in.nextLine ();
                      System.out.println (response);
     
                      s.close ();
                   }
          }

    Server
    public class SimpleServer
          {
    		   public static void main (String [] args) throws IOException
    		         {
    					 final int APP_PORT = 3816;
    					 ServerSocket ss = new ServerSocket (APP_PORT);
     
    					 System.out.println ("Waiting for clients to connect");
    					 while (true)
    					      {
    							  Socket s = ss.accept ();
    							  System.out.println ("Client connected...");
    					          Service service = new Service (s);
    					          Thread t = new Thread (service);
    					          t.start ();
    					      }
    				 }
    	  }

    Service
    public class Service implements Runnable {
    	Socket s;
    	Scanner in;
    	PrintWriter out;
    	InputStream instream;
    	OutputStream outstream;
     
    	public Service (Socket socket) {s = socket;}
     
    	public void run (){
    		try {
    			instream = s.getInputStream ();
    			outstream = s.getOutputStream ();
    			in = new Scanner (instream);
    			out = new PrintWriter (outstream);
    			System.out.print("Service running");
    			performService ();
    		}
    		catch (IOException ioe) {
    			ioe.getMessage ();
    		}
    	}
     
    	public void performService () throws IOException {
    		while (true) {
    			String command = in.next ();
    			if (in.next().equalsIgnoreCase ("one")) {
    				out.print ("server: two");
    			}
    			else {
    				out.print ("server: three");
    			}
    		}
    	}
    }


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Simple server-client trouble

    You might need to flush the outputstream as well, or create the PrintWriter with autoflush set to true.

    out = new PrintWriter(outstream, true);

    And then instead of using print you want to use println

    For more information about the PrintWriter have a look at PrintWriter (Java Platform SE 6)

    // Json

  3. #3
    Junior Member
    Join Date
    Nov 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Simple server-client trouble

    Flushing the stream worked. Also I didn't realize that readLine was waiting for a line terminator and holding the program up. The print method didn't send out the terminator, while println did.

    Thanks.

  4. #4
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Simple server-client trouble

    No worries, marking this as solved.

    // Json

Similar Threads

  1. How to Write a simple XMPP (Jabber) client using the Smack API
    By JavaPF in forum Java Networking Tutorials
    Replies: 35
    Last Post: August 5th, 2014, 12:59 AM
  2. Having trouble with strings
    By Reaperkid77 in forum Java SE APIs
    Replies: 3
    Last Post: October 20th, 2009, 06:30 PM
  3. Download a file to client from server
    By mvittalreddy in forum Java Networking
    Replies: 4
    Last Post: October 5th, 2009, 04:23 AM
  4. instead of printing in the client, report get printed in the server
    By jmvenkat in forum Java Theory & Questions
    Replies: 0
    Last Post: September 19th, 2009, 01:30 AM
  5. Replies: 1
    Last Post: April 20th, 2009, 11:17 AM