为什么getProperty方法不会打印出工作目录?

时间:2015-02-04 12:10:15

标签: java sockets client-server inputstream outputstream

我正在尝试构建一个简单的客户端 - 服务器应用程序。现在我想打印出服务器的当前工作目录。我尝试过使用String workingDir= System.getProperty("user.dir");方法。但它实际上并没有打印出目录。我不知道为什么。我需要帮助。

   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 );
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我认为问题可能更基本一点。您在客户端上执行writeBytes(),在服务器上执行readUTF(),反之亦然。如果你坚持使用writeBytes()/ readBytes()或writeUTF()/ readUTF()组合会更好。不要混淆两者。

来自javadoc of DataInput#readUTF()

  

接口DataOutput的writeUTF方法可用于写入适合此方法读取的数据。