Select Git revision
Client.java
Client.java 1.97 KiB
package ftp;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Client {
int cport;
int timeout;
private static SocketChannel client;
private static ByteBuffer buffer;
private static Client instance;
/**
* @desc constructs a client
* @param cport controller port to use
* @param timeout timeout (ms)
*/
public Client(int cport, int timeout) {
this.cport = cport;
this.timeout = timeout;
try {
client = SocketChannel.open(new InetSocketAddress(cport));
buffer = ByteBuffer.allocate(256);
} catch (IOException e) {
e.printStackTrace();
}
}
public String sendMessage(String msg) {
buffer = ByteBuffer.wrap(msg.getBytes());
String response = null;
try {
client.write(buffer);
buffer.clear();
client.read(buffer);
response = new String(buffer.array()).trim();
System.out.println("response=" + response);
buffer.clear();
} catch (IOException e) {
e.printStackTrace();
}
return response;
}
public static void main(String args[]) {
Stream<String> str = Arrays.stream(args);
List<Integer> intArgs = str.map(x -> {return Integer.parseInt(x);})
.collect(Collectors.toList());
instance = new Client(intArgs.get(0),intArgs.get(1));
Scanner scanner = new Scanner(System.in);
String command = "";
while (!command.equals("quit")) {
System.out.println("Enter Command:");
command = scanner.nextLine();
String response = instance.sendMessage(command);
System.out.println(response);
}
}
}