Current location: Hot Scripts Forums » Programming Languages » PHP » PHP sockets - checking connection.. HELP!!!


PHP sockets - checking connection.. HELP!!!

Reply
  #1 (permalink)  
Old 12-29-05, 01:37 PM
Turboz Turboz is offline
Newbie Coder
 
Join Date: Mar 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
PHP sockets - checking connection.. HELP!!!

Hi
I'm working on my own instant messenger. This will not use a dedicated chat server, but instead each client will have it's own built in.

The client will contact my site, executing a script will will then see if anyone is currently hosting a server or not.

My script will read an ini file, read an ip address/port and then try to connect and see if the connection works. Once it's determined that the connection is ok, it will write the IPort to my program for the client to connect to that address.

So far I've found 3 methods for doing this and none of them like working full time.

Sometimes it works, then it just stops for no aparent reason. I've spent all day on this and now I'm at the point where I want to thrown my pc out the window, and come to the USA to destroy my host's server

OK, this is my script so far:
Code:
<HTML>
<HEAD>
<TITLE>Turboz Messenger</TITLE>
<?
If ($_SERVER['QUERY_STRING'] == 'test')
	{ ?>
	<SCRIPT LANGUAGE="JavaScript">
	<!-- Begin
	function verify()
		{
		var themessage = "You are required to complete the following fields: ";
		if (document.test.host.value=="")
			{
			themessage = themessage + "Host,";
			}
		if (document.test.port.value=="")
			{
			themessage = themessage + " & Port";
			}
		//alert if fields are empty and cancel form submit
		if (themessage == "You are required to complete the following fields: ")
			{
			document.test.submit();
			}
			else
			{	
			alert(themessage);
			return false;
			}
		}
	//  End -->
	</script>
	<?
	}
?>

</HEAD>
<? 
$MainPort = 80;
If ($_SERVER['QUERY_STRING'] == 'check')
	{
	require ("inirw.class.php");
	$ini = new iniRW ();

	$Host = $ini->read_item("Server.ini", "Server", "Host");
	$Port = $ini->read_item("Server.ini", "Server", "Port");
	}

If ($_SERVER['QUERY_STRING'] == 'test')
	{
	echo '<FORM NAME="test" ACTION="' .$_SERVER[PHP_SELF] .'" METHOD=POST><BR>';
	echo 'Host (Domain/IP): <INPUT TYPE="TEXT" WIDTH="40" NAME="host"><BR>';
	echo 'Port Number: <INPUT TYPE="TEXT" WIDTH="5" NAME="port"><BR>';
	echo '<INPUT TYPE=BUTTON VALUE="test" onclick="verify();">';
	echo '</FORM>';
	exit;
	}

If ($_POST['host'])
	{
	$Host = $_POST['host'];
	$Port = $_POST['port'];
	}

If ($_GET['host'])
	{
	$Host = $_GET['host'];
	$Port = $_GET['port'];
	}

If (empty($Port))
	{
	echo 'No port number supplied. Assuming 80.<BR>';
	$Port = $MainPort;
	}

Test($Host, $Port);

function Test($Host, $Port)
	{
	If (empty($Host))
		{
		echo 'No Host/IP supplied.';
		exit;
		}

	echo "$Host ==> $Port<BR>";
	}

//Connection Checker Goes Here
?>
</HTML>
Now the problem I am having is theses following 3 methods for checking the connection:

1: Works in the original script it came from but does not work now in mine.
Code:
//My Version
$churl = @fsockopen(server($Host), $Port, $errno, $errstr, 20);
	if (!$churl)
		{
		//echo $errstr;
		echo 'Fail';
		}
		else
		{
		echo 'good';
		}

function server($Host)
	{
      if(strstr($Host,"/"))
		{
		$Host = substr($Host, 0, strpos($Host, "/"));
		}
         return $Host;
	}
Original Working Version
Code:
//Test the server connection
$churl = @fsockopen(server($addr), $port, $errno, $errstr, 20);
             if (!$churl){
			 //echo $errstr;
                header("Location: $dead");
                }
             else {
             	  header("Location: $live");             
		  }
function server($addr){
         if(strstr($addr,"/")){$addr = substr($addr, 0, strpos($addr, "/"));}
         return $addr;
2: Came from php's manual itself. Works on Windows, never on my site.
Code:
	/*Get the IP address for the target host. */
	$Host = gethostbyname($Host);
	
	/*Create a TCP/IP socket.
	$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
	//$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
	if ($socket < 0)
		{
		echo "Failure";
		}
		else
		{
		echo "Socket OK.<BR>";
		}
	
	echo "Attempting to connect to '$Host' on port 

'$Port'...<BR>";
	$result = socket_connect($socket, $Host, $Port);
	if ($result < 0)
		{
		echo "socket_connect() failed.<BR>Reason: ($result) " 

. socket_strerror($result) . "<BR>";
		}
		else
		{
		echo "OK<BR>";
		$out = '';
//Trouble here
		while ($out > 0);
			{
			$out = socket_recv($socket,$buffer,1024,0);
			echo 'Response was: '."$buffer<BR>";
			}
		}
//This was used to get around the socket_read not working on Windows
	echo "Closing socket...";
	socket_close($socket);
	echo "OK.<BR>";
Then we have 3: Original version from php's manual - Doesn't work on windows, once worked on my site and then stopped working altogether
Code:
	/*Get the IP address for the target host. */
	$Host = gethostbyname($Host);
	
	/*Create a TCP/IP socket.
	$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
	//$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
	if ($socket < 0)
		{
		echo "Failure";
		}
		else
		{
		echo "Socket OK.<BR>";
		}
	
	echo "Attempting to connect to '$Host' on port 

'$Port'...<BR>";
	$result = socket_connect($socket, $Host, $Port);
	if ($result < 0)
		{
		echo "socket_connect() failed.<BR>Reason: ($result) " 

. socket_strerror($result) . "<BR>";
		}
		else
		{
		echo "OK<BR>";
		$out = '';
//This is the troublesome part..
		while ($out = socket_read($socket, 2048)) 
			{
			echo $out;
			}
		}
	echo "Closing socket...";
	socket_close($socket);
	echo "OK.<BR>";
Can anyone help me?

The rest of my project depends upon this script being able to check an IP/Host/port number for a working connection.

-Turboz

Last edited by Turboz; 12-29-05 at 01:43 PM.
Reply With Quote
  #2 (permalink)  
Old 12-30-05, 03:53 PM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
i'm sorry but i can't help. this is a rather advanced script, maybe you'ld ask it better on this forum:

http://forums.devnetwork.net

there are some great programmers overthere

Greetz,
UnrealEd
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
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

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

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to make max connection limit per ip using php? Osjur PHP 7 04-01-06 12:32 AM
PHP multi-dimensional array sorting issue aqw PHP 2 06-24-05 11:09 PM
PHP Valid Login Checking phppick PHP 3 04-25-05 05:23 PM
ASP syntax error stuckonaproject ASP 8 12-14-04 10:30 AM
PHP with Oracle Connection problem !!! please Help webFani PHP 0 11-08-04 12:30 AM


All times are GMT -5. The time now is 06:24 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.