Visit Our UNIX and Linux User Community


PHP: How to use socket_select() and socket_read() correctly


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting PHP: How to use socket_select() and socket_read() correctly
# 1  
Old 08-26-2013
PHP: How to use socket_select() and socket_read() correctly

I have a multithreaded PHP application that uses sockets for communicating between threads. The communication works pretty well, except for one thing: when I call socket_select(), it works the first time after a new connection is made, but after that it always reports read handles ready, even after I've read all the data from them. Here's what I mean:

Main thread:
1. Setup socket, bind to port, listen
2. socket_select() (block, with null timeout to make it wait forever) on the socket from step 1
3. That call works fine: after the child thread connects, socket_select() tells me that the socket from step 1 is ready
4. Accept new connection, remember the handle returned by accept().
5. socket_select() on the main socket plus the new socket
6. After child thread writes, socket_select() reports that the new socket is ready
7. I read all the data from the socket, then go back to step 5.

At this point, socket_select() continues to report that the new socket is ready, even though the child thread has written no new data to it. What in the world am I doing wrong? Is it convention to close the new connection at this point and have the child thread reconnect? How can I get socket_select() to stop reporting that the new handle is ready to read?

Previous Thread | Next Thread
Test Your Knowledge in Computers #949
Difficulty: Medium
In computer security, term virus and malware refer to the exact same thing.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Getopts not echoing correctly

Hi, When I run the the following code: #!/bin/bash if ]; then usage fi if ] then echo "Do not execute this as root, use -s instead" fi SERVERFILE="servers" function usage { echo "USAGE: ${0} COMMAND" (4 Replies)
Discussion started by: mohca2020
4 Replies

2. UNIX for Beginners Questions & Answers

Help with function - is placed in script correctly?

Hi Folks - I hope everyone is well. I just need some assistance with a script I've put together. I haven't used functions before so I was wondering if my script is built properly in terms of architecture? The first command of the script shuts down services. Then, the second part finds a... (1 Reply)
Discussion started by: SIMMS7400
1 Replies

3. Shell Programming and Scripting

Subroutines; am I capturing them correctly?

HI Folks - Again, I'm very sorry for the amateur post. I'm reletively new to shell scripting so please bear with me. I have a script that execute another script which stops a particular set of services for my application. IF the execution is successful, I want to check for any hung ESSSVR... (4 Replies)
Discussion started by: SIMMS7400
4 Replies

4. Shell Programming and Scripting

Printf ... some suggestions on using correctly?

Hi all. Probably a silly question... :-( This is my script. $ cat ./QuoteGrabber.sh #!/usr/bin/bash a=$(./DDECmd.exe request -s TOS -t ASK -i /ESM1) b=$(./DDECmd.exe request -s TOS -t BID -i /ESM1) c=$(./DDECmd.exe request -s TOS -t VOLUME -i /ESM1) d=$(./DDECmd.exe request -s TOS -t... (4 Replies)
Discussion started by: paolfili
4 Replies

5. Shell Programming and Scripting

rsync is not correctly working

We are using Red Hat linux system. I am transferring my rman backup files to another server. Here is the command i am using to transfer the files. /usr/bin/rsync -avpP --delete /xyz/xyz/ 99.99.999.99::db110bkp Here is the rsync version. >rsync --version rsync version 3.0.6 ... (1 Reply)
Discussion started by: govindts
1 Replies

6. Shell Programming and Scripting

Escaping ** correctly

Hello This should be easy, but bash is giving me headaches. At the command line the following command works: duplicity --include /home --exclude '**' / file:///foo Doing that from a script is not straightforward. Note that it is basically a requirement that I place the... (3 Replies)
Discussion started by: brsett
3 Replies

7. Shell Programming and Scripting

Is grep being used correctly?

My goal is to find files contain the "signal 11" string in a specific directory. I need the file details followed by the string. I wrote a script to test out and play with Shell command since it's my first time to write a Shell script. Let me cut the story short... this command line: if ... (2 Replies)
Discussion started by: sai0899
2 Replies

8. Shell Programming and Scripting

if not working correctly

Anyone have an idea why this if statement does not work correctly? "test2.sh" 18 lines, 386 characters #!/usr/bin/sh WARNING=80 CRITICAL=95 check_it() { if ] || ];then echo "YES ] || ]" else echo "NO ] || ]" fi } check_it 80.1 check_it 81.1 (3 Replies)
Discussion started by: 2dumb
3 Replies

9. HP-UX

HP-UX will not boot correctly

i've same failure too, but this command boot pri isl not work/not found Thanks! (1 Reply)
Discussion started by: pantas manik
1 Replies

10. UNIX for Dummies Questions & Answers

HP-UX will not boot correctly

I am an intern at a company with an HP visualize C160 running HP-UX B.10.20. It has stopped booting to the gui and, after some coaxing, will end up at a text-based menu screen but virtually nothing can be run from there. At first the computer ran through it's boot process then stopped at this... (5 Replies)
Discussion started by: intern
5 Replies
SOCKET_CREATE_PAIR(3)							 1						     SOCKET_CREATE_PAIR(3)

socket_create_pair - Creates a pair of indistinguishable sockets and stores them in an array

SYNOPSIS
bool socket_create_pair (int $domain, int $type, int $protocol, array &$fd) DESCRIPTION
socket_create_pair(3) creates two connected and indistinguishable sockets, and stores them in $fd. This function is commonly used in IPC (InterProcess Communication). PARAMETERS
o $domain - The $domain parameter specifies the protocol family to be used by the socket. See socket_create(3) for the full list. o $type - The $type parameter selects the type of communication to be used by the socket. See socket_create(3) for the full list. o $protocol - The $protocol parameter sets the specific protocol within the specified $domain to be used when communicating on the returned socket. The proper value can be retrieved by name by using getprotobyname(3). If the desired protocol is TCP, or UDP the corre- sponding constants SOL_TCP, and SOL_UDP can also be used. See socket_create(3) for the full list of supported protocols. o $fd - Reference to an array in which the two socket resources will be inserted. RETURN VALUES
Returns TRUE on success or FALSE on failure. CHANGELOG
+--------+---------------------------------------------------+ |Version | | | | | | | Description | | | | +--------+---------------------------------------------------+ | 5.3.0 | | | | | | | This function is now re-enabled on Windows plat- | | | forms. | | | | | 4.3.0 | | | | | | | This function was due to a bug made unavailable | | | on Windows platforms. | | | | +--------+---------------------------------------------------+ EXAMPLES
Example #1 socket_create_pair(3) example <?php $sockets = array(); /* On Windows we need to use AF_INET */ $domain = (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN' ? AF_INET : AF_UNIX); /* Setup socket pair */ if (socket_create_pair($domain, SOCK_STREAM, 0, $sockets) === false) { echo "socket_create_pair failed. Reason: ".socket_strerror(socket_last_error()); } /* Send and Recieve Data */ if (socket_write($sockets[0], "ABCdef123 ", strlen("ABCdef123 ")) === false) { echo "socket_write() failed. Reason: ".socket_strerror(socket_last_error($sockets[0])); } if (($data = socket_read($sockets[1], strlen("ABCdef123 "), PHP_BINARY_READ)) === false) { echo "socket_read() failed. Reason: ".socket_strerror(socket_last_error($sockets[1])); } var_dump($data); /* Close sockets */ socket_close($sockets[0]); socket_close($sockets[1]); ?> Example #2 socket_create_pair(3) IPC example <?php $ary = array(); $strone = 'Message From Parent.'; $strtwo = 'Message From Child.'; if (socket_create_pair(AF_UNIX, SOCK_STREAM, 0, $ary) === false) { echo "socket_create_pair() failed. Reason: ".socket_strerror(socket_last_error()); } $pid = pcntl_fork(); if ($pid == -1) { echo 'Could not fork Process.'; } elseif ($pid) { /*parent*/ socket_close($ary[0]); if (socket_write($ary[1], $strone, strlen($strone)) === false) { echo "socket_write() failed. Reason: ".socket_strerror(socket_last_error($ary[1])); } if (socket_read($ary[1], strlen($strtwo), PHP_BINARY_READ) == $strtwo) { echo "Recieved $strtwo "; } socket_close($ary[1]); } else { /*child*/ socket_close($ary[1]); if (socket_write($ary[0], $strtwo, strlen($strtwo)) === false) { echo "socket_write() failed. Reason: ".socket_strerror(socket_last_error($ary[0])); } if (socket_read($ary[0], strlen($strone), PHP_BINARY_READ) == $strone) { echo "Recieved $strone "; } socket_close($ary[0]); } ?> SEE ALSO
socket_create(3), socket_create_listen(3), socket_bind(3), socket_listen(3), socket_last_error(3), socket_strerror(3). PHP Documentation Group SOCKET_CREATE_PAIR(3)

Featured Tech Videos