Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

No response to socket

Status
Not open for further replies.

chals1

Technical User
Sep 3, 2003
73
ES
I have this code to get a temperature message from a server connected into a temperature sensor, but there isn't any response

Code:
<html>
<head>
<style type="text/css">
input {
 background-color: #000000;
 color: #ffffff;
}

#box1{
position: absolute;
width: 250px;
border: solid #000000 3px;
background-color: #ffffff;
color: #000000;
padding: 10px;
}

</style>

</head>

<body>

<?
// form not yet submitted
if (!$submit)
{
?>
<div id="box1">
<form action="<? echo $_SERVER['PHP_SELF']; ?>" method="post">
<b>Sensor de temperatura</b><br>
<input type="Text" name="message" size="15"><input type="submit"
name="submit" value="Enviar">
</form>
</div>
<?
}
else
{
	// form submitted

	// where is the socket server?
	$host="158.42.58.43";
	$port = 10001;

	// open a client connection
	$fp = fsockopen ($host, $port, $errno, $errstr);

	if (!$fp)
	{
      $result = "Error: could not open socket connection";
	}
	else
	{
  // get the welcome message
     fgets ($fp, 1024);
  // write the user string to the socket
     fputs ($fp, $message);
  // get the result
     $result .= fgets ($fp, 1024);
     // close the connection
  fputs ($fp, "END");
     fclose ($fp);

  // trim the result and remove the starting ?
  $result = trim($result);
  $result = substr($result, 2);

  // now print it to the browser
	}
?>
Server said: <b><? echo $result; ?></b>
<?
}
?>

</body>
</html>
 
What is the error message if there's any?
What about setting a timeout?
Does the server actually respond at that port?
 
Let's check some assumptions:
I see that you use $submit, which indicates to me that you think register_globals is ON. Check out if that is true or not with phpinfo();
I suggest you use $_POST['submit'] instead.

I did not see any error messages, just no outcome. If that's because the socket failed or not can't be assessed from that.
 
Can you specify on which line i must replace submit.I'm very newbie
 
Code:
// form not yet submitted
if (!$submit)
## should be
// form not yet submitted
if (!$_POST['submit'])

Let's see if that helps.
 
WHat does your code look like now? The error is a file permission error. Pls. post.
 
I checked just now and see that your connection is not being established. Again, it might sound like a stupid question, but does your host 158.42.58.43 accept connections at port 10001?
 
It looks like the sensor is off right now
Until monday i won't know your answer but think the port is right;anyhow the script seems to work if coul establish connection
I'll inform you next week

many thanks!!
 
The advice you follow is "phony". All the "@" sign does in front of a PHP command is to suppress the error message generated if the command fails. It will make it look as if it works - but it really won't.
I'm still not sure if your target machine responds on port 10001 - I've tried telnetting and it won't respond. The machine even doesn't answer a ping.
 
I've a new ip
$host="158.42.52.43";
$port = 10001;
and have put an echo to see $message but it's empty
 
I used the following code and tried to connect, but the server connection times out:
Code:
$host="158.42.58.43";
$port = 10001;

// open a client connection
$fp = fsockopen ($host, $port, $errno, $errstr,10);
echo($errstr);
 
Could you try this code?
Code:
<body>

<?
// form not yet submitted
if (!$_POST['submit'])
{
?>
<div id="box1">
<fieldset class="fieldseta">
<legend>
<b>Sensor de temperatura</b>
</legend>
<form action="<? echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="text" name="message" class="texta">
<input type="submit" name="submit" class="submita" value="Enviar">
</form>
</fieldset>
</div>
<?
}
else
{
	// form submitted

	// where is the socket server?
	$host="158.42.52.43";
	$port = 10001;

	// open a client connection
	$fp = fsockopen ($host, $port, $errno, $errstr);

	if (!$fp)
	{
      $result = "Error: could not open socket connection";
	}
	else
	{
  // get the welcome message
  // fgets ($fp, 1024);
  // write the user string to the socket
     echo ("Enviando el msg ");
     echo $message;
     fputs ($fp, $message);
  // get the result
     $result .= fgets ($fp, 1024);
     // close the connection
  fputs ($fp, "END");
     fclose ($fp);

  // trim the result and remove the starting ?
  echo ("El resultado es: ");
  echo $result;
  $result = trim($result);
  $result = substr($result, 2);

  // now print it to the browser
	}
?>
Server said: <b><? echo $result; ?></b>
<?
}
?>

</body>
</html>
 
I'm sorry.The full code:

Code:
<html>
<head>
<style type="text/css">
#box1{
position: absolute;
width: 250px;
}

.texta {
 font-size: 10px;
 background-color: #CCCCCC;
 border: 1px solid #666666;
}

.fieldseta {
 border: 2px solid #000000;
 padding: 10px;
}

.submita {
 font-size: 10px;
 width: 60px;
 text-align: center;
 padding: 0px;
}

</style>

</head>

<body>

<?
// form not yet submitted
if (!$_POST['submit'])
{
?>
<div id="box1">
<fieldset class="fieldseta">
<legend>
<b>Sensor de temperatura</b>
</legend>
<form action="<? echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="text" name="message" class="texta">
<input type="submit" name="submit" class="submita" value="Enviar">
</form>
</fieldset>
</div>
<?
}
else
{
	// form submitted

	// where is the socket server?
	$host="158.42.52.43";
	$port = 10001;

	// open a client connection
	$fp = fsockopen ($host, $port, $errno, $errstr);

	if (!$fp)
	{
      $result = "Error: could not open socket connection";
	}
	else
	{
  // get the welcome message
  // fgets ($fp, 1024);
  // write the user string to the socket
     echo ("Enviando el msg ");
     echo $message;
     fputs ($fp, $message);
  // get the result
     $result .= fgets ($fp, 1024);
     // close the connection
  fputs ($fp, "END");
     fclose ($fp);

  // trim the result and remove the starting ?
  echo ("El resultado es: ");
  echo $result;
  $result = trim($result);
  $result = substr($result, 2);

  // now print it to the browser
	}
?>
Server said: <b><? echo $result; ?></b>
<?
}
?>

</body>
</html>
 
I have put an echo to see $message but it's empty;so there couldn't be any response since nothing seems be sent
Some $_POST problem?
 
You need to use the superglobal array $_POST to refer to the posted values. I assume from the described behavior that the setting of register_globals is OFF. That's good so.
Use $_POST['message'] instead of $message
However, that doesn't have anything to do with the server not responding to the socket connection. Again, I believe that it is the receiving server that's the problem here.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top