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?
Login or Register to Ask a Question

Previous Thread | Next Thread

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 ./ #!/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? "" 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
Login or Register to Ask a Question
SOCKET_LAST_ERROR(3)							 1						      SOCKET_LAST_ERROR(3)

socket_last_error - Returns the last error on the socket

int socket_last_error ([resource $socket]) DESCRIPTION
If a socket resource is passed to this function, the last error which occurred on this particular socket is returned. If the socket resource is omitted, the error code of the last failed socket function is returned. The latter is particularly helpful for functions like socket_create(3) which don't return a socket on failure and socket_select(3) which can fail for reasons not directly tied to a particular socket. The error code is suitable to be fed to socket_strerror(3) which returns a string describing the given error code. PARAMETERS
o $socket - A valid socket resource created with socket_create(3). RETURN VALUES
This function returns a socket error code. EXAMPLES
Example #1 socket_last_error(3) example <?php $socket = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP); if ($socket === false) { $errorcode = socket_last_error(); $errormsg = socket_strerror($errorcode); die("Couldn't create socket: [$errorcode] $errormsg"); } ?> NOTES
Note socket_last_error(3) does not clear the error code, use socket_clear_error(3) for this purpose. PHP Documentation Group SOCKET_LAST_ERROR(3)