Need help with sockets
Posted: Tue May 23, 2006 11:11 am
Hello, I am having a problem with sockets and would like some advice on what I may be doing wrong.
I have the following small test function, which I found in one of the examples and I have modified it to perform my testing. The problem I am facing is that it appears the socket is being opened and connected and the data is being sent but the script hangs on the socket_read() and never returns.
The test code is;
So when I run this the results I get are :
TCP/IP Connection
Host ip 'IP HIDDEN FOR SECURITY'
OK Socket Created.
Attempting to connect to 'IP HIDDEN FOR SECURITY' on port '5000'...
OK Socket is connected.
Peer Name 'IP HIDDEN FOR SECURITY
Sending message to server
Ok written to socket '37'
Reading response:
But the script never returns from the read. Now I know the server is there and I can connect and send and receive messages using a C program. So I don't understand why the read is never returning.
Could someone help in respect to this. I sure would appreciate it.
Thanks very much.
I have the following small test function, which I found in one of the examples and I have modified it to perform my testing. The problem I am facing is that it appears the socket is being opened and connected and the data is being sent but the script hangs on the socket_read() and never returns.
The test code is;
Code: Select all
function SocketConnectTest()
{
error_reporting(E_ALL);
echo "TCP/IP Connection<br>";
/* Define Port to use */
$service_port = "5000" ;
/* Define IP to use */
$address = 'IP HIDDEN FOR SECURITY';
echo "Host ip '$address'<br>";
/* Create a TCP/IP socket. */
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket < 0) {
echo "socket_create() failed: reason: " . socket_strerror($socket) . "\n";
} else {
echo "OK Socket Created.<br>";
}
echo "Attempting to connect to '$address' on port '$service_port'...<br>";
$result = socket_connect($socket, $address, $service_port);
if ($result < 0) {
echo "socket_connect() failed.\nReason: ($result) " . socket_strerror($result) . "<br>";
} else {
echo "OK Socket is connected.<br>";
}
socket_getpeername($socket, &$connectip);
echo "Peer Name $connectip<br>";
$in = "MESSAGE I SEND" ;
$out = '';
echo "Sending message to server<br>";
$Status = socket_write($socket, $in, strlen($in));
if ( $Status === FALSE ) {
echo "socket_write() failed.\nReason: ($Status) " . socket_strerror($Status) . "\n";
} else {
echo "Ok written to socket '$Status'<br>";
}
echo "Reading response:<br>";
while ($out = socket_read($socket,100)) {
echo $out;
}
echo "Closing socket...<br>";
socket_close($socket);
echo "OK socket closed<br>";
}So when I run this the results I get are :
TCP/IP Connection
Host ip 'IP HIDDEN FOR SECURITY'
OK Socket Created.
Attempting to connect to 'IP HIDDEN FOR SECURITY' on port '5000'...
OK Socket is connected.
Peer Name 'IP HIDDEN FOR SECURITY
Sending message to server
Ok written to socket '37'
Reading response:
But the script never returns from the read. Now I know the server is there and I can connect and send and receive messages using a C program. So I don't understand why the read is never returning.
Could someone help in respect to this. I sure would appreciate it.
Thanks very much.