470,862 Members | 1,701 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,862 developers. It's quick & easy.

Cliente/Server with PHP

Dan
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($cliente, $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

Jul 17 '05 #1
1 1624
"Dan" <dk****@gmail.com> wrote in message
news:10*********************@k17g2000odb.googlegro ups.com...
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


session.auto_start is on?
Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Nathan Sokalski | last post: by
14 posts views Thread by Developer | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.