Search code examples
javasocketsclient-serverinputstreamoutputstream

Why getProperty method doesn't print out the working directory?


I am trying to build a simple client-server aplication. Now I want to print out the current working directory of the server. I have tried by using String workingDir= System.getProperty("user.dir"); method. But it doesn't actually print out the directory. I don't know why. I need help.

   package clint;
import java.io.*;
import java.net.*;
import java.util.Scanner;

public class AreaClient{
   private static final int PORT = 8000;
    private static final String SERVER = "194.47.46.146";

    public static void main ( String [] args ){
       String server = "localhost";
       int port = PORT;

        if ( args.length >= 1 )
          server = args[0];
        if (args.length >= 2)
          port = Integer.parseInt( args[1]);

        new AreaClient ( server, port ); 
    }

    public AreaClient( String server, int port ){
      DataInputStream inputFromServer;
      DataOutputStream outputToServer;

      try {
        // create a socket to connect to the server
        Socket socket = new Socket( server, port );

         // create data input/output streams
         inputFromServer = new DataInputStream( socket.getInputStream( ) );
         outputToServer = new DataOutputStream( socket.getOutputStream( ) );

         // create Scanner object to read command from the keyboard
         Scanner sc = new Scanner ( System.in );
         System.out.print("Enter pwd command = ");
         while ( sc.hasNext()){
            // get the pwd command
             String command = sc.nextLine();

             // send the pwd command to sever
             outputToServer.writeBytes(command );
             outputToServer.flush( );

             // get working directory from server
             String workingDir = inputFromServer.readUTF();

             System.out.println("Working directory is: " + workingDir);

         }
         inputFromServer.close();
         outputToServer.close();
         socket.close( );
        } catch ( IOException e ){
          System.err.println( e );
        }

    }

}




package server;
import java.io.*;
import java.net.*;

public class AreaServer{
   private static final int PORT = 8000;
    private ServerSocket serverSocket;

    public static void main ( String [] args ){
       int port = PORT;
        if ( args.length == 1 )
           port = Integer.parseInt( args[0] );  
        new AreaServer( port );
    }

    public AreaServer( int port ){
      // create a server socket
      try {
          serverSocket = new ServerSocket( port );
      } catch ( IOException e ) {
         System.err.println( "Error in creation of the server socket");
          System.exit( 0 );
      }

      while ( true ) {
        try {
           // listen for a connection
            Socket socket = serverSocket.accept( ) ;

             // create data input/output streams
             DataInputStream inputFromClient = new DataInputStream( socket.getInputStream( ));
            DataOutputStream outputToClient = new DataOutputStream( socket.getOutputStream( ) );

             // loop to service a client
            while ( true ){
                //receive command from client
              String command = inputFromClient.readUTF();

              //getting the directory
              String workingDir=  System.getProperty("user.dir");

              //send the working directory to client
              outputToClient.writeBytes(workingDir );
             } 

            } catch ( IOException e ){
              System.err.println( e );
            }
        }
    }
}

Solution

  • I think the problem maybe a little more basic. You are doing a writeBytes() on your client and a readUTF() on the server and vice versa. It would be better if you stick to writeBytes() / readBytes() or a writeUTF() / readUTF() combination. Do not mix the two.

    From the javadoc of DataInput#readUTF()

    The writeUTF method of interface DataOutput may be used to write data that is suitable for reading by this method.