Socket AT Commands

Note

  • Socket AT commands are disabled by default.

  • Refer to Configuring SDK (menuconfig) to enter CONFIG AT CMD configuration, and check Enable Socket to enable the Socket AT commands.

AT+SKTCFG

Function Description

Configure socket TCP/UDP/SSL global option parameters.

Command Format

AT+SKTCFG=[<so_sndtimeo>],[<so_rcvtimeo>],[<tcp_nodelay>],[<so_keepalive>],[<tcp_keepidle>],[<tcp_keepintvl>],[<tcp_keepcnt>]

Response Format

Success Response:

OK

Error Response:

ERROR: <error_no>

Parameters

<so_sndtimeo>:

Set SO_SNDTIMEO option to enable socket send operate to return within <so_sndtimeo> timeout, regardless of success or failure.

  • Unit: millisecond

  • Non-negative integer

  • Default: 0

<so_rcvtimeo>:

Set SO_RCVTIMEO option to enable socket receive operate to return within <so_rcvtimeo> timeout, regardless of success or failure.

  • Unit: millisecond

  • Non-negative integer

  • Default: 0

<tcp_nodelay>:

Set the TCP_NODELAY option for the socket to enable/disable the Nagle’s algorithm in TCP connections.

  • Default: 0 (enable the Nagle’s algorithm)

  • 0: disable TCP_NODELAY, 1: enable TCP_NODELAY

<so_keepalive>:

Set the SO_KEEPALIVE option for the socket to enable/disable the keepalive feature in TCP connections.

  • Default: 0

  • 0: disable SO_KEEPALIVE, 1: enable SO_KEEPALIVE

<tcp_keepidle>:

Set TCP_KEEPIDLE option to configure the idle time before the first keepalive probe is sent to check if the connection is still active.

  • Unit: second

  • Range: [0,7200]; Positive integer

  • Default: 0

<tcp_keepintvl>:

Set TCP_KEEPINTVL option to configure the interval between successive keepalive probes if no response is received

  • Unit: second

  • Range: [0,75]; Positive integer

  • Default: 0

<tcp_keepcnt>:

Set TCP_KEEPCNT option to determines how many probes are sent without a response before declaring the connection dead

  • Range: [0,10]; Positive integer

  • Default: 0

Error Codes

  • 1: Parameter error

Examples

Configure global option parameters:

AT+SKTCFG=2000,2000,,1,60,10,3
OK

AT+SKTQUERY

Function Description

Query global socket option configuration.

Command Format

AT+SKTQUERY

Response Format

Standard Response:

Global SOCKET configuration:
so_sndtimeo: <so_sndtimeo>
so_rcvtimeo: <so_rcvtimeo>
tcp_nodelay: <tcp_nodelay>
so_keepalive: <so_keepalive>
tcp_keepidle: <tcp_keepidle>
tcp_keepintvl: <tcp_keepintvl>
tcp_keepcnt: <tcp_keepcnt>

OK

Error Codes

  • 1: No need input Parameter

Example

Query the default configuration value:

AT+SKTQUERY

Global SOCKET configuration:
so_sndtimeo: 0
so_rcvtimeo: 0
tcp_nodelay: 0
so_keepalive: 0
tcp_keepidle: 0
tcp_keepintvl: 0
tcp_keepcnt: 0

OK

AT+SKTSERVER

Function Description

Configure TCP/UDP/SSL server listening service.

Command Format

AT+SKTSERVER=<link_id>,<conn_type>[,<cert_index>],<src_port>,<auto_rcv>

Response Format

Success Response:

OK

Error Response:

ERROR: <error_no>

Parameters

<link_id>:

Link ID specified by user

  • Range: [0, MEMP_NUM_NETCONN-1], currently MEMP_NUM_NETCONN=20 for lwip configuration

<conn_type>:

Connection type

  • 0: Socket server over UDP

  • 1: Socket server over TCP

  • 2: Socket server over TLS (provides server certificate for client verification)

  • 3: Socket server over TLS (provides server certificate for client verification and uses CA certificate to verify client certificate)

<cert_index>:

Security certificate suite index, refer to AT Security Certificates

  • Range: [1-10]

  • Only required when <conn_type> is 2 or 3

<src_port>:

The local port that the server to be created listens on

  • Range: [1,65535]

<auto_rcv>:

Auto receive flag

  • 0: don’t enable

  • 1: enable

Error Codes

  • 1: Parameter error

  • 2: Failed to create TCP server

  • 3: Failed to create the task that auto-receives the TCP client and TCP data

  • 4: Failed to create UDP server

  • 5: Failed to create the task that auto-receives UDP client and its data

  • 6: Failed to get SERVER_CERT/SERVER_KEY/SERVER_CA from flash

  • 7: Memory allocation failed

  • 8: mbedtls_ssl_config_defaults() failed

  • 9: mbedtls_x509_crt_parse() failed

  • 10: mbedtls_ssl_conf_own_cert() failed

  • 11: mbedtls_net_bind() failed

  • 12: Failed to create the task that auto-receives the TLS client and TLS data

Examples

Create a TCP server with auto-receive on link_id = 1, port = 1234:

AT+SKTSERVER=1,1,,1234,1
OK

Create a UDP server without auto-receive on link_id = 2, port = 4321:

AT+SKTSERVER=2,0,,4321,0
OK

Create a TLS server(provides server certificate for client verification and uses CA certificate to verify client certificate, cert_index = 1) with auto-receive on link_id = 0, port = 4433:

AT+SKTSERVER=0,3,1,4433,1
OK

If a client connects to this TLS server, the log window will pop up unsolicited message:

[$][SKT][EVENT]: A client[link_id:1,seed,tls,dst_address:192.168.0.103,dst_port:49946] connected to server[link_id:0]

AT+SKTCLIENT

Function Description

Establish TCP/UDP/SSL client connection.

Command Format

AT+SKTCLIENT=<link_id>,<conn_type>[,<cert_index>],<dst_host>,<dst_port>[,<src_port>],<auto_rcv>

Response Format

Success Response:

OK

Error Response:

ERROR: <error_no>

Parameters

<link_id>:

Link ID specified by user

  • Range: [0, MEMP_NUM_NETCONN-1], currently MEMP_NUM_NETCONN=20 for lwip configuration

<conn_type>:

Connection type

  • 0: Socket client over UDP

  • 1: Socket client over TCP

  • 2: Socket client over TLS (no certificate verification)

  • 3: Socket client over TLS (verify server certificate with CA certificate)

  • 4: Socket client over TLS (provide client certificate to server for verification)

  • 5: Socket client over TLS (mutual authentication, both 3 and 4)

<cert_index>:

Security certificate suite index, refer to AT Security Certificates

  • Range: [1-10]

  • Only required when <conn_type> is 3, 4, or 5

<dst_host>:

Remote server address

  • Supports IPv4 address or domain name (e.g., 192.168.1.100 or www.example.com)

  • Domain name maximum length: 255 characters

<dst_port>:

Remote server port

  • Range: [1,65535]

<src_port>:

The local port binded by UDP client (required for UDP only)

  • Range: [1,65535]

<auto_rcv>:

Auto receive flag

  • 0: don’t enable

  • 1: enable

Error Codes

  • 1: Parameter error

  • 2: Failed to create TCP client

  • 3: Failed to create the task that auto-receives TCP data as TCP client

  • 4: Failed to create UDP client

  • 5: Failed to create the task that auto-receives UDP data as UDP client

  • 6: Failed to get CLIENT_CERT/CLIENT_KEY/CLIENT_CA from flash

  • 7: Memory allocation failed

  • 8: mbedtls_ssl_config_defaults() failed

  • 9: mbedtls_x509_crt_parse() failed

  • 10: mbedtls_ssl_conf_own_cert() failed

  • 11: mbedtls_ssl_setup() failed

  • 12: inet_ntoa_r() failed

  • 13: mbedtls_net_connect() failed

  • 14: mbedtls_ssl_handshake() failed

  • 15: Failed to create the task that auto-receives TLS data as TLS client

Examples

Connect to TCP server(IPv4 address: 192.168.0.100, port: 6666) with auto-receive on link_id = 1:

AT+SKTCLIENT=1,1,,192.168.0.100,6666,,1
OK

Create UDP client(port: 12345) connect to UDP server(IPv4 address: 192.168.1.10, port: 6666) with auto-receive on link_id = 0:

AT+SKTCLIENT=0,0,,192.168.1.10,6666,12345,1
OK

Connect to TLS server(IPv4 address: 192.168.1.10, port: 4433) with auto-receive on link_id = 5, no certificate verification:

AT+SKTCLIENT=5,2,,192.168.1.10,4433,,1
OK

AT+SKTSEND

Function Description

Send data through specified connection <link_id>.

Command Format

AT+SKTSEND=<link_id>,<data_size>[,<dst_ip>,<dst_port>],<data>

Response Format

Success Response:

OK

Error Response:

ERROR: <error_no>

Parameters

<link_id>:

Link ID created successfully

  • Range: [0, MEMP_NUM_NETCONN-1], currently MEMP_NUM_NETCONN=20 for lwip configuration

<data_size>:

The number of data bytes that user want to send

  • Maximum value: decided by UART_LOG_CMD_BUFLEN - the number of characters before the <data> field, the maximum UART_LOG_CMD_BUFLEN is 2000, refer to Configuring SDK (menuconfig) to enter CONFIG AT CMD configuration and check Enable Longer CMD, refer to Command Length to check command length

<dst_ip>:

Destination IPv4 address (required for UDP server only)

  • e.g., 192.168.1.100

<dst_port>:

Destination port (required for UDP server only)

  • Range: [1,65535]

<data>:

The actual data to be send, length is specified by <data_size>

Error Codes

  • 1: Parameter error

  • 2: Failed to send data

Examples

TCP client(link_id = 0) send data to TCP server:

AT+SKTSEND=0,6,,,Hello!
OK

UDP server(link_id = 3) send data to UDP client(IPv4 address: 192.168.1.101, port: 61409):

AT+SKTSEND=3,10,192.168.1.101,61409,Lwip_Test!
OK

TLS server(link_id = 1) send data to TLS client(seed link_id = 2):

AT+SKTSEND=2,5,,,12345
OK

AT+SKTREAD

Function Description

Read buffered data from specified connection <link_id>.

Command Format

AT+SKTREAD=<link_id>,<data_size>

Response Format

Success Response:

+SKTREAD:<link_id>,<actual_recv_size>[,<udp_dstip>,<udp_dstport>],<data>
OK

Error Response:

ERROR: <error_no>

Parameters

<link_id>:

Link ID created successfully

  • Range: [0, MEMP_NUM_NETCONN-1], currently MEMP_NUM_NETCONN=20 for lwip configuration

<data_size>:

The number of data bytes that user want to read

<actual_recv_size>:

The actual number of bytes read

<udp_dstip>:

Destination UDP client/server IPv4 address (used for UDP only)

  • e.g., 192.168.1.100

<udp_dstport>:

Destination UDP client/server port (used for UDP only)

  • Range: [1,65535]

<data>:

The actual data to be read, length is decided by <actual_recv_size>

Error Codes

  • 1: Parameter error

  • 2: Memory allocation failed

  • 3: select() failed

  • 4: Failed to read data

Examples

TCP server (link_id = 0) read data from TCP client (seed link_id = 1):

AT+SKTREAD=1,10
+SKTREAD:1,6,Hello!
OK

UDP server (link_id = 3) read data from UDP client (IPv4 address: 192.168.1.101, port: 61409):

AT+SKTREAD=3,10
+SKTREAD:3,5,192.168.1.101,61409,12345
OK

TLS client (link_id = 2) read data from TLS server:

AT+SKTREAD=2,10
+SKTREAD:2,3,ABC
OK

AT+SKTSENDRAW

Function Description

Send data through specified connection <link_id> in Transparent Transmission mode. Refer to Configuring SDK (menuconfig) to enter CONFIG AT CMD configuration and select Host Control Mode, and then refer to the introduction of Transparent Transmission (TT) Mode.

Note

This command is only supported in Host Control Mode.

Command Format

AT+SKTSENDRAW=<link_id>,<data_size>[,<dst_ip>,<dst_port>]

Response Format

Success Response:

>>>
>>>
<data>
OK

Error Response:

ERROR: <error_no>

Parameters

<link_id>:

Link ID created successfully

  • Range: [0, MEMP_NUM_NETCONN-1], currently MEMP_NUM_NETCONN=20 for lwip configuration

<data_size>:

The number of data bytes that user want to send

  • Arbitrarily user-specified data size

<dst_ip>:

Destination IPv4 address (required for UDP server only)

  • e.g., 192.168.1.100

<dst_port>:

Destination port (required for UDP server only)

  • Range: [1,65535]

<data>:

The actual data to be send, length is specified by <data_size>

Error Codes

  • 1: Parameter error

  • 2: Failed to send data

  • 3: Memory allocation failed

  • 4: Get data failed in TT mode

Examples

TCP client (link_id = 0) send data to TCP server:

AT+SKTSENDRAW=0,6
>>>
Hello!
OK

UDP server (link_id = 3) send data to UDP client (IPv4 address: 192.168.0.103, port: 61468):

AT+SKTSENDRAW=3,10,192.168.0.103,61468
>>>
Lwip_Test!
OK

TLS server (link_id = 1) send data to TLS client (seed link_id = 2):

AT+SKTSENDRAW=2,5
>>>
12345
OK

AT+SKTSTATE

Function Description

Query all active connection <link_id> status.

Command Format

AT+SKTSTATE

Response Format

Standard Response:

link_id:<link_id>,<role>,<protocol>,src_address:<ip>,src_port:<port>,dst_address:<ip>,dst_port:<port>,socket_fd:<fd>

OK

Parameters

<link_id>:

Link ID created successfully

  • Range: [0, MEMP_NUM_NETCONN-1], currently MEMP_NUM_NETCONN=20 for lwip configuration

<role>:

String format

  • server

  • client

  • seed

<protocol>:

String format, the protocol used by connection <link_id>

  • tcp

  • udp

  • tls

<ip>:

IPv4 address

  • e.g., 192.168.1.100

<port>:

port number

  • Range: [1,65535]

<fd>:

socket fd created by lwip

Example

Assume there is a TCP server (link_id = 0) with its seed (a TCP client, link_id = 3), a UDP server (link_id = 1), and TLS client (link_id = 2):

AT+SKTSTATE
link_id:0,server,tcp,src_address:192.168.0.109,src_port:6666,dst_address:0.0.0.0,dst_port:0,socket_fd:0
link_id:3,seed,tcp,src_address:0.0.0.0,src_port:0,dst_address:192.168.0.103,dst_port:49941,socket_fd:3
link_id:1,server,udp,src_address:192.168.0.109,src_port:12345,dst_address:0.0.0.0,dst_port:0,socket_fd:1
link_id:2,client,ssl,src_address:0.0.0.0,src_port:0,dst_address:192.168.0.103,dst_port:4433,socket_fd:2

OK

AT+SKTDEL

Function Description

Delete specified connection <link_id>. If <link_id> corresponds to a client or a seed, the execution of the command will only delete the <link_id> itself. If <link_id> corresponds to a server, the execution of the command will delete this <link_id> along with all of its seeds (clients).

Command Format

AT+SKTDEL=<link_id>

Response Format

Success Response:

OK

Error Response:

ERROR: <error_no>

Parameters

<link_id>:

Link ID created successfully

  • Range: [0, MEMP_NUM_NETCONN-1], currently MEMP_NUM_NETCONN=20 for lwip configuration

Error Codes

  • 1: Parameter error

Examples

Assume there is a TCP server (link_id = 0) with its seed (a TCP client, link_id = 3), a UDP server (link_id = 1), and TLS client (link_id = 2):

AT+SKTSTATE
link_id:0,server,tcp,src_address:192.168.0.109,src_port:6666,dst_address:0.0.0.0,dst_port:0,socket_fd:0
link_id:3,seed,tcp,src_address:0.0.0.0,src_port:0,dst_address:192.168.0.103,dst_port:49941,socket_fd:3
link_id:1,server,udp,src_address:192.168.0.109,src_port:12345,dst_address:0.0.0.0,dst_port:0,socket_fd:1
link_id:2,client,ssl,src_address:0.0.0.0,src_port:0,dst_address:192.168.0.103,dst_port:4433,socket_fd:2

OK

Now user want to delete link_id = 0, cause link_id = 0 corresponds to a TCP server, to delete this link, it needs to be removed along with its seed(link_id = 3), and then use the command AT+SKTSTATE to check:

AT+SKTDEL=0
OK

AT+SKTSTATE
link_id:1,server,udp,src_address:192.168.0.109,src_port:12345,dst_address:0.0.0.0,dst_port:0,socket_fd:1
link_id:2,client,ssl,src_address:0.0.0.0,src_port:0,dst_address:192.168.0.103,dst_port:4433,socket_fd:2

OK