簡體   English   中英

如何制作Java客戶端程序

[英]How to make a Java client program

該程序未處理客戶端/服務器請求,並且未分配響應變量...線程保持掛起狀態,將用戶輸入從客戶端發送到服務器后沒有任何反應...任何幫助?

代碼如下:

public class NumberClient
{
   public static void main(String[] args)
         throws IOException
   {
      final int PORT = 8899;
      Scanner console = new Scanner(System.in);

      while (true)
      {          
          Socket s = new Socket("localhost", PORT);
          InputStream instream = s.getInputStream();
          OutputStream outstream = s.getOutputStream();
          Scanner in = new Scanner(instream);
          PrintWriter out = new PrintWriter(outstream);

          System.out.println("Enter a number to process: ");
          double newnumber = console.nextDouble();
          if (newnumber == 0) System.exit(0);

          System.out.print("Sending: " + new number);
          //does nothing after here

          out.print(newnumber);
          out.flush();          

          String response = in.nextLine();
          System.out.println("The square root of " + newnumber + " is: " + response);

         s.close();
      }
   }
}

這是我的服務器程序,使用Squarerooter.java計算平方根

    import java.io.IOException;
    import java.net.ServerSocket;
    import java.net.Socket;

    /**
    A server that executes the the Squarerooter command.
    */
    public class SquareRootServer
    {
    public static void main(String[] args) throws IOException
       { 

          final int SBAP_PORT = 8899;
          ServerSocket server = new ServerSocket(SBAP_PORT);
          System.out.println("Waiting for clients to connect . . . ");


      while (true)
           {
              Socket s = server.accept();
              System.out.println("Client connected.");
              Squarerooter service = new Squarerooter(s);

              Thread t = new Thread(service);
              t.start();
           }
        }
     }

這是Squarerooter.java / *類,用於計算平方根並將結果寫入客戶端。 * /

    import java.io.InputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.io.PrintWriter;
    import java.net.Socket;
    import java.util.Scanner;
    import java.lang.*;

    /**
    Executes Simple square root computation on number from a socket.
    */
     public class Squarerooter implements Runnable
     {
      private Socket s;
      private Scanner in;
      private PrintWriter out;

        /**
      Constructs a object that processes commands
      from a socket.
           @param aSocket the socket
        */
      public Squarerooter(Socket aSocket)
        {
           s = aSocket;
        }

      public void run()
        {
      try
           {
          try
              {
                 in = new Scanner(s.getInputStream());
                 out = new PrintWriter(s.getOutputStream());
                 squareRoot();
              }
      finally
              {
                 s.close();
              }
           }
      catch (IOException exception)
           {
              exception.printStackTrace();
           }
        }

    /**
    Executes a command until the end of input.
    */

      public void squareRoot() throws IOException
        {

      while (true)
           { 
           if (!in.hasNext()) return;
           double mynumber = in.nextDouble();
           if (mynumber == 0) return;
           else executeCommand(mynumber);
           }
         }  

         /** Executes a single command.
             @param mynumber the number to square
         */
         public void executeCommand(double mynumber)
         {

                mynumber = in.nextDouble();

                if (mynumber == 0) return;
                else
                {
                mynumber = Math.sqrt(mynumber);
                System.out.print(mynumber);

                out.print(mynumber);
                out.flush();
                }
         }

    }

這是我用來測試連接的ClientServerDemo.java

    import java.io.IOException;

    public class ClientServerDemo
    {
       static class ServerRunnable implements Runnable
       {
          public void run()
          {
             // start the client
             Thread t2 = new Thread(new ClientRunnable());
             t2.start();

             try
             {
                SquareRootServer.main(null);
             }
             catch (IOException ex)
             {
                ex.printStackTrace();
             }
          }
       };

       static class ClientRunnable implements Runnable
       {
          public void run()
          {
             try
             {
                NumberClient.main(null);
             }
             catch (IOException ex)
             {
                ex.printStackTrace();
             }
          }
       };


       public static void main(String[] args) throws InterruptedException
       {
          // start the server
          Thread t1 = new Thread(new ServerRunnable());
          t1.start();           
       }
    }

檢查Squarerooter.run()方法主體。 或在這里分享。

編輯:

問題是您兩次從套接字讀取了該數字。 第一次是在SquareRootServer.squareRoot()中,第二次是在SquareRootServer.executeCommand()中。 號碼丟了嗎?

另外,SquareRootServer.run()中的try-catch塊也很奇怪。 我認為嵌套捕獲是沒有用的。 用它做一個方塊。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM