TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 03-17-2010, 08:10 PM   #1 (permalink)
The Acquainted
 
EyeDentify's Avatar
 
Join Date: Nov 2007
Location: Sweden
Posts: 106
Thanks: 13
EyeDentify is on a distinguished road
Default 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.
__________________
Of course the whole point of a doomsday machine, would have been lost if you keep it a secret.
EyeDentify is offline  
Reply With Quote
Old 05-25-2010, 01:13 AM   #2 (permalink)
Banned
 
Join Date: May 2010
Posts: 1
Thanks: 0
helenk579 is on a distinguished road
Default

Thanks you for the post.

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

Last edited by Salathe : 05-25-2010 at 07:22 PM.
helenk579 is offline  
Reply With Quote
Old 05-25-2010, 02:11 PM   #3 (permalink)
The Contributor
 
nefus's Avatar
 
Join Date: Nov 2007
Location: Nashville, TN
Posts: 66
Thanks: 20
nefus is on a distinguished road
Default

Oh.. I'd like to know the answer to this as well.
__________________
I am not a programmer, nor do I play one on tv.
nefus is offline  
Reply With Quote
Old 05-25-2010, 07:11 PM   #4 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

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

Village Idiot is offline  
Reply With Quote
Old 05-25-2010, 07:20 PM   #5 (permalink)
The Contributor
 
nefus's Avatar
 
Join Date: Nov 2007
Location: Nashville, TN
Posts: 66
Thanks: 20
nefus is on a distinguished road
Default

Awww bummer. I didn't notice that. Still, does anyone know the basic answer to this?
__________________
I am not a programmer, nor do I play one on tv.
nefus is offline  
Reply With Quote
Old 05-26-2010, 09:05 AM   #6 (permalink)
The Wanderer
 
Join Date: May 2010
Posts: 19
Thanks: 1
core1024 is on a distinguished road
Default

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.
core1024 is offline  
Reply With Quote
The Following User Says Thank You to core1024 For This Useful Post:
nefus (05-26-2010)
Old 05-26-2010, 01:35 PM   #7 (permalink)
The Contributor
 
nefus's Avatar
 
Join Date: Nov 2007
Location: Nashville, TN
Posts: 66
Thanks: 20
nefus is on a distinguished road
Default

Interesting.
__________________
I am not a programmer, nor do I play one on tv.
nefus is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Stream problem Tanax Advanced PHP Programming 9 06-10-2009 12:33 PM
Huge Session Problem Killswitch General 1 11-17-2008 02:36 AM
Problem with Query + While $a = mysql_fetch_array($b) Aaron Absolute Beginners 0 07-03-2008 07:16 PM
Strange string comparison problem Jelmer General 0 05-23-2008 04:00 PM
A bug or a cache problem?! yazid Advanced PHP Programming 0 05-22-2008 08:40 AM


All times are GMT. The time now is 02:53 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design