TCP/IP Communication with a Remote Host

These are the minimum steps required to communicate with a remote host using TCP/IP.

In this example, you read a page from the RFC Editor Web site using a TCP/IP object.

  1. Create and configure an instrument object — First you create a TCP/IP object in the MATLAB® workspace. Port 80 is the standard port for Web servers.

    t = tcpip('', 80);

    By default, the TCP/IP object has an InputBufferSize of 512, which means it can only read 512 bytes at a time. The web page data is much greater than 512 bytes, so you need to set a larger value for this property.

    t.InputBufferSize = 30000;

    To successfully send a command to the web page, set the Terminator property of the TCP/IP object.

    t.Terminator = {"LF", "CR/LF"};
  2. Connect the object — Next, you open the connection to the server. If the server is not present or is not accepting connections you would get an error here.

  3. Write and read data — You can now communicate with the server using the functions fprintf, fscanf, fwrite, and fread.

    To ask a Web server to send a Web page, you use the GET command. You can ask for a text file from the RFC Editor Web site using 'GET (path/filename)'.

    fprintf(t, 'GET /rfc/rfc793.txt');

    The server receives the command and sends back the Web page. You can see if any data was sent back by looking at the BytesAvailable property of the object.


    Now you can start to read the Web page data. By default, fscanf reads one line at a time. You can read lines of data until the BytesAvailable value is 0. Note that you will not see a rendered web page; the HTML file data will scroll by on the screen.

    while (t.BytesAvailable > 0)
        A = fscanf(t)
  4. Disconnect and clean up — If you want to do more communication, you can continue to read and write data here. If you are done with the object, close it and delete it.

    clear t

Server Drops the Connection

This example shows what happens when a TCP/IP object loses its connection with a remote server. The server is a Sony/Tektronix® AWG520 Arbitrary Waveform Generator (AWG). Its address is and its port is 4000. The AWG's host IP address is and is user configurable in the instrument. The associated host name is given by your network administrator. The port number is fixed and is found in the instrument's documentation.

The AWG can drop the connection because it is taken off line, it is powered down, and so on:

  1. Create an instrument object — Create a TCP/IP object for the AWG.

    t = tcpip('', 4000);
  2. Connect to the instrument — Connect to the remote instrument.

  3. Write and read data — Write a command to the instrument and read back the result.

    ans =
    SONY/TEK,AWG520,0,SCPI:95.0 OS:2.0 USR:2.0

    Assume that the server drops the connection. If you attempt to read from the instrument, a timeout occurs and a warning is displayed.

    Warning: A timeout occurred before the Terminator was reached.
    (Type "warning off instrument:fscanf:unsuccessfulRead" to 
    suppress this warning.)
    ans =

    At this point, the object and the instrument are still connected.

    ans =

    If you attempt to write to the instrument again, an error message is returned and the connection is automatically closed.

    ??? Error using ==> fprintf
    Connection closed by RemoteHost. Use FOPEN to connect to 

    Note that if the TCP/IP object is connected to the local host, the warning message is not displayed. Instead, the error message is displayed following the next read operation after the connection is dropped.

  4. Disconnect and clean up — When you no longer need t, you should disconnect it from the host, and remove it from memory and from the MATLAB workspace.

    clear t