初学,从晚上找的例子:
1、为啥client.php只能连一次啊,第二次就不行了。
2、怎么传递数据
3、麻烦解释一下几行的意思!
client.php
<html>
<title>socket_client </title>
<body>
Connect...
<?php
$commenProtocol = getprotobyname("tcp");
$client_socket = socket_create(AF_INET,SOCK_STREAM,0);
if(socket_connect($client_socket,'127.0.0.1',888)){
$buff = ""; //这行啥意思?buff?
echo "connect successful";
socket_recv($client_socket,$buff,10,0); //这行啥意思?
echo $buff;
}
else{
echo "can not connect";
}
?>
</body>
</html> server.php<html>
<?php
set_time_limit(0);
$commenProtocol = getprotobyname("tcp");
$listen_socket = socket_create(AF_INET,SOCK_STREAM,0) or die("Could not create");
socket_bind($listen_socket,"127.0.0.1",888) or die("Could not bind");
socket_listen($listen_socket,3) or die("Could not listen"); //这行啥意思?
echo "listening";
$accept_socket = socket_accept($listen_socket); //这行啥意思?
echo "one connection";
printf("One connection");
socket_write($accept_socket,"helloworld",10); //这行啥意思?
echo "<br>";
if($accept_socket){ //这行啥意思?
echo "Successful";
}
?>
</html>
1、为啥client.php只能连一次啊,第二次就不行了。
2、怎么传递数据
3、麻烦解释一下几行的意思!
client.php
<html>
<title>socket_client </title>
<body>
Connect...
<?php
$commenProtocol = getprotobyname("tcp");
$client_socket = socket_create(AF_INET,SOCK_STREAM,0);
if(socket_connect($client_socket,'127.0.0.1',888)){
$buff = ""; //这行啥意思?buff?
echo "connect successful";
socket_recv($client_socket,$buff,10,0); //这行啥意思?
echo $buff;
}
else{
echo "can not connect";
}
?>
</body>
</html> server.php<html>
<?php
set_time_limit(0);
$commenProtocol = getprotobyname("tcp");
$listen_socket = socket_create(AF_INET,SOCK_STREAM,0) or die("Could not create");
socket_bind($listen_socket,"127.0.0.1",888) or die("Could not bind");
socket_listen($listen_socket,3) or die("Could not listen"); //这行啥意思?
echo "listening";
$accept_socket = socket_accept($listen_socket); //这行啥意思?
echo "one connection";
printf("One connection");
socket_write($accept_socket,"helloworld",10); //这行啥意思?
echo "<br>";
if($accept_socket){ //这行啥意思?
echo "Successful";
}
?>
</html>
当客户端连接到上来后,服务器端向连接写放helloworld,然后就退出了<html>
<title>socket_client </title>
<body> Connect...
<?php
$commenProtocol = getprotobyname("tcp");
$client_socket = socket_create(AF_INET,SOCK_STREAM,0);
if(socket_connect($client_socket,'127.0.0.1',888))
{
$buff = ""; //这行啥意思?buff? 就是buffer
echo "connect successful";
socket_recv($client_socket,$buff,10,0); //这行啥意思? //从连接读取数据
echo $buff;
}
else
{
echo "can not connect";
}
?>
</body> </html>
<html>
<?php
set_time_limit(0);
$commenProtocol = getprotobyname("tcp");
$listen_socket = socket_create(AF_INET,SOCK_STREAM,0) or die("Could not create");
socket_bind($listen_socket,"127.0.0.1",888) or die("Could not bind");
socket_listen($listen_socket,3) or die("Could not listen"); //这行啥意思? 开始监听
echo "listening";
$accept_socket = socket_accept($listen_socket); //这行啥意思? 等待客户端连接
echo "one connection";
printf("One connection");
socket_write($accept_socket,"helloworld",10); //这行啥意思? 向连接写入helloworld
echo "<br>";
if($accept_socket){ //这行啥意思? socket_accept函数,是否成功
echo "Successful";
}
?>
</html>
????那第二个再连怎么办?