From 8baaa76e71afff64d07ae23388b59bb05b14f260 Mon Sep 17 00:00:00 2001 From: Mainak Majumder Date: Mon, 21 Jun 2021 04:03:52 +0200 Subject: [PATCH] code for baeldung article (#10875) * Application source code for the Baeldung article "HTTP PUT vs POST method in REST API" * update indention in pom file, update code in Address class * update indention * rename application * update pom * source code for article "Connection timeout vs read timeout" * Update TcpServerSocket.java --- .../com/baeldung/timeout/TcpClientSocket.java | 46 +++++++++++++++++++ .../com/baeldung/timeout/TcpServerSocket.java | 43 +++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 core-java-modules/core-java-networking-3/src/main/java/com/baeldung/timeout/TcpClientSocket.java create mode 100644 core-java-modules/core-java-networking-3/src/main/java/com/baeldung/timeout/TcpServerSocket.java diff --git a/core-java-modules/core-java-networking-3/src/main/java/com/baeldung/timeout/TcpClientSocket.java b/core-java-modules/core-java-networking-3/src/main/java/com/baeldung/timeout/TcpClientSocket.java new file mode 100644 index 0000000000..a482db3543 --- /dev/null +++ b/core-java-modules/core-java-networking-3/src/main/java/com/baeldung/timeout/TcpClientSocket.java @@ -0,0 +1,46 @@ +package com.baeldung.timeout; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.PrintStream; +import java.net.Socket; +import java.net.UnknownHostException; + +public class TcpClientSocket { + + private Socket socket; + private PrintStream out; + private BufferedReader in; + + public void connect(String host, int port) { + try { + socket = new Socket(host, port); + socket.setSoTimeout(30000); + System.out.println("connected to " + host + " on port " + port); + + out = new PrintStream(socket.getOutputStream(), true); + System.out.println("Sending message ... "); + out.println("Hello world"); + + in = new BufferedReader(new InputStreamReader(socket.getInputStream())); + out.println(in.readLine()); + + System.out.println("Closing connection !!! "); + in.close(); + out.close(); + socket.close(); + + } catch (UnknownHostException e) { + System.err.println(e); + } catch (IOException e) { + System.err.println(e); + } + } + + public static void main(String[] args) throws IOException { + TcpClientSocket client = new TcpClientSocket(); + client.connect("127.0.0.1", 5000); + } + +} diff --git a/core-java-modules/core-java-networking-3/src/main/java/com/baeldung/timeout/TcpServerSocket.java b/core-java-modules/core-java-networking-3/src/main/java/com/baeldung/timeout/TcpServerSocket.java new file mode 100644 index 0000000000..452aaac191 --- /dev/null +++ b/core-java-modules/core-java-networking-3/src/main/java/com/baeldung/timeout/TcpServerSocket.java @@ -0,0 +1,43 @@ +package com.baeldung.timeout; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.ServerSocket; +import java.net.Socket; + +public class TcpServerSocket { + + private Socket socket; + private ServerSocket serverSocket; + private BufferedReader in; + + public TcpServerSocket(int port) { + + try { + serverSocket = new ServerSocket(port); + System.out.println("Server is listening on port :: " + port); + System.out.println("Waiting for a client ..."); + + socket = serverSocket.accept(); + socket.setSoTimeout(40000); + System.out.println("Client connected !! "); + + in = new BufferedReader(new InputStreamReader(socket.getInputStream())); + + String line = in.readLine(); + System.out.println(line); + + System.out.println("Closing connection !!! "); + + socket.close(); + in.close(); + } catch (IOException i) { + System.out.println(i); + } + } + + public static void main(String args[]) { + TcpServerSocket server = new TcpServerSocket(5000); + } +}