source code for bealdung article "Find IP address of client" (#10995)

* 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"

* Source code for Baeldung article BAEL-4896

* update code

* source code for Baeldung article bael-4896

* add main method to client and server

* remove comment

* include command-line argument option for client, remove try-catch block

* update greetings message section
This commit is contained in:
Mainak Majumder 2021-09-06 00:08:08 +02:00 committed by GitHub
parent 6009a3bb87
commit ec6f5d90cc
2 changed files with 90 additions and 0 deletions

View File

@ -0,0 +1,39 @@
package com.baeldung.clientaddress;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class ApplicationClient {
private Socket clientSocket;
private PrintWriter out;
private BufferedReader in;
public void connect(String ip, int port) throws IOException {
clientSocket = new Socket(ip, port);
out = new PrintWriter(clientSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
}
public void sendGreetings(String msg) throws IOException {
out.println(msg);
String reply = in.readLine();
System.out.println("Reply received from the server :: " + reply);
}
public void disconnect() throws IOException {
in.close();
out.close();
clientSocket.close();
}
public static void main(String[] args) throws IOException {
ApplicationClient client = new ApplicationClient();
client.connect(args[0], Integer.parseInt(args[1])); // IP address and port number of the server
client.sendGreetings(args[2]); // greetings message
client.disconnect();
}
}

View File

@ -0,0 +1,51 @@
package com.baeldung.clientaddress;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class ApplicationServer {
private ServerSocket serverSocket;
private Socket connectedSocket;
private PrintWriter out;
private BufferedReader in;
public void startServer(int port) throws IOException {
serverSocket = new ServerSocket(port);
connectedSocket = serverSocket.accept();
InetSocketAddress socketAddress = (InetSocketAddress) connectedSocket.getRemoteSocketAddress();
String clientIpAddress = socketAddress.getAddress()
.getHostAddress();
System.out.println("IP address of the connected client :: " + clientIpAddress);
out = new PrintWriter(connectedSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(connectedSocket.getInputStream()));
String msg = in.readLine();
System.out.println("Message received from the client :: " + msg);
out.println("Hello Client !!");
closeIO();
stopServer();
}
private void closeIO() throws IOException {
in.close();
out.close();
}
private void stopServer() throws IOException {
connectedSocket.close();
serverSocket.close();
}
public static void main(String[] args) throws IOException {
ApplicationServer server = new ApplicationServer();
server.startServer(5000);
}
}