communicate using tcp/ip server sockets
about server sockets
support for server sockets is available using the function. this support is for a single remote connection. you can use this connection to communicate between a client and matlab® or between two instances of matlab.
for example, you might collect data such as a waveform into one instance of matlab and then transfer it to another instance of matlab.
note
the use of the server socket on either the client or server side should be done in accordance with the license agreement as it relates to your particular license option and activation type. if you have questions, you should consult with the administrator for your license or your legal department.
this is intended for use behind a firewall on a private network.
communicate between two instances of matlab
the following example shows how to connect two matlab sessions on the same computer, showing the example code for each
session. to use two different computers, replace "localhost"
with
the ip address of the server in the code for session 2. using 0.0.0.0 as the ip
address means that the server will accept the first machine that tries to connect.
to restrict the connections that will be accepted, replace
"0.0.0.0"
with the address of the client in the code for
session 1.
session 1: matlab server
accept a connection from any machine on port 30000.
server = tcpserver("0.0.0.0",30000)
server = tcpserver with properties: serveraddress: "0.0.0.0" serverport: 30000 connected: 0 clientaddress: "" clientport: [] numbytesavailable: 0 show all properties, functions
session 2: matlab client
this code is running on a second instance of matlab.
create a client interface that connects to the server.
client = tcpclient("localhost",30000)
client = tcpclient with properties: address: 'localhost' port: 30000 numbytesavailable: 0 show all properties, functions
create a waveform and visualize it.
data = sin(1:64); plot(data);
write the waveform to the client. since the client is connected to the server, this data is available in the server session.
write(client,data,"double")
session 1: matlab server
read the waveform and confirm it visually by plotting it.
data = read(server,server.numbytesavailable,"double");
plot(data);
see also
|