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

Thread: Help! Client only reads first line server sends!

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    6
    My Mood
    Sad
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help! Client only reads first line server sends!

    Hello guys! Basically I need to know how to make the client receive multiple lines from the server. In my code it only reads the first line.

    Server:
    public class Server
    {
     
    	public static void main(String[] args) throws Exception
    	{
     
    		Server SERVER = new Server();
    		SERVER.run();
     
    	}
     
    	public void run() throws Exception
    	{
     
    		ServerSocket SRVSOCK = new ServerSocket(4444);
    		Socket SOCK = SRVSOCK.accept();
    		InputStreamReader IR = new InputStreamReader(SOCK.getInputStream());
    		BufferedReader BR = new BufferedReader(IR);
     
    		PrintStream PS = new PrintStream(SOCK.getOutputStream());
    		PS.println("Message 1" + "\n" + "Message2");//*bounces the message back to the client!
     
     
    	}
     
    }

    Client:

    public class Client
    {
     
    	public static void main(String[] args) throws Exception
    	{
     
    		Client CLIENT = new Client();
    		CLIENT.run();
     
    	}
     
    	public void run() throws Exception
    	{
     
    		Socket SOCK = new Socket("localhost",4444);
    		PrintStream PS = new PrintStream(SOCK.getOutputStream()); //* from client to server
     
    		InputStreamReader IR = new InputStreamReader(SOCK.getInputStream());//*from server to client
    		BufferedReader BR = new BufferedReader(IR);
     
    		String MESSAGE = BR.readLine();
    		System.out.println(MESSAGE);//*display what is received from the server
     
     
    	}
     
    }

    I am new to java so any help is appreciated! Thanks guys!


  2. #2
    Junior Member
    Join Date
    Oct 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help! Client only reads first line server sends!

    I am also new to java my friend but when i see readLine i understand that it's only going to read a line

  3. #3
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Help! Client only reads first line server sends!

    maxitis is correct. Please check out examples of this type of coding and you'll see that most use while loops to capture all streamed text from the server.

Similar Threads

  1. Replies: 0
    Last Post: May 31st, 2012, 05:35 PM
  2. [SOLVED] Server/client
    By lorider93p in forum Java Theory & Questions
    Replies: 1
    Last Post: February 8th, 2012, 01:36 PM
  3. server/client application fails when client closes
    By billykid in forum Java Networking
    Replies: 4
    Last Post: January 26th, 2012, 01:54 AM
  4. server and client
    By lanpan in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 24th, 2011, 09:29 AM
  5. Client/Server
    By Dillz in forum Paid Java Projects
    Replies: 2
    Last Post: June 2nd, 2010, 05:19 AM