HttpClientCel:
ServerSocket, Socket, strumienie tekstoweWskazowki:
accept()println + readLineRozwiazanie - serwer:
import java.io.*; import java.net.*; public class EchoTcpServer { public static void main(String[] args) throws IOException { try (ServerSocket server = new ServerSocket(5000)) { System.out.println("Serwer TCP: 5000"); while (true) { Socket client = server.accept(); new Thread(() -> handle(client)).start(); } } } private static void handle(Socket client) { try (client; BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream())); PrintWriter out = new PrintWriter(client.getOutputStream(), true)) { String line = in.readLine(); out.println("ECHO: " + line); } catch (IOException e) { e.printStackTrace(); } } }
Rozwiazanie - klient:
import java.io.*; import java.net.*; public class EchoTcpClient { public static void main(String[] args) throws IOException { try (Socket socket = new Socket("127.0.0.1", 5000); BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); PrintWriter out = new PrintWriter(socket.getOutputStream(), true)) { out.println("Ala ma kota"); System.out.println(in.readLine()); } } }
Cel:
DatagramSocket i DatagramPacketWskazowki:
packet.getLength() przy odczycieRozwiazanie - odbiorca:
import java.net.*; import java.nio.charset.StandardCharsets; public class UdpReceiver { public static void main(String[] args) throws Exception { try (DatagramSocket socket = new DatagramSocket(6000)) { byte[] buffer = new byte[1024]; DatagramPacket packet = new DatagramPacket(buffer, buffer.length); socket.receive(packet); String text = new String(packet.getData(), 0, packet.getLength(), StandardCharsets.UTF_8); System.out.println("Odebrano: " + text); } } }
Rozwiazanie - nadawca:
import java.net.*; import java.nio.charset.StandardCharsets; public class UdpSender { public static void main(String[] args) throws Exception { try (DatagramSocket socket = new DatagramSocket()) { byte[] data = "ping-udp".getBytes(StandardCharsets.UTF_8); DatagramPacket packet = new DatagramPacket( data, data.length, InetAddress.getByName("127.0.0.1"), 6000 ); socket.send(packet); } } }
Cel:
HttpClientWskazowki:
statusCode i fragment odpowiedziIOExceptionRozwiazanie:
import java.net.URI; import java.net.http.*; import java.time.Duration; public class HttpGetDemo { public static void main(String[] args) throws Exception { HttpClient client = HttpClient.newBuilder() .connectTimeout(Duration.ofSeconds(5)) .build(); HttpRequest request = HttpRequest.newBuilder() .uri(URI.create("https://api.github.com")) .header("Accept", "application/json") .GET() .build(); HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString()); System.out.println("Status: " + response.statusCode()); System.out.println(response.body().substring(0, Math.min(200, response.body().length()))); } }
Cel:
/time zwracajacy JSONWskazowki:
com.sun.net.httpserver.HttpServerContent-Type i kod odpowiedzi 200OutputStream po zapisie odpowiedziRozwiazanie:
import com.sun.net.httpserver.HttpServer; import java.io.OutputStream; import java.net.InetSocketAddress; import java.nio.charset.StandardCharsets; import java.time.Instant; public class MiniHttpServer { public static void main(String[] args) throws Exception { HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0); server.createContext("/time", exchange -> { String body = "{\"time\":\"" + Instant.now() + "\"}"; exchange.getResponseHeaders().add("Content-Type", "application/json; charset=utf-8"); byte[] bytes = body.getBytes(StandardCharsets.UTF_8); exchange.sendResponseHeaders(200, bytes.length); try (OutputStream os = exchange.getResponseBody()) { os.write(bytes); } }); server.start(); System.out.println("HTTP server: http://localhost:8080/time"); } }
Cel:
Wskazowki:
Rozwiazanie:
for (int attempt = 1; attempt <= 3; attempt++) { try { HttpResponse<String> r = client.send(request, HttpResponse.BodyHandlers.ofString()); if (r.statusCode() < 500) { System.out.println("OK w probie " + attempt); break; } } catch (Exception e) { if (attempt == 3) throw e; } Thread.sleep(200L * attempt); }