Kobiic
Kobiic

Reputation: 31

PHP socket connections: read and send data in loop

I have to make a function, in which I send a packet to server and then read data. After a while I sometimes require to send a data to server again using the same socket (it's required that I use the same one).

For some reason second send, which is after read doesn't send data (it returns correct number (not false), but server doesn't receive packet). If I create new socket and send data - it works.

Here is an example:

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_connect($socket, '192.168.1.179', 1455);
socket_set_option($socket,SOL_SOCKET, SO_RCVTIMEO, array("sec"=>5, "usec"=>0));

$this->socket_functions->send_message($socket,$sendMsg,$funcName);
$tmp = trim($this->socket_functions->read_packet($socket));

for($i =0; $i<10; $i++)
{
    $tmp = trim($this->socket_functions->read_packet($socket));

    $this->socket_functions->send_message($socket, 'AAAA', $funcName);      //doesn't work
    /*
    ///works
    $socket2 = $this->socket_functions->create_socket();
    $this->socket_functions->send_message($socket2, 'AAAA', $funcName);
    $this->socket_functions->disconnect($socket2);
    */
}

Function create_socket does the same as first 3 lines so the connection data is the same. I just brought it out so you are able to see my configuration.

For read and write I use functions socket_send() and socket_write().

Read packet function:

    $resultMsg = "";
    while(strlen($currentData = socket_read($socket,256))==256) 
    {
        $resultMsg .=$currentData;
    }
    $resultMsg.=$currentData;

    if(strlen($resultMsg)>1 && strpos($resultMsg,'<')>0)
    {
        $resultMsg = substr($resultMsg,strpos($resultMsg,'<'));
    }
    return $resultMsg;

Sending packet:

function create_packet($msg, $type)     
{
    $msg = $type.$this->convert_data->IntToAsciiChars(strlen($msg)).$msg;
    $msg = chr(0).$this->convert_data->IntToAsciiChars(strlen($msg)).$msg;
    return $msg;
}

function send_message($socket,$msg,$type)
{
    $packet = $this->create_packet($msg,$type);
    $res = socket_send($socket,$packet,strlen($packet),0);
    if($res === false) return false;
    return true;
}

EDIT

I did more testing and found out, that this only occurs, if the server, to which I'm connected keeps sending data. As in - the server sends multiple packets in row. If I try to send packet before the server has sent all of them, the packet isn't received. Does anyone knows why is this happening? I have desktop application, which does the same thing (sends data using the same socket while server is sending data), but it works there. Is there something specific?

Upvotes: 0

Views: 1519

Answers (1)

Kobiic
Kobiic

Reputation: 31

I finally managed to fix this. After testing I found out, if I used send n+1 times to send the message, where n is amount of times read was called , I was able to send a message then.

I really don't know why this worked - maybe someone here knows?

Upvotes: 1

Related Questions