Fading Coder

One Final Commit for the Last Sprint

Home > Notes > Content

Establishing Dual Server Connections in Java

Notes May 17 2

A common scenario in distributed systems involves a Java application acting as a client to two separate servers, or one server process that needs to accept connections from two disitnct clients. The core mechanism relies on the ServerSocket class for accepting incoming connections and the Socket class for initiating connections.

Workflow Overview

  1. Instantiate two ServerSocket instances bound to different ports.
  2. Create two Socket objects pointing to the respective server addresses.
  3. Initiate the client connections to the remote servers.
  4. Accept the connections on the server side.
  5. Exchange data over the established channels.

Implementation Steps

Step 1 – Initialize Server Sockets

ServerSocket primaryServer = new ServerSocket(8080);
ServerSocket secondaryServer = new ServerSocket(9090);

The two server sockets listen independently on the specified ports, ready to accept incoming Socket connections.

Step 2 – Create Client Sockets and Connect

Socket clientA = new Socket();
clientA.connect(new InetSocketAddress("192.168.10.100", 8080), 5000);

Socket clientB = new Socket("192.168.10.200", 9090);

clientA uses a no-arg constructor and explicitly connects with a timeout. clientB performs the connection directly in the constructor.

Step 3 – Accept Remote Connections

Socket serverConnA = primaryServer.accept();
Socket serverConnB = secondaryServer.accept();

Each call to accept() blocks until a client connection is received and returns a dedicated Socket for communication.

Step 4 – Transfer Data

Once connections are established, input and output streams are obtained from each Socket:

// For serverConnA (reading from client)
BufferedReader readerA = new BufferedReader(new InputStreamReader(serverConnA.getInputStream()));
PrintWriter writerA = new PrintWriter(serverConnA.getOutputStream(), true);

// For clientB (sending to server)
PrintWriter writerB = new PrintWriter(clientB.getOutputStream(), true);
BufferedReader readerB = new BufferedReader(new InputStreamReader(clientB.getInputStream()));

// Example exchange
writerB.println("Hello from client B");
String response = readerA.readLine();

Any real implementation must handle exceptions (IOException, SocketTimeoutException) and properly close all resources in a finally block or using try-with-resources.

Tags: Java

Related Articles

Designing Alertmanager Templates for Prometheus Notifications

How to craft Alertmanager templates to format alert messages, improving clarity and presentation. Alertmanager uses Go’s text/template engine with additional helper functions. Alerting rules referenc...

Deploying a Maven Web Application to Tomcat 9 Using the Tomcat Manager

Tomcat 9 does not provide a dedicated Maven plugin. The Tomcat Manager interface, however, is backward-compatible, so the Tomcat 7 Maven Plugin can be used to deploy to Tomcat 9. This guide shows two...

Skipping Errors in MySQL Asynchronous Replication

When a replica halts because the SQL thread encounters an error, you can resume replication by skipping the problematic event(s). Two common approaches are available. Methods to Skip Errors 1) Skip a...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.