Jump to content

[SOLVED] Sockets


GKWelding

Recommended Posts

Ok,

 

I have a custom socket handler for a project I am working on. Now this socket handler has been tested thoroughly and can keep the connection open, and stable for days. However, randomly, it loses the socket connection every now and again for no apparent reason. We've spent a lot of time and monet trying to track the cause of this down and we can't. So now what we're looking to do is just detect when the socket disconnects and reconnect it. Now our flash handler has a function that does this for us, it detects the disconnect and reconnects automatically. However, from the PHP docs I can't seem to find a similar function in PHP. Is there one? Is it possible to automatically reconnect a disconnected socket in PHP?

 

Just a note the socket handler is auto run every 10 seconds or so where it queries our database and z00m server for any changes to pass between the two.

Link to comment
Share on other sites

Ok, I'm going to make a couple assumptions:

 

1. Your using php to connect to a tcp listener.

 

2. I'm assuming we're talking about TCP and not UDP.

 

Here is some code that might help. I tested it with a php tcp listener I wrote and it worked. I've not tested it in your environment though:

 

<?php

//The ip address of the host
$address = gethostbyaddr('127.0.0.1);

//Create a TCP socket in memory
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

if ($socket === false)
{
     echo "socket_create() failed: reason: " . socket_strerror(socket_last_error());
}

//Attempt to connect to the server
//Here is the meat of it. Put your connection in a loop and try to connect a designated amount of times
for ($i=0;, $i<100, $i++)
{
     //Connect to whatever port you wish
     $result = socket_connect($socket, $address, 1015);
     
     if ($result !== false)
     {
            break;
     }
}

if ($result === false)
{
     echo "socket_connect() failed: reason:" . socket_strerror(socket_last_error($socket));
}

// ... your code after this
?>

 

As you can see I just put the socket_connect() function in a loop and attempted to connect multiple times. If it connects then exit out of the loop and continue. I used a for loop instead of a while loop since this could run on forever if there really is something wrong with the listener.

 

Link to comment
Share on other sites

Thanks for your replies, however, I think I may need to explain a little more here. I can connect fine to the socket, however, randomly the socket drops the connection when it shouldn't. Now all I want to do is catch this connection dropping and tell the script to reconnect again.

 

The socket server and is just a tcp listener written by my company using ruby. When the open socket gets randomly disconnected it sends out a message sayign so, now flash can interpret this message and reconnects the socket automatically. However, PHP doesn't seem to have a function for this, is that correct?

Link to comment
Share on other sites

when the 'connection' drops, does your server crash, or php just exits?

 

I know a socket would consist of a loop

 

try at the end of every loop..

 

put simple functions at the bottom and just watch the output

 

echo socket_strerror(socket_last_error());

var_dump($socket_handle);

 

how do you "know" the socket drops per-say?

 

per loop do you re-open a mysql connection? if so you can be crashing your server..

 

if you're NOT using a server.. disregard the above lol..

 

idk what else to say, your socket shouldn't die..

 

 

maybe show us the script? hide any valuable information, or PM it to me if its sensitive and you don't want any1 to see it.

 

I've worked on a few servers so far and I'm sure I could help lol

 

if anything, at the very least, I hope you resolve your problem

Link to comment
Share on other sites

problem solved, before evey data send i do a socket_read. If the socket_read returns false then I know the sockets down and I reconnect. If the socket returns data then the socket is up, if the socket returns a blank string I also know the socket is up but that the buffer's empty. Either way, socket_read and $socket === false will tell me if he socket's down.

 

Cheers for the help anyway guys.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.