⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 client.java

📁 此源码为机械工业出版社出版的《Java语言程序设计》第三版所配套的书中所有源代码。
💻 JAVA
字号:
// Client.java: The client sends the input to the server and receives
// result back from the server
import java.io.*;
import java.net.*;

public class Client
{
  // Main method
  public static void main(String[] args)
  {
    try
    {
      // Create a socket to connect to the server
      Socket connectToServer = new Socket("localhost", 8000);
      // Socket connectToServer = new Socket("bellamy.armstrong.edu", 8000);

      // Create an input stream to receive data from the server
      DataInputStream isFromServer = new DataInputStream(
        connectToServer.getInputStream());

      // Create an output stream to send data to the server
      DataOutputStream osToServer =
        new DataOutputStream(connectToServer.getOutputStream());

      // Continuously send radius and receive area from the server
      while (true)
      {
        // Read the radius from the keyboard
        System.out.print("Please enter a radius: ");
        double radius = MyInput.readDouble();

        // Send the radius to the server
        osToServer.writeDouble(radius);
        osToServer.flush();

        // Get area from the server
        double area = isFromServer.readDouble();

        // Print area on the console
        System.out.println("Area received from the server is "
          + area);
      }
    }
    catch (IOException ex)
    {
      System.err.println(ex);
    }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -