par
Olivierd » 20 mai 2005, 12:52
Bon voila, j'a un petit souci.
Je voudrais envoye et recevoir, a la maniere d'un server, des données a une machine x.
Les sockets ca doit pouvoir permettre de la faire. Mais je debute : je vais voir dans les manuels, je trouve ce script:
<?php
error_reporting (E_ALL);
/* Allow the script to hang around waiting for connections. */
set_time_limit (0);
/* Turn on implicit output flushing so we see what we're getting
* as it comes in. */
ob_implicit_flush ();
$address = '127.0.0.1';
$port = 80;
if (($sock = socket_create(AF_INET, SOCK_STREAM,SOL_TCP)) < 0)
{
echo "socket_create() failed: reason: " . socket_strerror($sock) . "\n";
}
if (($ret = socket_bind($sock,address ,$port )) < 0)
{
echo "socket_bind() failed: reason: " . socket_strerror($ret) . "\n";
}
if (($ret = socket_listen($sock, 10)) === false)
{
echo "socket_listen() failed: reason: " . socket_strerror($ret) . "\n";
}
do
{
if (($msgsock = socket_accept($sock)) === false)
{
echo "socket_accept() failed: reason: " . socket_strerror($msgsock) . "\n";
break;
}
/* Send instructions. */
$msg = "\n Welcome to the PHP Test Server. \n\r" .
"To quit, type 'quit'. To shut down the server type 'shutdown'.\n\r";
socket_write($msgsock, $msg, strlen($msg));
$cmd = "";
do {
if (false === ($buf = socket_read($msgsock, 4096)))
{
echo "socket_read() failed: reason: " . socket_strerror($ret) . "\n";
break 2;
}
if ($buf == "\r\n")
{
if ($cmd == 'm00')
{
socket_write($msgsock, "PHP replies: Must be a cow out there...\n\r", strlen ("PHP replies: Must be a cow out there... "));
}
if ($cmd == 'quit')
{
socket_write($msgsock, "\n\rClosing connection ...", strlen (" Closing connection ..."));
break;
}
if ($cmd == 'shutdown')
{
socket_write($msgsock, "\n\rShutdown initiated...", strlen (" Shutdown initiated..."));
socket_close ($msgsock);
break 2;
}
$cmd = "";
}
else
{
$cmd .= $buf;
}
} while (true);
socket_close ($msgsock);
} while (true);
socket_close ($sock);
?>
comment le configurer de telle maniere qu'il marche :
lorsque je lance ce code, l'erreur suivante apparait
Warning: socket_bind() unable to bind address [0]: Only one usage of each socket address (protocol/network address/port) is normally permitted. in c:\program files\easyphp1-8\www\index.php on line 16
socket_bind() failed: reason: The operation completed successfully.
Warning: socket_listen() unable to listen on socket [0]: An invalid argument was supplied. in c:\program files\easyphp1-8\www\index.php on line 21
socket_listen() failed: reason: The operation completed successfully.
Warning: socket_accept() unable to accept incoming connection [0]: An invalid argument was supplied. in c:\program files\easyphp1-8\www\index.php on line 27
Warning: socket_accept() unable to accept socket connection [0]: The operation completed successfully. in c:\program files\easyphp1-8\www\index.php on line 27
socket_accept() failed: reason: The operation completed successfully.
evidemment, je m'y prend peut etre mal, voire tres mal. Mais excuser ma debutante attitude.
Merci de m'aider
Bon voila, j'a un petit souci.
Je voudrais envoye et recevoir, a la maniere d'un server, des données a une machine x.
Les sockets ca doit pouvoir permettre de la faire. Mais je debute : je vais voir dans les manuels, je trouve ce script:
<?php
error_reporting (E_ALL);
/* Allow the script to hang around waiting for connections. */
set_time_limit (0);
/* Turn on implicit output flushing so we see what we're getting
* as it comes in. */
ob_implicit_flush ();
$address = '127.0.0.1';
$port = 80;
if (($sock = socket_create(AF_INET, SOCK_STREAM,SOL_TCP)) < 0)
{
echo "socket_create() failed: reason: " . socket_strerror($sock) . "\n";
}
if (($ret = socket_bind($sock,address ,$port )) < 0)
{
echo "socket_bind() failed: reason: " . socket_strerror($ret) . "\n";
}
if (($ret = socket_listen($sock, 10)) === false)
{
echo "socket_listen() failed: reason: " . socket_strerror($ret) . "\n";
}
do
{
if (($msgsock = socket_accept($sock)) === false)
{
echo "socket_accept() failed: reason: " . socket_strerror($msgsock) . "\n";
break;
}
/* Send instructions. */
$msg = "\n Welcome to the PHP Test Server. \n\r" .
"To quit, type 'quit'. To shut down the server type 'shutdown'.\n\r";
socket_write($msgsock, $msg, strlen($msg));
$cmd = "";
do {
if (false === ($buf = socket_read($msgsock, 4096)))
{
echo "socket_read() failed: reason: " . socket_strerror($ret) . "\n";
break 2;
}
if ($buf == "\r\n")
{
if ($cmd == 'm00')
{
socket_write($msgsock, "PHP replies: Must be a cow out there...\n\r", strlen ("PHP replies: Must be a cow out there... "));
}
if ($cmd == 'quit')
{
socket_write($msgsock, "\n\rClosing connection ...", strlen (" Closing connection ..."));
break;
}
if ($cmd == 'shutdown')
{
socket_write($msgsock, "\n\rShutdown initiated...", strlen (" Shutdown initiated..."));
socket_close ($msgsock);
break 2;
}
$cmd = "";
}
else
{
$cmd .= $buf;
}
} while (true);
socket_close ($msgsock);
} while (true);
socket_close ($sock);
?>
comment le configurer de telle maniere qu'il marche :
lorsque je lance ce code, l'erreur suivante apparait
Warning: socket_bind() unable to bind address [0]: Only one usage of each socket address (protocol/network address/port) is normally permitted. in c:\program files\easyphp1-8\www\index.php on line 16
socket_bind() failed: reason: The operation completed successfully.
Warning: socket_listen() unable to listen on socket [0]: An invalid argument was supplied. in c:\program files\easyphp1-8\www\index.php on line 21
socket_listen() failed: reason: The operation completed successfully.
Warning: socket_accept() unable to accept incoming connection [0]: An invalid argument was supplied. in c:\program files\easyphp1-8\www\index.php on line 27
Warning: socket_accept() unable to accept socket connection [0]: The operation completed successfully. in c:\program files\easyphp1-8\www\index.php on line 27
socket_accept() failed: reason: The operation completed successfully.
evidemment, je m'y prend peut etre mal, voire tres mal. Mais excuser ma debutante attitude.
Merci de m'aider