Skip to content

Instantly share code, notes, and snippets.

@audinue
Created March 12, 2023 04:12
Show Gist options
  • Save audinue/33ef59ee95e06fab059ac9f4e194a746 to your computer and use it in GitHub Desktop.
Save audinue/33ef59ee95e06fab059ac9f4e194a746 to your computer and use it in GitHub Desktop.
AutoServer AutoClient
import java.io.*;
import java.net.*;
abstract class Client
{
Socket socket;
ObjectOutputStream out;
boolean closed;
void init(final Socket newSocket) {
new Thread() {
public void run() {
try {
socket = newSocket;
out = new ObjectOutputStream(socket.getOutputStream());
onOpen();
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
while (true) {
onMessage(in.readObject());
}
} catch (Exception e) {
close();
}
}
}.start();
}
Client(Socket socket) {
init(socket);
}
Client(String host, int port) {
try {
init(new Socket(host, port));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
abstract void onOpen();
abstract void onClose();
abstract void onMessage(Object data);
void send(final Object data) {
new Thread() {
public void run() {
try {
out.writeObject(data);
} catch (Exception e) {
close();
}
}
}.start();
}
void close() {
try {
if (!closed) {
closed = true;
onClose();
socket.close();
}
} catch (Exception e) {
}
}
}
abstract class Server
{
Server(final int port) {
new Thread() {
public void run() {
try {
ServerSocket socket = new ServerSocket(port);
while (true) {
new Client(socket.accept()) {
void onOpen() {
Server.this.onOpen(this);
}
void onClose() {
Server.this.onClose(this);
}
void onMessage(Object data) {
Server.this.onMessage(this, data);
}
};
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}.start();
}
abstract void onOpen(Client client);
abstract void onClose(Client client);
abstract void onMessage(Client client, Object data);
}
abstract class AutoServer
{
AutoServer() {
new Server(1111) {
void onOpen(Client client) {
AutoServer.this.onOpen(client);
}
void onClose(Client client) {
AutoServer.this.onClose(client);
}
void onMessage(Client client, Object data) {
AutoServer.this.onMessage(client, data);
}
};
new Thread() {
public void run() {
try {
DatagramSocket socket = new DatagramSocket();
DatagramPacket packet = new DatagramPacket(
new byte[] {1, 1, 1, 1},
4,
InetAddress.getByName("255.255.255.255"),
1110);
while (true) {
socket.send(packet);
Thread.sleep(1000);
}
} catch (Exception e) {
}
}
}.start();
}
abstract void onOpen(Client client);
abstract void onClose(Client client);
abstract void onMessage(Client client, Object data);
}
abstract class AutoClient
{
Client client;
void init() {
new Thread() {
public void run() {
try {
try (DatagramSocket socket = new DatagramSocket(1110)) {
DatagramPacket packet = new DatagramPacket(new byte[4], 4);
while (true) {
socket.receive(packet);
if (packet.getData()[0] == 1 && packet.getData()[1] == 1 && packet.getData()[2] == 1 && packet.getData()[3] == 1) {
client = new Client(packet.getAddress().getHostAddress(), 1111) {
void onOpen() {
AutoClient.this.onOpen();
}
void onClose() {
AutoClient.this.onClose();
client = null;
AutoClient.this.init();
}
void onMessage(Object data) {
AutoClient.this.onMessage(data);
}
};
break;
}
}
}
} catch (Exception e) {
}
}
}.start();
}
AutoClient() {
init();
}
abstract void onOpen();
abstract void onClose();
abstract void onMessage(Object data);
void send(Object data) {
if (client != null) {
client.send(data);
}
}
void close() {
if (client != null) {
client.close();
}
}
}
class AutoServerTest {
public static void main(String[] args) {
new AutoServer() {
void onOpen(Client client) {
System.out.println("[AUTOSERVER] onOpen");
client.send("Hello from server!");
}
void onClose(Client client) {
System.out.println("[AUTOSERVER] onClose");
}
void onMessage(Client client, Object data) {
System.out.println("[AUTOSERVER] onMessage " + data);
}
};
}
}
class AutoClientTest {
public static void main(String[] args) {
new AutoClient() {
void onOpen() {
System.out.println("[AUTOCLIENT] onOpen");
send("Hello from client!");
}
void onClose() {
System.out.println("[AUTOCLIENT] onClose");
}
void onMessage(Object data) {
System.out.println("[AUTOCLIENT] onMessage " + data);
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment