通过pid_ioctl函数的get命令可确认TCP的各种状态。
$return = pid_ioctl($pid, "get ITEM");
ITEM | 说明 | 返还值 | 返还类型 |
---|---|---|---|
state | TCP session is closed | TCP_CLOSED | integer |
TCP session is connected | TCP_CONNECTED | integer | |
TCP session waits for connection | TCP_LISTEN | integer | |
SSL session is closed | SSL_CLOSED | integer | |
SSL session is connected | SSL_CONNECTED | integer | |
SSL session waits for connection | SSL_LISTEN | integer | |
SSL session is closed | SSH_CLOSED | integer | |
SSL session is connected | SSH_CONNECTED | integer | |
SSL session waits for connection | SSH_LISTEN | integer | |
SSL authentication is completed | SSH_AUTH | integer | |
srcaddr | local IP address | e.g. 192.168.0.1 | string |
srcport | local port number | e.g. 1470 | integer |
dstaddr | peer IP address | e.g. 192.168.0.2 | string |
dstport | peer TCP number | e.g. 1470 | integer |
txbuf | size of send buffer[Byte] | e.g. 1152 | integer |
txfree | remaining send buffer size[Byte] | e.g. 1152 | integer |
rxbuf | size of receive buffer[Byte] | e.g. 1068 | integer |
rxlen | received data size[Byte]/td> | e.g. 200 | integer |
ssh username | SSH user name | e.g. user | string |
ssh password | SSH password | e.g. password | string |
TCP在连接过程以后进行数据通信,故查看TCP Session状态非常重要。 状态值分为3种,表示未连接或者已经断开的状态:TCP_CLOSED、完成连接的状态:TCP_CONNECTED和作为TCP server等待连接的状态:TCP_LISTEN。 SSL与SSH同样分为未连接(SSL_CLOSED)、完成连接(SSL_CONNECTED)及等待连接(SSL_LISTEN)的三种状态,SSH还多一个认证(SSH_AUTH)状态。 此值都是在PHPoC中提前定义的常数(predefined constant)。 所有session的状态可如下确认。
$state = pid_ioctl($pid, "get state");
※ 在试图进行TCP,SSL或是SSH连接中或终止时进行会话的状态信息确认时,会返还表中之外的值。此值为固件内部使用的常数,以后会有被变更的情况发生,故不建议用户编程当中。
在发送缓冲区剩余数据的大小可如下进行计算。
发送缓冲区剩余的数据大小 = 发送缓冲区大小r - 发送缓冲区的剩余空间
在这个例子进行TCP连接后传送8字节数据,计算TCP发送缓冲区中的数据大小输出。
$tx_len = -1;
$pid = pid_open("/mmap/tcp0"); // open TCP 0
do
{
pid_connect($pid, "10.1.0.2", 1470); // TCP active connection
usleep(500000);
}
while(pid_ioctl($pid, "get state") != TCP_CONNECTED);
pid_send($pid, "01234567"); // send 8 bytes
while($tx_len && (pid_ioctl($pid, "get state") == TCP_CONNECTED))
{
$txbuf = pid_ioctl($pid, "get txbuf"); // get the size of send buffer
// get the empty size of send buffer
$txfree = pid_ioctl($pid, "get txfree");
// calculate the size of remaining data in send buffer
$tx_len = $txbuf - $txfree;
echo "tx len = $tx_len\r\n"; // print the result
usleep(10000);
}
pid_close($pid); // close TCP
在TCP接收的数据大小可如下进行确认。
$rxlen = pid_ioctl($pid, "get rxlen[ $string]");
在"rxlen"命令后面输入特定字符串($string),pid_ioctl函数在相应字符串进来前返还0,遇到相应字符串返还至该字符串的接收数据大小。
TCP的接收缓冲区剩余空间可如下进行计算。
接收缓冲区剩余空间 = 接收缓冲区大小 - 接收数据大小
此例中在TCP连接后计算接收缓冲区的剩余空间输出。
$rx_free = 1068;
$pid = pid_open("/mmap/tcp0"); // open TCP 0
do
{
pid_connect($pid, "10.1.0.2", 1470); // TCP active connection
usleep(500000);
}
while(pid_ioctl($pid, "get state") != TCP_CONNECTED);
while(($rx_free > 500) && (pid_ioctl($pid, "get state") == TCP_CONNECTED))
{
$rxbuf = pid_ioctl($pid, "get rxbuf"); // get the size of receive buffer
$rxlen = pid_ioctl($pid, "get rxlen"); // get the size of received data
// calculate the available space of receive buffer
$rx_free = $rxbuf - $rxlen;
echo "rx free = $rx_free\r\n"; // print the result
sleep(1);
}
pid_close($pid); // close TCP