Results 1 to 3 of 3
  1. #1
    Tankske is offline Member
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Default Server can read, but client doesn't

    Hello,

    I'm building a server-client application. Since it is my first time doing that, I first tried to set up a simple connection on my computer. The purpose is to extend it to a real server and my computer.

    I used request because I''l expand the program...

    Server part

    Java Code:
    import java.io.*;
    import java.net.*;
    
    public class Server {
    
    	
    	   public static void main(String [] args) throws IOException {
    		   int port = 1234;
    		   final ServerSocket serverSocket = new ServerSocket(port);
    		   InetAddress address = InetAddress.getLocalHost();
    		   System.out.println("Server created: " + address);
    		   
    		   while(true)
    	      {
    			 System.out.println("Server waiting for connection...");  
    	         try
    	         {
    	           final Socket connection = serverSocket.accept();
    	           Runnable task = new Runnable(){
    	        	   public void run(){
    	        		   handleWriteRequest(connection);	        		   
    	        		   handleReadRequest(connection);
    
    	        		   System.out.println("handled requests with " + serverSocket.getLocalSocketAddress());
    						//TODO: controleren want op eigen computer in netwerk niet goed. 
    	        	   }
    	        	   
    
    				/**
    				 * Methode om iets te schrijven
    				 * @param connection
    				 */
    				private void handleWriteRequest(Socket connection) {
    					try{
    						System.out.println("Server probeert te schrijven");
    						PrintWriter out = new PrintWriter(connection.getOutputStream(), true);
    						out.write("Test schrijven server");
    						System.out.println("Server heeft geschreven");
    					} catch (Exception e){
    						System.out.println("Probleem met schrijven aan serverzijde");
    					}
    					
    				}
    
    				/**
    				 * Methode om iets te lezen
    				 * @param connection
    				 */
    				private void handleReadRequest(Socket connection) {
    					try {
    						System.out.println("Server probeert te lezen");
    						BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    						String input = in.readLine();
    						System.out.println(input);
    						System.out.println("Server heeft geschreven");
    					} catch (IOException e) {
    						System.out.println("Probleem met lezen aan serverzijde");
    						e.printStackTrace();
    					}
    					
    					
    				} 
    	           };
    	          new Thread(task).start();
    	         }catch(SocketTimeoutException s)
    	         {
    	            System.out.println("Socket timed out!");
    	            break;
    	         }catch(IOException e)
    	         {
    	            e.printStackTrace();
    	            break;
    	         }
    	     
    	   }
    	   }
    	
    }
    Client part

    Java Code:
    package connection;
    
    import java.io.*;
    import java.net.InetAddress;
    import java.net.Socket;
    import java.net.UnknownHostException;
    
    public class Client {
    	
    	 public static void main(String [] args)
    	   {
    	     
    	     try {
    //	    	Server.main(null);
    //			server.PrepareServer();
    //			System.out.println("Server prepared");
    		} catch (Exception e1) {
    			System.out.println("Cannot prepare server");
    		}
    		 
    		 String serverName = "localhost";
    		 InetAddress address = null;
    		try {
    			address = InetAddress.getByName(serverName);
    		} catch (UnknownHostException e1) {
    			System.out.println("Problemen met de host");
    			e1.printStackTrace();
    		}
    	      int port = 1234;
    	      try
    	      {
    	         System.out.println("Connecting to " + serverName
    	                             + " on port " + port);
    	         Socket client = new Socket(address, port);
    	         System.out.println("Just connected to "
    	                      + client.getRemoteSocketAddress() + "with " + client.getLocalAddress());
    	         
    	         handleWriteRequest(client);
    	         
    	         handleReadRequest(client);
    	         	         
    	         System.out.println("Mission completed");
    	         client.close();
    	         System.out.println("Disconnecting from server");
    	      }catch(IOException e)
    	      {
    	         System.out.println("Failed to connect with...");
    	    	  e.printStackTrace();
    	      }
    	   }
    
    	 private static void handleReadRequest(Socket client){
        	 try{
        		 System.out.println("Client probeert te lezen...");
    	    	 BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
    	    	 String input = in.readLine();
    	    	 System.out.println(input);
    	    	 System.out.println("Client heeft gelezen...");
        	 } catch (Exception e){
        		 System.out.println("Probleem met lezen aan clientzijde");
        	 }      	 
         }
         
         private static void handleWriteRequest(Socket client){
        	 try{
        		 System.out.println("Client probeert te schrijven...");
        		 PrintWriter out = new PrintWriter(client.getOutputStream(), true);
        		 out.println("Test schrijven client");
        		 System.out.println("Client heeft geschreven...");
        	 } catch (Exception e){
        		 System.out.println("Probleem met schrijven aan clientzijde");
        	 }
         }
    	
    }

    You first have to start the server, and then the client(s). The problem is that my client 'hangs' because it waits for a write from the server which he does, but i guess some place wrong or something...

    This is my problem...

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    786
    Rep Power
    5

    Default

    Client read: String input = in.readLine();
    Please read the API Doc :
    A line is considered to be terminated by any one
    * of a line feed ('\n'), a carriage return ('\r'), or a carriage return
    * followed immediately by a linefeed.
    And now look at your servers write method:

    out.write("Test schrijven server");

    no line feed, no carriage return ... so readLine block until it will get it :)

    In your client, you have used out.println("Test schrijven client"); , why do you dont use this at your server side? --> out.println("Test schrijven server");

  3. #3
    Tankske is offline Member
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Default thanks

    THANK YOU!!

    I knew it had to be something little but I was staring at my screen for an hour and didn't found it.

    Thanks a lot...

Similar Threads

  1. Replies: 2
    Last Post: 09-24-2010, 11:31 AM
  2. Replies: 3
    Last Post: 07-29-2009, 04:52 AM
  3. Replies: 1
    Last Post: 10-05-2008, 03:12 PM
  4. send/read int in a client/server app
    By dim_ath in forum New To Java
    Replies: 2
    Last Post: 01-03-2008, 01:03 PM
  5. Identify Client in Socket Client Server Application
    By masadjie in forum Networking
    Replies: 1
    Last Post: 12-20-2007, 09:18 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
  •