简体   繁体   中英

How to make 2 server threads communicate with 2 client threads?

I basically want to achieve this:

ServerThreadA                         ClientThreadA
             \                       /
               --->>>--->>>--->>>---
                      SOCKET
               --->>>--->>>--->>>---
             /                       \
ServerThreadB                         ClientThreadB  

What's happening here is that I have 2 threads on the server sending data to 2 respective threads on the client side. To be more specific I'd need to use .writeUTF(String) from DataOutputStream on the server and .readUTF() from DataInputStream on the client.

When ServerThreadA writes something to the stream only ClientThreadA shall read it, and not ClientThreadB. In the same way I don't want ClientThreadB to read what ServerThreadA has written.

Is there any way I can achieve this?

EDIT:
I think I might have explained poorly: I already know how to make a server that can accept multiple connections and give each one of them a thread to work on, what I'm trying to is to have multiple communications going on in the same direction and in the same client. So let's say the server connects to a client, a thread is created representing the connection but what I want to do is read from 2 different channels, if you will, transfering different types of data. For example I might want the client to send two strings simultaneously and the server to read them at the same time

Change protocol so that before each String a number should be written: 0 if the message is for ClientThreadA, and 1 if message is for ClientThreadB.

At the server side, start a ClientThread0 which should read all the input data from the socket. It reads a number and then a string in a loop. Then passes the string to appropriate thread depending on the number. To pass messages from the ClientThread0 to ClientThreadA/B, BlockingQueue<String> s are used. ClientThread0 calls to queue.put(string) , and ClientThreadA/B call to queue.take() .

You can use the ServerSocket for server where the sever accepts the port number which are passed or called by the client.

Both use the same port number for communicating.

Many number of clients can call the server. As the client request (on that port number) server accepts client request it means client joins to the server....

I think your understanding of how servers should work is wrong. That picture should very much look like:

ServerThreadA --->>>--->>>--->>>--- ClientThreadA

ServerThreadB --->>>--->>>--->>>--- ClientThreadB

The point is: the server code waits on one server socket; and when a new client comes in and accept() gives you a "dedicated" socket to talk to that client.

In other words: no matter how many client threads connect to your server; and no matter how many server threads take care with them; there is always one specific socket per client.

There is no sharing of sockets; thus there is no need to worry about messages that could go to more than one client!

And when you thought about "sharing" sockets to allow one side to send messages to all peers on the other side, then you need to do using different ways!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM