Skip to content

Instantly share code, notes, and snippets.

@odds-get-evened
Last active November 1, 2021 22:19
Show Gist options
  • Save odds-get-evened/abb0fea6e323dc0e8879d7b1250b2fa5 to your computer and use it in GitHub Desktop.
Save odds-get-evened/abb0fea6e323dc0e8879d7b1250b2fa5 to your computer and use it in GitHub Desktop.
Echo client/server pair
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import java.io.IOException;
import java.net.*;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Scanner;
import java.util.stream.Stream;
class EchoClient {
private byte[] buffer = new byte[128];
private DatagramSocket socket;
private InetAddress address;
private Scanner commandLine = new Scanner(System.in);
private boolean isRunning = true;
public EchoClient() throws IOException {
socket = new DatagramSocket();
address = InetAddress.getByName("localhost");
Scanner scan = new Scanner(System.in);
System.out.print(">> ");
String line = scan.nextLine();
while(isRunning) {
if(line.startsWith("end")) {
System.out.println("closing...");
isRunning = false;
socket.close();
System.exit(0);
} else {
if(!line.isEmpty()) {
String[] a = StringUtils.split(line);
List<String> b = Arrays.stream(a).map(v -> v.trim().toLowerCase()).toList();
a = b.toArray(new String[b.size()]);
if(a.length == 1) {
// no arguments taken, just run the job
} else if(a.length > 1) {
String command = a[0];
String[] args = ArrayUtils.subarray(a, 1, a.length);
procCommand(command, args);
} else {
}
}
}
System.out.print(">> ");
line = scan.nextLine();
}
}
private void procCommand(String command, String[] args) throws IOException {
switch(command) {
case "send":
if(args.length > 0) sendEcho(StringUtils.join(args, " "));
break;
}
}
public String sendEcho(String msg) throws IOException {
buffer = msg.getBytes(StandardCharsets.UTF_8);
DatagramPacket packet = new DatagramPacket(buffer, buffer.length, address, 6881);
socket.send(packet);
packet = new DatagramPacket(buffer, buffer.length);
socket.receive(packet);
String received = new String(packet.getData(), 0, packet.getLength());
System.out.println("from client: " + received);
return received;
}
public void close() {
socket.close();
}
}
class EchoServer extends Thread {
private DatagramSocket socket;
private boolean isRunning = false;
private byte[] buffer = new byte[128];
public EchoServer() throws SocketException {
socket = new DatagramSocket(6881);
}
@Override
public void run() {
isRunning = true;
while(isRunning) {
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
try {
socket.receive(packet);
} catch (IOException e) {
e.printStackTrace();
}
InetAddress address = packet.getAddress();
int port = packet.getPort();
packet = new DatagramPacket(buffer, buffer.length, address, port);
String received = new String(packet.getData(), 0, packet.getLength());
System.out.println("from server: " + received);
if(received.equals("end")) {
isRunning = false;
continue;
}
try {
socket.send(packet);
} catch (IOException e) {
e.printStackTrace();
}
}
socket.close();
}
}
class Scratch {
private static EchoClient client;
public static void main(String[] args) throws IOException {
new EchoServer().start();
client = new EchoClient();
/*
String echo = client.sendEcho("hello, dork!");
echo = client.sendEcho("server is working.");
client.sendEcho("end");
client.close();
*/
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment