我运行server.php,然后在几次尝试从客户端发送到服务器后,在client.php屏幕上运行client.php并显示此警告。
警告:socket_connect():无法连接[10048]:通常只允许使用每个套接字地址(协议/网络地址/端口)。在第32行的C:\ xampp \ htdocs \ client.php中,套接字连接失败!
之后xampp停止工作。
这是server.php
<!DOCTYPE html>
<html>
<?php
error_reporting(0);
set_time_limit(0);
ob_implicit_flush();
$host = "127.0.0.1";
$port = 65535;
echo "Waiting for connections... \n";
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
$result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n");
$result = socket_listen($socket) or die("Could not set up socket listener\n");
while(1){
$spawn[++$i] = socket_accept($socket) or die("Could not accept incoming connection\n");
echo "\n";
$input = socket_read($spawn[$i],1024);
$client = $input;
echo $client ."\n";
socket_close($spawn[$i]);
echo "\n";
}
socket_close($socket);
?>
</html>
这是我的client.php
<!DOCTYPE html>
<html>
<body>
<form method="post" action="client.php">
<p><h4><label>Type Your Message Here:<input name = "message" size = "25" maxlength = "30" required></label></h4></p>
<input type="submit" name="sendmsg" class="btn btn-primary" value="send message"/>
</form>
<?php
$user="abc";
if(empty($_POST)){
}
elseif(isset($_POST['sendmsg'])) {
$message =$_POST["message"];
while(1){
if($message=='q') { exit; }
$socket= socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
if($socket===false)
{
echo "Socket creation failed!";
}
$result = socket_connect($socket,"127.0.0.1",65535);
if($result===false)
{
echo "Socket connection failed!";
}
else {
if($message !='0'){
socket_write($socket,"$user says --> $message",1024);
$message='0';
}
}
socket_close($socket);
}
}
?>
</body>
</html>
答案 0 :(得分:1)
您在POST
内不断创建,连接和关闭套接字,它们之间的延迟最小。您可能正在使用所有可用端口作为原始端点,因为关闭套接字需要一些时间。
您是否只想在页面{{1}}添加新邮件时才创建连接?