Sei sulla pagina 1di 5

Setting Up a MATLAB TCPIP Server Session

The MATLAB server session creates a data set to be transferred out, opens a TCP/IP server socket and waits for the client to connect to it. hen the connection is established, the data is written out to the socket. Prepare the data we want to send over to the client MATLAB session. In this case our data is created b! a call to the membrane function.
data = membrane(1);

Let us list details of the data set we want to transfer. e will use this infor"ation later to set up so"e para"eters on the server socket and in the client.
s = whos('data') s = name: size: bytes: class: global: sparse: complex: nesting: persistent: 'data' [31 31] 7688 'double' 0 0 0 [1x1 struct] 0

#et the di"ensions of the data arra! we will be transferrin$.


s.size ans = 31 31

#et the nu"ber of b!tes of data we will be transferrin$.


s.bytes ans = 7688

%tart a TCP/IP server socket in MATLAB. B! settin$ the IP address to '0.0.0.0' the server socket will accept connections on the specified port &arbitraril! chosen to be ''((( in our case) fro" an! IP address. *ou can restrict the TCP/IP server socket to onl! accept inco"in$ connections fro" a specific IP address b! e+plicitl! specif!in$ the IP address. ,ote the new propert! NetworkRole.

tcpipSer er = tcpip('0.0.0.0'!""000!'NetworkRole'!'Ser er');

%et the #$tp$t%$&&erSize propert! to a value lar$e enou$h to hold the data. This is the first place where we use the output of the whos function, specificall! the value of s.bytes.
set(tcpipSer er!'#$tp$t%$&&erSize'!s.bytes);

-pen the server socket and wait indefinitel! for a connection. This line will cause MATLAB to wait until an inco"in$ connection is established.
&open(tcpipSer er);

%ince the MATLAB server code is runnin$ in a separate MATLAB session than the client, !ou "a! notice the %$sy status ne+t to the MATLAB %tart Button in the server session until the followin$ co""ands have been e+ecuted. *ou "a! stop the MATLAB server socket creation and break out of this bus! state b! usin$ the Control.C ke! co"bination to close the server socket. ,ote that once !ou close the server socket clients will no lon$er be able to connect to it until it has been re.opened. -nce the connection is "ade b! the client, write the data out and close the server.
&write(tcpipSer er!data(')!'do$ble'); &close(tcpipSer er);

Setting Up the MATLAB Client Session


The MATLAB server session is runnin$ on a co"puter with a known IP address or hostna"e. In our case, this is the address '1().0.0.1'. The second MATLAB session that acts as the client application creates a TCP/IP client, connects to the server and retrieves the data. -nce retrieved, the data will be visuali/ed in the client session. Create a MATLAB client connection to our MATLAB server socket. ,ote the value of the NetworkRole propert! on the client. Also note that the port nu"ber of the client "atches that selected for the server.
tcpip*lient = tcpip('1().0.0.1'!""000!'NetworkRole'!'*lient') !"#" $b%ect : !"#"&1'7(0(0(1 ++000 1'7(0(0(1 '-.' client closed o22

!ommunication )ettings *emote"ort: *emote,ost: erminator: /et0or1*ole: !ommunication )tate )tatus: *ecord)tatus:

*ead34rite )tate rans2er)tatus: 5ytes67ailable: 8alues*ecei7ed: 8alues)ent:

idle 0 0 0

%et the +np$t%$&&erSize propert! so we have sufficient roo" to hold the data that will be sent to us b! the server. The nu"ber 0122 is the nu"ber of b!tes in the data arra!. 3or "ore $eneral purpose code, !ou can para"etri/e this code b! usin$ the value in s.bytes instead of the hard. coded value of 0122.
set(tcpip*lient!'+np$t%$&&erSize'!),--);

I will define a lon$ value for the .imeo$t4 the waitin$ ti"e for an! read or write operation to co"plete. Ad5ust this value to ensure that an! data that is bein$ transferred to the client will be read back within the selected ti"eout.
set(tcpip*lient!'.imeo$t'!/0);

-pen a TCPIP connection to the server.


&open(tcpip*lient);

6ead the data that is bein$ written out on the server. ,ote that the data t!pes need to be "atched up on the client and server. The nu"ber of do$ble values to be read back fro" the server socket is 718. 3or "ore $eneral purpose code, !ou "a! para"etri/e the second ar$u"ent usin$ prod(s.size), for e+a"ple.
raw0ata = &read(tcpip*lient!1,1!'do$ble');

Close the connection to the server once we9ve retrieved the data.
&close(tcpip*lient);

6eshape the data arra! and plot it. *ou can para"etri/e this b! specif!in$ s.size as the input to reshape instead of the hard.coded arra! si/e &:8+:8).
reshaped0ata = reshape(raw0ata!/1!/1); s$r&(reshaped0ata);

Closing Notes
,ote that this functionalit! is "eant to be used behind a firewall or on a private network, and should be used in accordance with the license a$ree"ent as it relates to !our particular license option and activation t!pe. 3or "ore infor"ation on usin$ MATLAB TCP/IP server sockets refer to the docu"entation. e could see a nu"ber of uses of this functionalit!4 an e+a"ple bein$ usin$ MATLAB on an instru"ent used to $ather data. ;sin$ TCP/IP server sockets in MATLAB it is now possible to transfer the data out, without the need for files, to a re"ote co"puter for further anal!sis and visuali/ation in MATLAB.

Network Analysis and Visualization


Apply basic graph theory algorithms to Protein-Protein Interactions (PPI) and other gene networks; view network relationships using interactive maps, hierarchy plots, and pathways

Functions
graphallshortestpat Find all shortest paths in graph hs Find strongly or weakly connected components in graphconncomp graph graphisdag est !or cycles in directed graph graphisomorphism Find isomorphism between two graphs graphisspantree "etermine i! tree is spanning tree graphma#!low $alculate ma#imum !low in directed graph graphminspantree Find minimal spanning tree in graph graphpred%path $onvert predecessor indices to paths graphshortestpath &olve shortest path problem in graph graphtopoorder Per!orm topological sort o! directed acyclic graph graphtraverse raverse graph by !ollowing ad'acent nodes

Classes
biograph biograph ob'ect $reate biograph ob'ect "ata structure containing generic interconnected data used to implement directed graph

Potrebbero piacerti anche