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

📄 mathclient.java

📁 JAVA实现的一个能够做多个数学功能的client-server程序。
💻 JAVA
字号:
import java.io.*;
import java.net.*;
import java.util.Scanner;

public class MathClient {
	private static String serverHostName = "";
	private static int port = 0;
	public static void main(String[] args){
		float val = 0;
		Scanner input = new Scanner(System.in);
		if(args.length != 2){
			System.out.println("Error argument!");
			System.exit(0);
		}
		serverHostName = args[0];
		try{
			port = Integer.parseInt(args[1]);
		}catch(Exception e){
			System.out.println("Error port number!");
			System.exit(0);
		}
		System.out.println("Input the match function:");
		String function = input.nextLine();
		System.out.println("Input a number:");
		String num = input.nextLine();
		val = gridbus_rpc(function,num);
		if(function.equalsIgnoreCase("prime")){
			if(val == 1){
				System.out.println("Is a prime!");
				System.exit(0);
			}
			else{
				System.out.println("Not a prime!");
				System.exit(0);
			}
		}
		System.out.println(val);
		
	}
	
	public static float gridbus_rpc(String mathFunction,String value){
		String tempRes = "";
		float result = 0;
		try{
			//open a connection to the server
			Socket socket = new Socket(serverHostName,port);
			
			//send the math function name and the argument to the math server
			BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
			writer.write(mathFunction+":"+value);
			writer.newLine();
			writer.flush();
			
			//read the result from the data stream
			BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			try{
				tempRes = reader.readLine().trim();
				result = (float)Float.parseFloat(tempRes);
			}catch(Exception e){
				System.out.println(tempRes);
				System.exit(0);
			}
			reader.close();
			writer.close();
			socket.close();
		}catch(Exception e){
			e.printStackTrace();
		}
		return result;
	}
}

⌨️ 快捷键说明

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