Create a TCP Server.
Attention: Available only with Twin Activate commercial edition.
Syntax
sobj = tcpserver(address,port)
sobj = tcpserver(address,port,[Name, value])
Inputs
- address
- server address tag
- Type: string
- port
- Numeric port number.
- Type: scalar
- Name, value
- Name
- Type: string
- Valid options are:
- Name
- Value
- Timeout
- Numeric value in seconds, time given for read operation to finish. Negative
value to wait forever.
- Type: double
- UserData
- User data to store in server object.
- Type: scalar | vector | matrix | struct | cell | Boolean | string
- ByteOrder
- Valid values are 'network' and 'host'
(default).
- Type: string
Outputs
- sobj
- TCPIP server object.
- Type: TCP client
- property, values
- Type: string
- Valid options are:
- Property Name
- Description/Values
- UserData
- User data.
- Type: scalar | vector | matrix | struct | cell | Boolean | string
- ByteOrder
- Byte order used for read/write.Valid values are 'network' and
'host' (default).
- Type: string
- Timeout
- Numeric value in seconds, time given for read operation to finish. Negative
value to wait forever.
- Type: double
- ClientSock
- Client socket. Readonly property.
- Type: scalar
- ServerSock
- Server socket. Readonly property.
- Type: scalar
- Name
- Name of the server object. Readonly property.
- Type: string
- Type
- Value is 'tcpserver'. Readonly property.
- Type: string
- Connected
- Client connection is accepted on accessing this property.
- Type: scalar
- ClientPort
- Client port number.Readonly property.
- Type: scalar
- ClientAddress
- Client ip address.Readonly property.
- Type: string
- ServerPort
- Server port number.Readonly property.
- Type: scalar
- ServerAddress
- Server ip address.Readonly property.
- Type: string
- Status
- Connection status.Readonly property.
- Type: string
- NumBytesAvailable
- Number of bytes available to read. Readonly property.
- Type: scalar
- NumBytesWritten
- Number of bytes written by write function call. Readonly property.
- Type: scalar
- Terminator
- Termination caharacter used read/write string data. Readonly property.
- Type: scalar | string
- WriteDataType
- Data type used to write data. Readonly property.
- Type: string
- ReadDataType
- Data type used to read data. Readonly property.
- Type: string
- Function Name, Syntax
- Type: string
- Valid options are:
- Function Name
- Description/Syntax/Arguments
- read
- reads data.
- data=read() data=read(NumberOfBytes) data=read(NumberOfBytes,DataType)
NumberOfBytes is a scalar. Supported "DataType" values are 'string', 'double'
,'int8','int16','int32','int64','uint8','uint16','uint32','uint64'.
- write
- writes data.
- data=write(Data) data=write(Data,DataType) Supported type for "Data" is string,
real number, real matrix. Supported "DataType" values are 'string', 'double'
,'int8','int16','int32','int64','uint8','uint16','uint32','uint64'.
- close
- Closes connection.
Examples
read/write string data
sobj=tcpserver('127.0.0.1',9999);
cobj=tcpclient('127.0.0.1',9999);
sobj.Connected;
cobj.write('Hello Server');
sobj.read()
sobj.write('Hello Client');
cobj.read()
sobj.close();
cobj.close();
ans = Hello Server
ans = Hello Client
read/write numeric data
sobj=tcpserver('127.0.0.1',9999);
cobj=tcpclient('127.0.0.1',9999);
sobj.Connected;
data=[1,2,3,4];
cobj.write(data,'double');
clientdata=sobj.read(4,'double')
data=99;
sobj.write(data,'int32');
serverdata=cobj.read(1,'int32')
sobj.close();
cobj.close();
clientdata = [Matrix] 1 x 4
1 2 3 4
serverdata = 99