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

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)

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.
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

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.
No, that is not the case for tcp sockets for which a bind() operation has taken place: such sockets are shut down when the client disconnects.
To get a socket that can accept tcp reconnections is the same as having a socket that can accept multiple connections from different locations. My memory for a few decades ago is that the process involves setting a SO_REUSE flag on the socket, and calling accept() on the socket when a connection comes through, and then fork()'ing a new process or creating a new thread to handle the file descriptor, and inside the new process or thread, you can call bind() . I might well have some of the details wrong after all of this time.
Remember that going silent on a TCP connection is not the same as disconnecting; if you just stop sending things for a while then the socket can continue active (though in practice there is often a timeout in the works somewhere, especially if keepalive is configured.) Disconnecting in TCP is sending a FIN packet to signal that the connection should be destroyed, that all buffers associated with it should be released, that all negotiated packet options and packet size information should be forgotten, that the sequence numbers should be forgotten, and so on. "Reconnecting" in TCP is exactly the same thing as forming a new connection.
If you are thinking that "disconnecting" is going to occur often, you should consider a couple of possibilities:
  • udp
  • use a protocol such as ThinkSpeak
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.
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.

Sign in to comment.

Asked:

on 25 Mar 2018

Commented:

on 6 Dec 2019

Community Treasure Hunt

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

Start Hunting!