How to address server and multiple clients communicate using tcpip function?
7 views (last 30 days)
Show older comments
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?
0 Comments
Answers (2)
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
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
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.
Walter Roberson
on 6 Dec 2019
I don't think you can set these using MATLAB's tcpip() . They are more things to know if you use Java to construct the sockets.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!