PHP Socket Communication
Posted: Mon May 26, 2008 6:20 am
I recently started working on a small project for one of my clients to develop a demonstration web page for one of their new systems.
What I need to do is connect to a socket, send a couple of commands and get a reply string. I am able to connect to the socket just fine and I'm not getting any errors. My problem is getting a respond from the system. I try to use socket_read() but don't seem to get any respond.
The problem is basically that I'm getting timeout on the socket_read command.
I get this output:
And then it just hangs up on loading and waits for the timeout.
There is the small chance that the system I'm sending the commands to actually doesn't know how to respond, but I want to make sure that my PHP script is actually right before we go into programming that is basically more time consuming and complex.
If I could get some explanation of how reading from a socket should be done, or maybe a confirmation that what I'm attempting to do is right it will speed up things.
Thanks in advance for any kind of help,
Adam Weisman.
P.S - Code is down below (ip and port are hidden...)
What I need to do is connect to a socket, send a couple of commands and get a reply string. I am able to connect to the socket just fine and I'm not getting any errors. My problem is getting a respond from the system. I try to use socket_read() but don't seem to get any respond.
The problem is basically that I'm getting timeout on the socket_read command.
I get this output:
Code: Select all
Socket Created...
Socket connected...
connect_button sent...
submit_button sent...
There is the small chance that the system I'm sending the commands to actually doesn't know how to respond, but I want to make sure that my PHP script is actually right before we go into programming that is basically more time consuming and complex.
If I could get some explanation of how reading from a socket should be done, or maybe a confirmation that what I'm attempting to do is right it will speed up things.
Thanks in advance for any kind of help,
Adam Weisman.
P.S - Code is down below (ip and port are hidden...)
Code: Select all
$ip = "xx.xx.xx.xx"; // hidden for security reasons
$port = xxxx; // hidden for security hidden
// Create socket resource
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Couldn't create socket: [$errorcode] $errormsg");
} else {
echo "Socket Created...<br />";
}
// Connect socket to remote machine
$client = socket_connect($socket,$ip,$port);
if ($client === false) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Couldn't accept socket: [$errorcode] $errormsg");
} else {
echo "Socket connected...<br />";
}
// Send connection command to remote machine
$write = socket_write($socket,"connect_button: 10.10.10.15 4460");
if ($write === false) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Couldn't write to socket: [$errorcode] $errormsg");
} else {
echo "connect_button sent...<br />";
}
// Send the request to remote machine
$write = socket_write($socket,"submit_button: 0507417411@@@I would like to buy $15 for 0548122562 today.###");
if ($write === false) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Couldn't write to socket: [$errorcode] $errormsg");
} else {
echo "submit_button sent...<br />";
}
// Read input
$input = socket_read($socket,1028);
echo $input;
// Close socket resource
socket_close($socket);