How to address server and multiple clients communicate using tcpip function?

22 views (last 30 days)
t = tcpip(0.0.0.0,3000,'NetworkRole','server');
fopent(t) --server is open for connection
t = tcpip('localhost',3000,'NetworkRole','client');
fopent(t) --client connects to server from other instance of matlab
But, only one client is able to connect, how can we make multiple instances to connect with server?

Answers (2)

Walter Roberson
Walter Roberson on 26 Mar 2018
You would need to call
t = tcpip(0.0.0.0,3000,'NetworkRole','server');
multiple times, once for each client.
Unfortunately there does not appear to be a timeout for the fopen() property of tcpip objects, and the fopen() is blocking, so you do not appear to be easily use the strategy of running a new tcpip/fopen with timeout after a successful fopen() in order to wait for potential new clients.
This suggests to me that the approach would have to be something along the lines of using parfeval() to create a future as the future can afford to sit around and wait while everything else goes on. I am not certain but this might limit the number of clients according to the size of your parpool.
To deal with communications between the future and the main work to be done, you might want to use parallel.pool.PollableDataQueue, using one in each direction for each future (each of them is unidirectional.)
I do not say that these are good ways to handle multiple clients, but keep in mind that MATLAB itself is single threaded.
... I suspect you might find it a lot smoother to use Java to create the sockets.

Kiran Satish
Kiran Satish on 6 Dec 2019
I have a similar question, as per my understanding, a socket opened as a server, should remain active until it is closed no matter how many times a client connects and disconnects to it. Isnt it not the case with tcpip objects defined as server in matlab? For me, i have to reopen the server port everytime I disconnect a client (another tcpip object defined as client on another system). Any suggestions why it is like that, I would like to keep server object active as long as I keep it open irrespective of client connect & disconnects. I am tlaking about only one client here.
thanks
  3 Comments
Kiran Satish
Kiran Satish on 6 Dec 2019
Thanks Walter for your reply. I know one can use UDP to have a open connection all the time, I have a version of my applications based on UDP. For now, the requirement is for TCPIP, will check on your suggestions on SO_REUSE etc.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!