This PHP program generates an HTML form which you can use to create a TCP socket to a remote device and send data to it. This assumes the remote device accepts the socket connection on the port you choose to connect to.
Thanks to John Schimmel for writing the code.
Technorati Tags: networked objects, networks
<?php
/*
by John Schimmel, john.schimmel@gmail.com
May 9, 2005
This script will display a html form.
On submit, the form will call itself and then check to see if the form element 'text1' is set.
If set a socket is made to the define IP address and port and the value of 'text1' is sent down the pipe.
The socket then disconnects and the form is redisplayed with the value of 'text1' in the html form's textbox.
IMPORTANT: If you are using an XPORT or COBOX change the connect mode to C4
so no IP address is being concatenating with the incoming message to the pic....unless you like it that way.
*/
$xportIP = "128.122.151.44"; //IP adddress to connect to
$port = 10001; // port number of IP
if ((isset($_POST["text1"])) && (isset($_POST["ip"])) && (isset($_POST["port"]))) { //if a filled textbox was submitted
$data = $_POST["text1"];
$ip = $_POST["ip"];
$port = $_POST["port"];
$fp = fsockopen ($ip, $port, $errno, $errstr, 30); //open the socket
if (!$fp) { //if the socket does not exist
echo "$errstr ($errno)n";
} else { //if the socket exists
fputs ($fp, $data); // send the data down the hole to the other end
echo "<font size='2' color='navy'>Message Sent: " . $_POST["text1"] . "</font>"; //confirmation message
fclose ($fp); //close the socket
}
}
?>
<!-- html form below -->
<html>
<body>
<form name="message" method="post" action="phpToSocketForm.php">
IP Address: <input type="text" name="ip" value="<?= $_POST["ip"]; ?>" size='15' maxlength='15'>
Port: <input type="text" name="port" value="<?= $_POST["port"]; ?>" size=5 maxlength=5> <br>
Data: <input type="text" name="text1" value="<?= $_POST["text1"]; ?>" size="6">
<input type="submit" value="Send It">
</form>
</body>
</html>