Commit fb27b257 authored by Maxime POULAIN's avatar Maxime POULAIN
Browse files

fix java client

parent 7cde9fe6
import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
......@@ -10,31 +11,28 @@ public class Main {
System.out.println("\t client [ip] [port]");
}
private static int menu(String ip) {
private static char menu(String ip) {
System.out.println("──────────────────" + ip + "─────────────────");
System.out.println("1) Size");
System.out.println("2) Sort");
System.out.println("3) Exit");
Scanner scanner = new Scanner(System.in);
return scanner.nextInt();
return scanner.nextLine().charAt(0);
}
private static void loop(String ip, int port){
private static void loop(String ip, int port) {
Socket socketOfClient = null;
BufferedWriter os = null;
BufferedReader is = null;
char choice = 0;
Scanner scanner = new Scanner(System.in);
char[] buffer = new char[1024];
try {
socketOfClient = new Socket(ip, port);
// Create output stream at the client (to send data to the server)
os = new BufferedWriter(new OutputStreamWriter(socketOfClient.getOutputStream()));
// Input stream at Client (Receive data from the server).
is = new BufferedReader(new InputStreamReader(socketOfClient.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host " + ip);
return;
......@@ -42,34 +40,25 @@ public class Main {
System.err.println("Couldn't get I/O for the connection to " + ip);
return;
}
try {
// Write data to the output stream of the Client Socket.
os.write("HELO");
// End of line
os.newLine();
// Flush data.
os.flush();
os.write("I am Tom Cat");
os.newLine();
os.flush();
os.write("QUIT");
os.newLine();
os.flush();
// Read data sent from the server.
// By reading the input stream of the Client Socket.
String responseLine;
while ((responseLine = is.readLine()) != null) {
System.out.println("Server: " + responseLine);
if (responseLine.indexOf("OK") != -1) {
do {
choice = menu(ip);
if (choice == '3') {
os.write("exit");
os.newLine();
os.flush();
break;
}
}
os.write(choice);
os.write(' ');
os.write(scanner.nextLine());
os.write('\0');
os.flush();
int result = is.read(buffer, 0, 1024);
System.out.println("Server: " + fromBuffer(buffer));
} while (true);
os.close();
is.close();
......@@ -84,6 +73,16 @@ public class Main {
}
private static String fromBuffer(char[] buffer){
StringBuilder sb = new StringBuilder();
int i = 0;
while(i < 1024 && buffer[i] != '\0'){
sb.append(buffer[i]);
i++;
}
return sb.toString();
}
public static void main(String[] args) {
if (args.length != 2) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment