Created
December 17, 2020 12:04
-
-
Save aryadiahmad4689/bac8e9ce849d6d3d50cd9261d3558814 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.io.PrintStream; | |
import java.net.Socket; | |
import java.util.Scanner; | |
public class RPCClient { | |
private final PrintStream printStream; | |
@SuppressWarnings("CallToThreadStartDuringObjectConstruction") | |
public RPCClient(String ipAddress, int port) throws IOException { | |
Socket rpcClient = new Socket(ipAddress, port); | |
new Thread(() -> { | |
try { | |
BufferedReader reader = new BufferedReader(new | |
InputStreamReader(rpcClient.getInputStream())); | |
String line; | |
while ((line = reader.readLine()) != null) { | |
System.out.println("Server response : " + line); | |
System.out.print("\nCommands [A, B, C, D, E, exit] : "); | |
} | |
} catch (IOException ex) { | |
System.err.println("\nDisconnected!!"); | |
System.exit(0); | |
} | |
}).start(); | |
printStream = new PrintStream(rpcClient.getOutputStream(), true); | |
} | |
public void sendMessage(String operation) { | |
System.out.println("Di Buat Oleh Ariadi Ahmad"); | |
Scanner scan = new Scanner(System.in); | |
System.out.print("\nEnter Jumlah Kerja : "); | |
int f1 = scan.nextInt(); | |
// System.out.print("Enter 2nd number : "); | |
// int s1 = scan.nextInt(); | |
printStream.println(operation + ":" + f1); | |
} | |
public static void main(String[] args) { | |
try { | |
Scanner scan = new Scanner(System.in); | |
System.out.print("Enter server ip address : "); | |
String ipAddress = scan.nextLine(); | |
System.out.print("Enter connection port : "); | |
int port = scan.nextInt(); | |
RPCClient client = new RPCClient(ipAddress, port); | |
System.out.println("\nConnected to server\n"); | |
System.out.print("Commands [A, B, C, D, E, exit] : "); | |
while (true) { | |
scan = new Scanner(System.in); | |
String command = scan.nextLine(); | |
if (command.equals("exit")) { | |
System.exit(0); | |
} | |
client.sendMessage(command); | |
System.out.print("\n"); | |
} | |
} catch (IOException ex) { | |
System.err.println("\nUnable to connected!"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment