Results 1 to 6 of 6
  1. #1
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default Simple server/client text problem

    Hi all. I'm trying to make a program (or 2 really) that has a client connect to a server, the server prints a line of text on the client's screen, and the program exits. However, the text from the two programs does not seem to transfer.

    My Server class:

    Java Code:
    package webapps;
    
    import java.io.*;
    import java.net.*;
    
    public class Server {
    
    	public Server(int port) throws IOException{
    		ServerSocket ss = null;
    		PrintWriter pw = null;
    		BufferedReader br = null;
    		Socket s = null;
    		ss = new ServerSocket(port);
    		try {
    			s = ss.accept();
    			pw = new PrintWriter(s.getOutputStream());
    			br = new BufferedReader(new InputStreamReader(s.getInputStream()));
    		} catch (IOException e) {
    			System.err.println("I/O setup error");
    		}
    		System.out.println("Connection made");
    		pw.println("Connection Complete.  Press any key to exit.");
    		System.out.println("Sent line");
    		try{
    			br.readLine();
    		} catch(IOException io){
    			pw.println("A strange error occured while processing entry.  Proceeding with exit.");
    		}
    		ss.close();
    		s.close();
    		pw.close();
    		br.close();
    		System.exit(0);
    	}
    	
    	public static void main(String[] args){
    		int port;
    		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    		System.out.println("Enter port to listen on");
    		while(true){
    			try {
    				String s = br.readLine();
    				port = Integer.parseInt(s);
    				break;
    			} catch (IOException e) {
    				System.err.println("Error with I/O.  Try again.");
    			} catch (NumberFormatException nfe){
    				System.err.println("Not a number.  Try again.");
    			}
    		}
    		try {
    			new Server(port);
    		} catch (IOException e) {
    			System.err.println("Server I/O exception.");
    			e.printStackTrace();
    		}
    	}
    }
    and my Client class:
    Java Code:
    package webapps;
    
    import java.net.*;
    import java.io.*;
    
    public class Client {
    
    	public Client(String hostName, int port){
    		Socket s = null;
    		PrintWriter pw = null;
    		BufferedReader systemBR = null, serverBR = null;
    		while(true){
    			try {
    				systemBR = new BufferedReader(new InputStreamReader(System.in));
    				s = new Socket(hostName,port);
    				break;
    			} catch (UnknownHostException e) {
    				System.err.println("Host " + hostName + " not found.");
    			} catch (IOException e) {
    				System.err.println("Error getting input/output for server or local system");
    			}
    			if(systemBR == null){
    				System.err.println("Exiting...");
    				System.exit(1);
    			}
    			else if(s == null){
    				System.out.println("Server could not be reached.  Try again? [y/n]");
    				String input = null;
    				try {
    					input = systemBR.readLine();
    				} catch (IOException e) {
    					System.out.println("I/O error... Exiting");
    					System.exit(1);
    				}
    				if("n".equalsIgnoreCase(input) || "no".equalsIgnoreCase(input)){
    					System.exit(0);
    				}
    			}
    		}
    		try {
    			pw = new PrintWriter(s.getOutputStream());
    			serverBR = new BufferedReader(new InputStreamReader(s.getInputStream()));
    			System.out.println("IO check");
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    		try {
    			System.out.println("Reading...");//prints
    			System.out.println(serverBR.readLine());//STICKS HERE
    			System.out.println("Line was read...");//does not print
    		} catch (IOException e1) {
    			System.err.println("Error occurred while processing server message.  Exiting...");
    			System.exit(1);
    		}
    		try {
    			pw.write(systemBR.readLine());
    		} catch (IOException e) {
    			System.err.println("Error occured... Continuing");
    		}
    		try{
    			s.close();
    			pw.close();
    			systemBR.close();
    			serverBR.close();
    		}catch(IOException io){
    			System.out.println("Error occurred while close IO streams");
    			System.exit(1);
    		}
    	}
    	//starts the client after getting hostname and port
    	public static void main(String[] args){
    		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    		System.out.println("Enter hostname...");
    		String hname = null;
    		try {
    			hname = br.readLine();
    		} catch (IOException e) {
    			System.err.println("Input/output error.  Try again.");
    		}
    		int port;
    		while(true){
    			System.out.println("Enter port...");
    			try{
    				String p = br.readLine();
    				port = Integer.parseInt(p);
    				break;
    			}catch(IOException io){
    				System.err.println("Input/output error.  Try again.");
    			}catch(NumberFormatException nfe){
    				System.err.println("Not a number.  Try again");
    			}
    		}
    		
    		new Client(hname, port);
    	}
    }
    Any suggestions? I believe the problem is in my transfer of data. All my tests for connection worked, so I'm not sure what's wrong...
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  2. #2
    Join Date
    Mar 2009
    Posts
    25
    Rep Power
    0

    Default

    The problem I guess is (and after testing it for myself) that the buffer isn't full enough for the socket to actually send the data across it. You can get around this by initiating a pw.flush() directly after the println statement.

  3. #3
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    806
    Rep Power
    6

    Default

    Hi,
    For PrintWriter you send "true" for autoflush for both Server and client programs.I think this will make some difference.

    pw = new PrintWriter(s.getOutputStream(),true);

    -Regards
    Ramya
    Ramya:cool:

  4. #4
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    When using sockets:

    Always flush() your streams when you want to send.
    The only thing you should ever be closing is the actual Socket instance (preferably in a finally block). Closing every stream separately will just get you confused.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  5. #5
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Hmm thanks all. Sounds right. Can't test it now since I'm not using the computer the program is on...

    Always flush() your streams when you want to send.
    The only thing you should ever be closing is the actual Socket instance (preferably in a finally block). Closing every stream separately will just get you confused.
    I was following the section in the Java Tutorials on servers, and the code there closed them all separately. I thought it was slightly odd, but didn't think too much of it.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  6. #6
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Hmm, that is odd. Do those tutorials accept corrections...?
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

Similar Threads

  1. Client Server socket problem - help needed
    By kellaw in forum Threads and Synchronization
    Replies: 6
    Last Post: 10-03-2008, 06:49 PM
  2. passing info between server/client problem
    By DarkBlaze in forum New To Java
    Replies: 13
    Last Post: 07-24-2008, 03:14 AM
  3. Replies: 3
    Last Post: 01-07-2008, 07:01 AM
  4. client-server communication problem
    By revathi17 in forum New To Java
    Replies: 1
    Last Post: 08-09-2007, 02:21 PM
  5. Simple example Client Server Application
    By ferosh in forum Networking
    Replies: 1
    Last Post: 04-01-2007, 10:36 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •