简体   繁体   English

为什么尝试建立网络套接字时连接被拒绝?

[英]Why is connection refused when trying to establish a web-socket?

I'm attempting to set up a web-socket connection between a java script client and a php server.我正在尝试在 java 脚本客户端和 php 服务器之间建立 Web 套接字连接。 However, whenever I try to connect I get the error "connection refused".但是,每当我尝试连接时,都会收到错误“连接被拒绝”。

I am already able to create a web-socket connection between two php scripts which makes me think the server side code is correct.我已经能够在两个 php 脚本之间创建 web-socket 连接,这让我认为服务器端代码是正确的。

Please explain how I can fix this problem.请解释我如何解决这个问题。

Server (PHP):服务器(PHP):

$host = "108.167.140.91";
$port = 5353;
set_time_limit(30);
// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
// bind socket to port
$result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n");
// start listening for connections
$result = socket_listen($socket, 20);

// accept incoming connections
// spawn another socket to handle communication
$spawn = socket_accept($socket) or die("Could not accept incoming connection\n");

socket_write($spawn, "connected", strlen ("connected"));

// close sockets
socket_close($spawn);
socket_close($socket);

Client (HTML):客户端(HTML):

<html>
<script>
var socket = new WebSocket("ws://108.167.140.91:5353");

// Open the socket
socket.onopen = function(event) {
    console.log("connected");

    // To close the socket....
    socket.close()

};
</script>
</html>

You can test it for yourselves here:你可以在这里自己测试:

Server: http://chrislanggames.site/SocketTest/server3.php服务器: http://chrislanggames.site/SocketTest/server3.php

Client: http://chrislanggames.site/SocketTest/client2.html客户端: http://chrislanggames.site/SocketTest/client2.html

Working PHP client: http://chrislanggames.site/SocketTest/client.php工作 PHP 客户端: http://chrislanggames.site/SocketTest/client.php

Try changing the host to "0.0.0.0".... that means it can accept socket connections requests from any host.尝试将主机更改为“0.0.0.0”......这意味着它可以接受来自任何主机的套接字连接请求。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Django 尝试创建 Web 套接字连接时出现通道错误 ValueError:找不到路径“”的路由 - Django Channels error when trying to create a web-socket connection ValueError: No route found for path ' ' 为什么消费者.py中的事件处理程序function被多次调用,然后web-socket连接被强制终止? - Why an event handler function in consumers.py is called multiple times and then web-socket connection is killed forcefully? 通过网络套接字接收数据时 Blob 获取的问题 - Problems with Blob acquisition when receiving data through a web-socket Socket.io一直在尝试建立连接 - Socket.io keeps trying to establish a connection Web套接字可用于约8KB的小文件。 但是对于〜50KB的文件,我的连接断开了 - Web-socket worked for small files of ~8KB. But for ~50KB file, my connection disconnects 是否可以识别收到的网络套接字请求? - Is it possible to identify web-socket request on receive? 无法建立套接字连接 - Unable to establish a socket connection 当我的互联网连接不稳定时(对于消息传递应用程序),我应该使用网络套接字吗? - Should I use web-socket when my internet connectivity is unstable (for messaging app)? 客户端站点上的Heroku服务器Web套接字连接拒绝错误 - Heroku server web socket connection refused error on client site 如何在没有 web-socket 的情况下从服务器监听事件 - How to listen the event from server without web-socket
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM