搜题
问题   更新时间2023/4/3 12:59:00

阅读以下程序段,假定服务器端和客户端程序正常运行无异常发生,请回答以下问题:
①描述程序功能。(3分)
②假定在客户端输入2.0,请写出服务器端的输出结果。(3分)
//服务器端程序

import java.io.*; import java.net.*; public class Server { public static void main(String[] args) { try { ServerSocket serverSocket = new ServerSocket(8000); Socket connectToClient = serverSocket.accept(); DataInputStream isFromClient = new DataInputStream( connectToClient.getInputStream()); DataOutputStream osToClient = new DataOutputStream( connectToClient.getOutputStream()); while (true) { double value = isFromClient.readDouble(); System.out.println("value received from client: " +value); double result = value*value; osToClient.writeDouble(result); osToClient.flush(); System.out.println("result: " + result); } } catch(IOException ex) { System.err.println(ex); } } } //客户端程序 import java.io.*; import java.net.*; public class Client { public static void main(String[] args) { try { Socket connectToServer = new Socket("localhost", 8000); DataInputStream isFromServer = new DataInputStream( connectToServer.getInputStream()); DataOutputStream osToServer = new DataOutputStream(connectToServer.getOutputStream()); while (true) { System.out.print("Please enter a double value: "); double value=Double.parseDouble( new BufferedReader(new InputStreamReader(System.in),1).readLine()); osToServer.writeDouble(value); osToServer.flush(); double result = isFromServer.readDouble(); System.out.println("result received from the server is "+ result); } } catch (IOException ex) { System.err.println(ex); } } } 客户端接受键盘输入,然后把他转换为Double类型的数据,传送给服务器,服务器收到后,进行乘方运算,然后把该值在服务器的屏幕上打印,并把结果传送给客户端. 输入2.0后,服务器上的结果是4.
王老师:19139051760(拨打)