Main Content

read

Read data sent to TCP/IP server

Since R2021a

    Description

    data = read(t,count) reads the number of values specified by count sent to the TCP/IP server t from the client connected to it and returns the data as a row or column vector of doubles or text. The function suspends MATLAB® execution until the specified number of values is read or a timeout occurs.

    example

    data = read(t,count,datatype) reads the number of values specified by count in the form specified by datatype and returns the data. The datatype argument is a character vector of a standard MATLAB data type. For all numeric datatype types, data is a row vector of double values. For the text type datatype values of "char" or "string", data is of the specified type.

    example

    Examples

    collapse all

    Create a TCP/IP server that listens for a client connection request at the specified port and IP address. Then, read data sent to the server from the connected client.

    Create a TCP/IP server that listens for connections at localhost and port 4000.

    server = tcpserver("localhost",4000)
    server = 
      TCPServer with properties:
    
            ServerAddress: "127.0.0.1"
               ServerPort: 4000
                Connected: 0
            ClientAddress: ""
               ClientPort: []
        NumBytesAvailable: 0
    
      Show all properties, functions
    
    

    Create a TCP/IP client to connect to your server object using tcpclient. You must specify the same IP address and port number you use to create server.

    client = tcpclient("localhost",4000)
    client = 
      tcpclient with properties:
    
                  Address: 'localhost'
                     Port: 4000
        NumBytesAvailable: 0
    
      Show all properties, functions
    
    

    Display the values of the Connected, ClientAddress, and ClientPort properties for server.

    server
    server = 
      TCPServer with properties:
    
            ServerAddress: "127.0.0.1"
               ServerPort: 4000
                Connected: 1
            ClientAddress: "127.0.0.1"
               ClientPort: 59357
        NumBytesAvailable: 0
    
      Show all properties, functions
    
    

    The output shows that server successfully accepts a request from client and that client establishes a connection to server.

    Write data to the TCP/IP client. Since the client is connected to the server, this data is available in the server. Read the data using the server object.

    write(client,[4,8,15,16,23,42],"uint8")
    read(server,server.NumBytesAvailable)
    ans = 1×6
    
         4     8    15    16    23    42
    
    

    Create a TCP/IP server that listens for a client connection request at the specified port and IP address. Then read data sent to the server from the connected client.

    Create a TCP/IP server that listens for connections at localhost and port 4000.

    server = tcpserver("localhost",4000)
    server = 
      TCPServer with properties:
    
            ServerAddress: "127.0.0.1"
               ServerPort: 4000
                Connected: 0
            ClientAddress: ""
               ClientPort: []
        NumBytesAvailable: 0
    
      Show all properties, functions
    
    

    Create a TCP/IP client to connect to your server object using tcpclient. You must specify the same IP address and port number you use to create server.

    client = tcpclient("localhost",4000)
    client = 
      tcpclient with properties:
    
                  Address: 'localhost'
                     Port: 4000
        NumBytesAvailable: 0
    
      Show all properties, functions
    
    

    Display the values of the Connected, ClientAddress, and ClientPort properties for server.

    server
    server = 
      TCPServer with properties:
    
            ServerAddress: "127.0.0.1"
               ServerPort: 4000
                Connected: 1
            ClientAddress: "127.0.0.1"
               ClientPort: 65440
        NumBytesAvailable: 0
    
      Show all properties, functions
    
    

    The output shows that server successfully accepts a request from client and that client establishes a connection to server.

    Write data to the TCP/IP client. Since the client is connected to the server, this data is available in the server. Read the first five values of string data using the server object.

    write(client,"helloworld","string")
    read(server,5,"string")
    ans = 
    "hello"
    

    If you read five more values, you receive the remaining string data.

    read(server,5,"string")
    ans = 
    "world"
    

    Input Arguments

    collapse all

    TCP/IP server, specified as a tcpserver object.

    Example: read(t,5) reads data sent to the TCP/IP server t from the client connected to it.

    Number of values to read, specified as a positive integer value. If count is greater than the NumBytesAvailable property of t, the function suspends MATLAB execution and waits until it reads the specified amount of data or a timeout occurs.

    Example: read(device,2) reads two values of uint8 data.

    Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

    Size and format of each value, specified as a character vector or string. datatype determines the number of bytes to read for each value and the interpretation of those bytes as a MATLAB data type.

    Example: read(t,1,"uint16") reads one value of uint16 data. Each uint16 value is two bytes.

    Data Types: char | string

    Version History

    Introduced in R2021a