Condition for Transferring Data Between Two Systems
Description:
Data transfer between two systems in Java typically uses Socket programming for direct communication, where one system acts as a server and the other as a client. The server listens on a port, and the client connects to it, exchanging data via InputStream and OutputStream. For more complex data, `ObjectOutputStream` and `ObjectInputStream` can be used to transfer serialized objects. Alternatively, HTTP-based protocols like RESTful APIs can be employed for transferring data using libraries like HttpURLConnection. Exception handling, timeouts, and data integrity are important considerations during the transfer process.
public class Server {
public static void main(String args[]) {
int port = 1234; // Define port number
try (ServerSocket serverSocket = new ServerSocket(port)) {
System.out.println("Server is listening on port " + port);
try (Socket socket = serverSocket.accept()) {
System.out.println("Client connected");
BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String receivedMessage = input.readLine();
System.out.println("Received: " + receivedMessage);
PrintWriter output = new PrintWriter(socket.getOutputStream(), true);
output.println("Hello from Server!");
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
Screenshots
STEP 1: The client establishes a connection with the server using the specified port.
STEP 2: After sending data from the client, the server responds with a confirmation message.