TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   Problem with sockets not closing (http://www.talkphp.com/general/5357-problem-sockets-not-closing.html)

EyeDentify 03-17-2010 08:10 PM

Problem with sockets not closing
 
Hello my PHP gurus.

I have something for you to sink your teeth into.

let me show you my code im working with:

PHP Code:

<?PHP
/* Simple Socket Server v1.0 */

    // set some variables
    
$host gethostbyname(gethostname());
    
$port 9996;
    
    
// don't timeout!
    
set_time_limit(0);
    
    
// create socket
    
$socket socket_create(AF_INETSOCK_STREAM0) or die("Could not create socket\n");
        
    
// bind socket to port
    
$result socket_bind($socket$host$port) or die("Could not bind to socket\n");
    
    
// start listening for connections
    
$result socket_listen($socket3) or die("Could not set up socket listener\n");
    
socket_set_block($socket);
    
    echo(
"Waiting for socket connection on " $host ":" $port "\n\r");
    
    
// accept incoming connections
    // spawn another socket to handle communication
    
    
$socketAccept '';
    
$socketAccept socket_accept($socket) or die("Could not accept incoming connection\n");
    
    echo(
"Incomming connection accepted!\n\r");
    
    
// Make a file to save incoming socket data to
    
$randFileName 'test_' md5(mt_rand(01000) ) . '.test';
    
$inFileHandle fopen($randFileName'wb');
    
    while(!
$input == '')
    {
        
// read client input
        
$input socket_read($socketAccept1024) or die("Could not read input\n\r");
        
        
// save data to file
        
fwrite($inFileHandle$input1024);
    
    }
    
    
// close file handle
    
fclose($inFileHandle);
    
    echo(
"Socket file data read and saved to\n\r" $randFileName "\n\r");
            
    
    
// shut down sockets
    /*
    socket_shutdown($socketAccept, 2);
    socket_shutdown($socket, 2);
    */
    
    // close sockets
    
socket_close($socketAccept);
    
socket_close($socket);
    
    echo(
"Sockets closed!\n\r\n\r");
    
    
?>

This script works the first time itīs run and my client script on another computer sends data and this server script saves it to a file. And all is well.

Now.. the problem is that when i want to run the server script again and try to send data to it with my client script. The client script gives the error that a connection on the server script is blocking the socket_write() function in my client script.

It would seem that the socket first opened the first run on the server script is still open and wont accept new connections.

I canīt get whats wrong. Im really looking for your advice.

By request i can post the client script also, but i donīt think its where the problem lies.

Sorry for the english :)

Note:
- I am running the script from Win7 command line via the php.exe executible.


Thank you in advance.

helenk579 05-25-2010 01:13 AM

Thanks you for the post.

<I'm a sucky spammer so now I'm banned; no link juice for me>

nefus 05-25-2010 02:11 PM

Oh.. I'd like to know the answer to this as well.

Village Idiot 05-25-2010 07:11 PM

This thread is ancient, the above post is fake sig (link spam).

nefus 05-25-2010 07:20 PM

Awww bummer. I didn't notice that. Still, does anyone know the basic answer to this?

core1024 05-26-2010 09:05 AM

I've tested it and the result is following
Code:

~/test$ php5 srv.php
Waiting for socket connection on 127.0.1.1:9996
Incomming connection accepted!
Socket file data read and saved to
test_b4288d9c0ec0a1841b3b3728321e7088.test
Sockets closed!

 
~/test$ pgrep -lf php
~/test$ netstat -n| grep 9996
tcp        0      0 127.0.1.1:9996          127.0.1.1:49404        TIME_WAIT 
~/test$

After the script terminates there is not zombie process. In netstat manual says that TIME_WAIT means that after the socket is closed the port is still waiting for not delivered packets. I am not sure what is causing it, but for sure it is a parameter to be set.

nefus 05-26-2010 01:35 PM

Interesting.


All times are GMT. The time now is 05:30 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0