Getting Status of TCP


To get states of TCP, get command of pid_ioctl function is required.

$return = pid_ioctl($pid, "get ITEM");

Available TCP States

ITEM Description Return Value Return Type
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 Session Status

Checking status of connection on TCP is very important because TCP data communication is made after the connection phase. There are three session states: TCP_CLOSED when session is not connected, TCP_CONNECTED when session is connected and TCP_LISTEN when TCP server is listening connection. SSL and SSH have also these three states: SSL_CLOSED, SSL_CONNECTED and SSL_LISTEN and SSH has an additional state about authentication (SSH_AUTH). These values are all predefined constants of PHPoC.
The following shows how to get states of session.

<?php
$state = pid_ioctl($pid, "get state");
?>

※ An unknown value, which is not listed in the table above, could be returned if you try to get a state when PHPoC is connecting or closing connection. Note that it is not recommended to use these values in your script because it might be changed in the future.

Remaining Data Size in Send Buffer

Remaining data size in send buffer can be calculated as follows:

remaining data size in send buffer = size of buffer - remaining size of buffer

example

In this example, PHPoC sends 8 bytes data to a server right after TCP connection is established. While sending the data, PHPoC prints remaining data size in send buffer.

<?php
$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

?>

Received Data Size

The following shows how to get the received data size from TCP socket.

$rxlen = pid_ioctl($pid, "get rxlen[ $string]");

Getting the received data size with string

If a string is specified after "get rxlen" command, pid_ioctl function returns 0 until the string comes into TCP socket. If the specified string comes, the function returns the whole data size including the string.

Remaining Size of Receive Buffer

Remaining size of receive buffer can be calculated as follows:

remaining size of receive buffer = size of buffer - size of received data

example

This example shows how to get the remaining size of receive buffer.

<?php
$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
?>