Java Network Programming. Question about sockets

I have a server and there are 2 clients connecting to it via TCP. These clients constantly send information to the server. It is a proxy server and transmits messages received from the client. But he must forward the messages one by one. that is, a message from client A, then a message from client B and again A, then B, etc. I can achieve this by checking where the message comes from, and then relaying the messages one at a time and ignoring consecutive messages from the same client.

However, I also do not want the server to start if any of the clients disconnects or does not send messages. If this happens, the proxy server will continue to wait forever for messages from the client, which is now disconnected (or for some reason does not send a message). In this case, I would like the server to send a message from the only connected client.

Instead, I think something like this is possible. If I receive 2 consecutive messages from the same client, I would like to check if another client has been read to send me messages. My question is whether it is possible to check from another client socket if there is a message that is buffered and ready to be sent. In this case, this case may ignore the serial message from the same client and instead send a message from another client first. (that I checked.)

Is it possible? I hope I clearly asked a question.

thanks

+3
source share
8 answers

I read the problem as follows:

You have:

1 server

2 clients

The server receives messages from clients 1 and 2 and forwards them.

- , , , . , , "", .

(client1queue client2queue).

, , . client1Socket → client1queue client2Socket → client2queue

, , , client1queue client2queue.

" ", , "" . , . , , . , , X , , , , .

+2

, - (, , , - , ) setSoTimeout(); , A, () B, SocketTimeoutException, .

, SocketTimeoutExceptions, , .

+1

, : , , , Java Socket . , - , , , - .

, , , : Socket.setSoTimeout(int), "" . , , - , , -1, , .

    Socket clientSocket=theServerSocket.accept();
    clientSocket.setSoTimeout(1);
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

while(serverIsRunning){
       try{
           if(bufferedReader.read()==-1){
                  logger.info("The server has disconnected!");
                  //Do whatever clean up you need, ect
           }
       }
       catch (SocketTimeoutException e){
           //the server is still running, yay?!
       }
}
+1

, , , . :

  • , , ,
  • ,
  • , : ; ( )
  • , , " ", " ".

, - , , , - , .

, , , , . , : " ". , , - - , , . "", . ? , - , , "" ...?

0

, , - : 2 A, , B. () inputStream , - , . , B. Else A ( ).

.

0

A , B , A? , , , . , , B?

, , "" . , , . , SynchronousQueue, . put , take SynchronousQueue.

take SynchronousQueue. , put.

, SynchronousQueue, . , .

0

javadoc available(). , .

0

:

1) .

2) .

, . , io SocketChannel Selector.

Remember that TCP / IP streams can provide several or only partial messages in each read operation, depending on the network and socket settings, so you need to take care of this.

The second problem can be solved by maintaining a queue for each client, or perhaps by using a priority queue and attaching a priority to each message depending on the activity of the clients.

0
source

Source: https://habr.com/ru/post/1706527/


All Articles