Jump to content

Recommended Posts

<?php

if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {

echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";

}

if (socket_connect($sock, '127.0.0.1', 10000) === false) {

echo "socket_connect() failed: reason: " . socket_strerror(socket_last_error()) . "\n";

}

$msg = "test";

socket_write($sock, $msg, strlen($msg));

if (false === ($buf = socket_read($sock, 2048, PHP_NORMAL_READ))) {

echo "socket_read() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";

}

echo "response: $buf \n";


I am using this code to connect to a C socket server. Server is functioning without any problems with clients that are written by C. But when I use php client it hangs after 2 attempts at socket_read. First two results are good but after that it hangs at socket_read. What might be the main cause of this behavior I have no idea.

 

Any suggestions ?

 

Link to comment
https://forums.phpfreaks.com/topic/315802-socket-fails-to-read-after-two-attempts/
Share on other sites

21 minutes ago, ytt said:

What might be the main cause of this behavior I have no idea.

Probably not enough data on the socket to be read.  The read will block until it has read the number of bytes specified, or found a \n or \r line ending.  Try making it non-blocking and see if it works.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.