Main Content

Transition Your Code to tcpserver Interface

The tcpip function, its object functions, and its properties will be removed. Use the tcpserver interface instead.

tcpip Interfacetcpserver InterfaceExample
tcpip, NetworkRole, and fopentcpserverCreate a TCP/IP Server
instrfind and instrfindalltcpserverfindFind Existing TCP/IP servers
fwritewriteWrite and Read
freadread
fprintfwritelineRead Terminated String
fscanfreadlineRead Terminated String
fgetlRead and Parse String Data
fgets
binblockwritewritebinblockWrite and Read Data with the Binary Block Protocol
binblockreadreadbinblock
flushinput and flushoutputflushFlush Data from Memory
TerminatorconfigureTerminatorSet Terminator
BytesAvailableFcnCount, BytesAvailableFcnMode, and BytesAvailableFcnconfigureCallbackSet Up Callback Function
BytesAvailableNumBytesAvailable 
LocalHostServerAddress 
RemoteHostClientAddress 
LocalPort and LocalPortModeServerPort 
RemotePort
ErrorFcnErrorOccurredFcn 
fcloseclear or deleteDisconnect TCP/IP Server

Removed Functionality

The ValuesReceived and ValuesSent properties will be removed. You can calculate the number of values sent using the NumBytesAvailable property and the data type of the data available. For example, if the NumBytesAvailable is 20 bytes of uint32 data, the number of values sent is five since each uint32 value is four bytes.

The readasync and stopasync functions and the ReadAsyncMode, TransferDelay, and TransferStatus properties will be removed. The updated interface reads data asynchronously.

The BytesToOutput, InputBufferSize, and OutputBufferSize properties will be removed. Buffer sizes are automatically managed and sized as needed.

The OutputEmptyFcn property will be removed. You can set callback functions using configureCallback in the updated interface, but not for this property.

The RecordDetail, RecordMode, RecordName, and RecordStatus properties will be removed.

The TimerFcn and TimerPeriod properties will be removed. Use timer instead.

The Name, Type, ObjectVisibility, Status, and Tag properties will be removed.

Find Existing TCP/IP servers

instrfind and instrfindall will be removed. Use tcpserverfind instead. (since R2024a)

Create a TCP/IP Server

These examples show how to create a TCP/IP server using the recommended functionality.

FunctionalityUse This Instead
t = tcpip("192.168.2.15",3030,"NetworkRole","server");
fopen(t)

This binds to host "0.0.0.0" (internally) and port 3030 and only accepts client connections coming from "192.168.2.15". MATLAB® is blocked until a client connection is established.

t = tcpserver("0.0.0.0",3030);

This binds to "0.0.0.0" and port 3030. "0.0.0.0" means that it accepts any incoming client connection requests on port 3030. MATLAB is not blocked.

The fopen function is not available in the updated interface. The object creation function tcpserver both creates and connects the object.

The fclose function is not available in the updated interface. The clear function disconnects the object when it removes the object from the workspace.

For more information, see tcpserver.

Write and Read

These examples show how to perform a binary write and read, and how to write and read nonterminated string data, using the recommended functionality.

FunctionalityUse This Instead
% t is a tcpip object
fwrite(t,1:5);
data = fread(t,5)
data =

     1
     2
     3
     4
     5
% t is a tcpserver object
write(t,1:5,"uint8")
data = read(t,5)
data =

     1     2     3     4     5
% t is a tcpip object
fwrite(t,"hello","char")
length = 5;
data = fread(t,length,"char")
data =

   104
   101
   108
   108
   111
data = char(data)'
data =

    'hello'
% t is a tcpserver object
write(t,"hello","string");
length = 5;
data = read(t,length,"string")
data =

    "hello"

For more information, see write or read.

Read Terminated String

These examples show how to write and read terminated string data using the recommended functionality.

FunctionalityUse This Instead
% t is a tcpip object
t.Terminator = "CR";
fprintf(t,"hello")
data = fscanf(t)
data =

    'hello
     '
% t is a tcpserver object
configureTerminator(t,"CR");
writeline(t,"hello");
data = readline(t)
data = 

    "hello"
% t is a tcpip object
t.Terminator = "CR";
fprintf(t,"hello")
data = fgetl(t)
data =

    'hello'

fgetl reads until the specified terminator is reached and then discards the terminator.

% t is a tcpip object
t.Terminator = "CR";
fprintf(t,"hello")
data = fgets(t)
data =

    'hello
     '

fgets reads until the specified terminator is reached and then returns the terminator.

For more information, see writeline or readline.

Read and Parse String Data

This example shows how to read and parse string data using the recommended functionality.

FunctionalityUse This Instead
% t is a tcpip object
data = scanstr(t,';')
data =

  3×1 cell array

    {'a'}
    {'b'}
    {'c'}
% t is a tcpserver object
data = readline(t)
data = 

    "a;b;c"
data = strsplit(data,";")
data = 

  1×3 string array

    "a"    "b"    "c"

For more information, see readline.

Write and Read Data with the Binary Block Protocol

This example shows how to write data with the IEEE standard binary block protocol using the recommended functionality.

FunctionalityUse This Instead
% t is a tcpip object
binblockwrite(t,1:5);
data = binblockread(t)
data =

     1
     2
     3
     4
     5
% t is a tcpserver object
writebinblock(t,1:5,"uint8");
data = readbinblock(t)
data =

     1     2     3     4     5

For more information, see writebinblock or readbinblock.

Flush Data from Memory

These examples show how to flush data from the buffer using the recommended functionality.

FunctionalityUse This Instead
% t is a tcpip object
flushinput(t)
% t is a tcpserver object
flush(t,"input")
% t is a tcpip object
flushoutput(t)
% t is a tcpserver object
flush(t,"output")
% t is a tcpip object
flushinput(t)
flushoutput(t)
% t is a tcpserver object
flush(t)

For more information, see flush.

Set Terminator

These examples show how to set the terminator using the recommended functionality.

FunctionalityUse This Instead
% t is a tcpip object
t.Terminator = "CR/LF";
% t is a tcpserver object
configureTerminator(t,"CR/LF")
% t is a tcpip object
t.Terminator = {"CR/LF" [10]};
% t is a tcpserver object
configureTerminator(t,"CR/LF",10)

For more information, see configureTerminator.

Set Up Callback Function

These examples show how to set up a callback function using the recommended functionality.

FunctionalityUse This Instead
% t is a tcpip object
t.BytesAvailableFcnCount = 5
t.BytesAvailableFcnMode = "byte"
t.BytesAvailableFcn = @mycallback

function mycallback(src,evt)
   data = fread(src,src.BytesAvailableFcnCount);
   disp(evt)
   disp(evt.Data)
end
    Type: 'BytesAvailable'
    Data: [1×1 struct]

    AbsTime: [2019 12 21 16 35 9.7032]
% t is a tcpserver object
configureCallback(t,"byte",5,@mycallback);

function mycallback(src,evt)
   data = read(src,src.BytesAvailableFcnCount);
   disp(evt)
end
  ByteAvailableInfo with properties:

    BytesAvailableFcnCount: 5
                   AbsTime: 21-Dec-2019 12:23:01
% t is a tcpip object
t.Terminator = "CR"
t.BytesAvailableFcnMode = "terminator"
t.BytesAvailableFcn = @mycallback

function mycallback(src,evt)
   data = fscanf(src);
   disp(evt)
   disp(evt.Data)
end
    Type: 'BytesAvailable'
    Data: [1×1 struct]

    AbsTime: [2019 12 21 16 35 9.7032]
% t is a tcpserver object
configureTerminator(t,"CR")
configureCallback(t,"terminator",@mycallback);

function mycallback(src,evt)
   data = readline(src);
   disp(evt)
end
  TerminatorAvailableInfo with properties:

                   AbsTime: 21-Dec-2019 12:23:01

For more information, see configureCallback.

Disconnect TCP/IP Server

The fclose function is not available in the updated interface. To disconnect a TCP/IP server, use clear or delete instead, depending upon whether you are working in a single workspace or multiple workspaces. For details, see the following examples on the tcpserver reference page:

See Also

Related Topics