Java - Can a Client act simultaneously as a Server simultaneously or vice versa?

Well, I am working on a project where I need to write a TCP / IP client-server program. Well, actually it's pretty simple, but I was asked to do something that I had never done before.

I want to know if I can write a Java client application that sends commands to the server and receives a response from the server. In addition, the Server can send the same commands to the client and receive similar responses.

Simply put, I am asked to write an application in which the Client is simultaneously a server (not for him, but for the server to which he connects) and vice versa.

For example, if the Client requests the video stream of camera 01 to the server, the server sends a confirmation. Immediately after that, if the server wants, it can send a request to view camcorder 02 to the client, and the client will also respond accordingly.

Can this be done?!? If so, please, some tips will be great, as I'm not sure how to deal with this, possibly multithreading. If so, how?!? Some help will be great.

+4
source share
2 answers

You can develop a multi-threaded application on both sides. On the server, two streams will be there to receive packets, and the other to send packets. On the client side, you can do the same.

+2
source

Yes you can do it. In fact, Peer-to-Peer apps do just that. Instead of a client or server, you have a peer-to-peer connection that is both a client and a server. You will definitely have to use multithreading to get this to work.

+1
source

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


All Articles