Created
January 24, 2025 05:28
-
-
Save Zhomart/2afd4ee349bc4c94320a3c5e01944f49 to your computer and use it in GitHub Desktop.
Http client/server example - java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.io.PrintWriter; | |
import java.net.ServerSocket; | |
import java.net.Socket; | |
class Client { | |
public static void main(String[] args) { | |
// main thread | |
System.out.println("hello from client"); | |
try { | |
Socket socket = new Socket("localhost", 3000); | |
System.out.println("connected to the server"); | |
System.out.println("port=" + socket.getPort()); | |
System.out.println("localPort=" + socket.getLocalPort()); | |
PrintWriter pw = new PrintWriter(socket.getOutputStream(), true); | |
pw.println("GET / HTTP/1.1"); | |
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); | |
String line = in.readLine(); // reads until "\n" | |
System.out.println("received: " + line); | |
in.close(); | |
socket.close(); | |
} catch (IOException e) { | |
System.out.println("Error " + e.getMessage()); | |
e.printStackTrace(); | |
} | |
System.out.println("finished"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.io.PrintWriter; | |
import java.net.ServerSocket; | |
import java.net.Socket; | |
import java.time.Instant; | |
public class Server { | |
public static void main(String[] args) { | |
// main thread | |
println("hello"); | |
new Server().run(); | |
println("finished"); | |
} | |
private void run() { | |
try { | |
final ServerSocket server = new ServerSocket(3000); | |
while (true) { | |
println("Waiting for a client to be connected...."); | |
// blocks, until a client is connected | |
final Socket client = server.accept(); | |
println("A client connected, running it inside a thread"); | |
new Thread(() -> { | |
println("A thread for client is started"); | |
try { | |
handleClient(client); | |
} catch (IOException e) { | |
println("Client Error " + e.getMessage()); | |
e.printStackTrace(); | |
} | |
}).start(); | |
} | |
// server.close(); | |
} catch (IOException e) { | |
println("Error " + e.getMessage()); | |
e.printStackTrace(); | |
} | |
} | |
private void handleClient(final Socket client) throws IOException { | |
println("yay, a client is connected: " + client.getInetAddress()); | |
println("local port: " + client.getLocalPort()); | |
println("port: " + client.getPort()); | |
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream())); | |
String line = in.readLine(); | |
println("a request from client: " + line); | |
String[] chunks = line.split(" "); | |
String method = chunks[0]; | |
String path = chunks[1]; | |
String httpVersion = chunks[2]; | |
println("HTTP method=" + method); | |
println("HTTP path=" + path); | |
println("HTTP version=" + httpVersion); | |
PrintWriter pw = new PrintWriter(client.getOutputStream(), true); | |
if (path.equals("/")) { | |
HomeController controller = new HomeController(); | |
String response = controller.get(); | |
pw.println("HTTP/1.1 200 OK"); | |
pw.println("Content-Type: text/html; charset=UTF-8"); | |
pw.println("Location: http://localhost/"); | |
pw.println(); | |
pw.println(response); | |
} else { | |
pw.println("HTTP/1.1 404 Not Found"); | |
pw.println("Content-Type: application/text"); | |
} | |
pw.close(); | |
client.close(); | |
} | |
static class HomeController { | |
public String get() { | |
try { | |
println("computing large computation open ai"); | |
Thread.sleep(10_000); | |
println("finished computing large llm"); | |
} catch (Exception e) {} | |
return "<h1>My app</h1><b>hello from home controller</b>"; | |
} | |
} | |
static void println(String msg) { | |
String now = Instant.now().toString(); | |
Thread t = Thread.currentThread(); | |
System.out.println(now + " - " + "[" + t.getName() + ":" + t.threadId() + "] " + msg); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment