Im trying to figure out the problem with a very simple client/server
program written in PHP.
Ill post the php code of the server and the client, both are based on
the annotated PHP Help.
The server will only write a message to a cliente every time one
connects to it.
Server Code:
<?
$direccion = '0.0.0.0';
$puerto = 4321;
if (($serverSocket = socket_create (AF_INET, SOCK_STREAM, 0)) < 0) {
echo socket_strerror ($serverSocket) . "\n";
}
if (($retorno = socket_bind ($serverSocket, $direccion, $puerto)) < 0)
{
echo socket_strerror ($retorno) . "\n";
}
if (($retorno = socket_listen ($serverSocket, 1)) < 0) {
echo socket_strerror ($retorno) . "\n";
}
do {
if (($cliente = socket_accept($ serverSocket)) < 0) {
echo socket_strerror ($cliente) . "\n";
break;
}
$mensaje = "bienvenido al server\n";
socket_write($c liente, $mensaje, strlen($mensaje ));
//socket_close ($msgsock);
} while (true);
socket_close ($sock);
?>
Client Code:
<?php
error_reporting (E_ALL);
echo "<h2>TCP/IP Connection</h2>\n";
$service_port = 4321;
$address = '127.0.0.1';
/* Create a TCP/IP socket. */
$socket = socket_create (AF_INET, SOCK_STREAM, 0);
if ($socket < 0) {
echo "socket_create( ) failed: reason: " . socket_strerror ($socket)
.. "\n";
} else {
echo "OK.\n";
}
echo "Attempting to connect to '$address' on port '$service_port' ...";
$result = socket_connect ($socket, $address, $service_port);
if ($result < 0) {
echo "socket_connect () failed.\nReason : ($result) " .
socket_strerror ($result) . "\n";
} else {
echo "OK.\n";
}
$out = '';
echo "Reading response:\n\n";
while ($out = socket_read ($socket, 2048)) {
echo $out;
}
echo "Closing socket...";
socket_close ($socket);
echo "OK.\n\n";
?>
Both pages are uploaded to the web, and both are in the same folder:
/server.php and /client.php. When I try to access the server page, it
stays waiting, and in a different client I try to access the client
page and it also stays waiting, without doing anything.
Anybody has any ideas what the problema is?
Thanks in advance,
Daniel Kawer
program written in PHP.
Ill post the php code of the server and the client, both are based on
the annotated PHP Help.
The server will only write a message to a cliente every time one
connects to it.
Server Code:
<?
$direccion = '0.0.0.0';
$puerto = 4321;
if (($serverSocket = socket_create (AF_INET, SOCK_STREAM, 0)) < 0) {
echo socket_strerror ($serverSocket) . "\n";
}
if (($retorno = socket_bind ($serverSocket, $direccion, $puerto)) < 0)
{
echo socket_strerror ($retorno) . "\n";
}
if (($retorno = socket_listen ($serverSocket, 1)) < 0) {
echo socket_strerror ($retorno) . "\n";
}
do {
if (($cliente = socket_accept($ serverSocket)) < 0) {
echo socket_strerror ($cliente) . "\n";
break;
}
$mensaje = "bienvenido al server\n";
socket_write($c liente, $mensaje, strlen($mensaje ));
//socket_close ($msgsock);
} while (true);
socket_close ($sock);
?>
Client Code:
<?php
error_reporting (E_ALL);
echo "<h2>TCP/IP Connection</h2>\n";
$service_port = 4321;
$address = '127.0.0.1';
/* Create a TCP/IP socket. */
$socket = socket_create (AF_INET, SOCK_STREAM, 0);
if ($socket < 0) {
echo "socket_create( ) failed: reason: " . socket_strerror ($socket)
.. "\n";
} else {
echo "OK.\n";
}
echo "Attempting to connect to '$address' on port '$service_port' ...";
$result = socket_connect ($socket, $address, $service_port);
if ($result < 0) {
echo "socket_connect () failed.\nReason : ($result) " .
socket_strerror ($result) . "\n";
} else {
echo "OK.\n";
}
$out = '';
echo "Reading response:\n\n";
while ($out = socket_read ($socket, 2048)) {
echo $out;
}
echo "Closing socket...";
socket_close ($socket);
echo "OK.\n\n";
?>
Both pages are uploaded to the web, and both are in the same folder:
/server.php and /client.php. When I try to access the server page, it
stays waiting, and in a different client I try to access the client
page and it also stays waiting, without doing anything.
Anybody has any ideas what the problema is?
Thanks in advance,
Daniel Kawer
Comment