Skip to content

Instantly share code, notes, and snippets.

@Crydust
Created January 24, 2021 19:15
Show Gist options
  • Save Crydust/101db83896b9546572c2d742958d4695 to your computer and use it in GitHub Desktop.
Save Crydust/101db83896b9546572c2d742958d4695 to your computer and use it in GitHub Desktop.
Java socket server and client
package spike;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.Timer;
import static java.nio.charset.StandardCharsets.UTF_8;
public class ClientApp {
public static final int CONNECT_TIMEOUT = 10_000;
public static final int READ_TIMEOUT = 10_000;
public static final int MESSAGE_TIMEOUT = 10_000;
public static void main(String[] args) {
if (args.length < 2) {
System.err.println("ERROR, no port provided");
System.exit(1);
return;
}
final String host = args[0];
final int port = Integer.parseInt(args[1]);
final InetSocketAddress address = new InetSocketAddress(host, port);
try (final Socket socket = new Socket()) {
socket.connect(address, CONNECT_TIMEOUT);
socket.setSoTimeout(READ_TIMEOUT);
final Timer timer = new Timer(true);
timer.schedule(new CloseResourceTimerTask(socket), MESSAGE_TIMEOUT);
try (final BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream());
final BufferedInputStream in = new BufferedInputStream(socket.getInputStream())) {
out.write("test".getBytes(UTF_8));
out.flush();
final byte[] buffer = new byte[256];
final int readBytes = in.read(buffer);
if (readBytes < 0) {
System.err.println("ERROR reading from socket");
System.exit(1);
return;
}
final String message = new String(buffer, 0, readBytes, UTF_8);
System.out.printf("The server replied: %s%n", message);
} finally {
timer.cancel();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
package spike;
import java.util.TimerTask;
class CloseResourceTimerTask extends TimerTask {
private final AutoCloseable resource;
CloseResourceTimerTask(AutoCloseable resource) {
this.resource = resource;
}
@Override
public void run() {
try {
resource.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
package spike;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import static java.nio.charset.StandardCharsets.UTF_8;
public class ServerApp {
public static final int CONNECT_TIMEOUT = 10_000;
public static final int READ_TIMEOUT = 10_000;
public static void main(String[] args) {
if (args.length < 1) {
System.err.println("ERROR, no port provided");
System.exit(1);
return;
}
final int port = Integer.parseInt(args[0]);
// address 127.0.0.1
final InetAddress bindAddr = InetAddress.getLoopbackAddress();
// address 0.0.0.0
// final InetAddress bindAddr = null;
try (final ServerSocket serverSocket = new ServerSocket(port, 1, bindAddr)) {
serverSocket.setSoTimeout(CONNECT_TIMEOUT);
System.out.printf("Waiting %d milliseconds for a client to connect to %s.%n", CONNECT_TIMEOUT, serverSocket.getLocalSocketAddress());
try (final Socket socket = serverSocket.accept();
final BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream());
final BufferedInputStream in = new BufferedInputStream(socket.getInputStream())) {
socket.setSoTimeout(READ_TIMEOUT);
System.out.printf("Waiting %d milliseconds for data.%n", READ_TIMEOUT);
final byte[] buffer = new byte[256];
final int readBytes = in.read(buffer);
if (readBytes < 0) {
System.err.println("ERROR reading from socket");
System.exit(1);
return;
}
final String message = new String(buffer, 0, readBytes, UTF_8);
System.out.printf("Here is the message: %s%n", message);
out.write("I got your message!".getBytes(UTF_8));
out.flush();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment