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
This commit is contained in:
Mainak Majumder 2021-06-21 04:03:52 +02:00 committed by GitHub
parent 203e065a01
commit 8baaa76e71
2 changed files with 89 additions and 0 deletions

View File

@ -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);
}
}

View File

@ -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);
}
}