Request-Reply With Java Sockets
I would like to know how to handle request reply properly to a single socket connection.
Here 's my scenario, i am trying to send messages to a socket and i am using Sockets to establish connection to it
Here's a sample scenario,
Thread 1 --> Establish Connection --> Write Message 1 --> Receive Response 2
Thread 2 --> Establish Connection --> Write Message 2 --> Receive Response 1
What happens is, the connections are exchanging replies with each other.
Socket 1 tends to read the replies coming from thread 2 and Socket 2 receives replies from thread 1.
One thing I could do is create a separate receiver thread running rather than waiting for the reply coming from the socket.
How do I ensure that i only get the reply from which I sent, One thing i could do is to synchronize socket access but this would result to slower performance
Here is the scenario that i wish to have
Thread 1 --> Establish Connection --> Write Message 1 --> Receive Response 1
Thread 2 --> Establish Connection --> Write Message 2 --> Receive Response 2
Need your help