ytt Posted January 14, 2023 Share Posted January 14, 2023 <?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 ? Quote Link to comment Share on other sites More sharing options...
kicken Posted January 14, 2023 Share Posted January 14, 2023 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. Quote Link to comment Share on other sites More sharing options...
ytt Posted January 14, 2023 Author Share Posted January 14, 2023 Problem occurs as same even if I add \r\n to server's response or set the socket non blocking. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.