php 绑定ip,php – 总是`无法绑定到tcp:// my_ip_here:8080地址已在使用中

我试图部署我的websocket服务器并开始运行它但总是给出:

PHP Fatal error:

Uncaught exception 'React\Socket\ConnectionException'

with message 'Could not bind to tcp://my_ip_here:8080:

Address already in use'

in /var/www/html/webscoket/vendor/react/socket/src/Server.php:29

这是我的server.php:

require dirname(__DIR__) . '/vendor/autoload.php';

use Ratchet\Server\IoServer;

use Ratchet\Http\HttpServer;

use Ratchet\WebSocket\WsServer;

use React\Socket\Server;

use React\ZMQ\Context;

$loop = React\EventLoop\Factory::create();

$app = new onyxsocket();

$webSock = new Server($loop);

$webSock->listen(8080, 'my_ip_here');

$webServer = new IoServer(

new HttpServer(

new WsServer(

$app

)

),

$webSock

);

$context = new Context($loop);

$pull = $context->getSocket(ZMQ::SOCKET_PULL);

$pull->bind('tcp://my_ip_here:5555');

$pull->on('error', function ($e) {

var_dump($e->getMessage());

});

$pull->on('message', array($app, 'onbroadcast'));

$loop->run();

到目前为止我所尝试的是检查可以在生产服务器中使用的可用端口:netstat – anp让我知道端口8080是免费的.但问题是它仍然显示已经使用的错误地址.我也尝试过管理员提供的其他端口,但没有运气.

我正在尝试部署的server.php在localhost上工作正常.但我不知道我需要做什么才能使它在生产服务器上运行.

需要帮忙.谢谢.

解决方法:

来自@ user3666197评论如上:

Clarify the code, pls. Your server-code ZeroMQ .bind() -s to port# 5555. So whose code binds to localhost port# 8080, that is reported in an unhandled exception above? How do you clean for a gracefull-exit any crashed EventLoop/Factory to release resources and avoid hanging orphans?

我决定重新检查netstat:netstat -tulpen并检查端口8080和5555,并发现当前连接的端口上存在已注册的PID.这些应用程序也是我想在控制台server.php上运行的相同脚本.

我杀了PID:kill PID_number并再次在控制台上运行server.php.有效.

标签:php,zeromq,sockets,ratchet

来源: https://codeday.me/bug/20190528/1172804.html

当然可以!一个基本的例子是使用socket库编写TCP客户端/服务器程序,用于建立网络连接和传输数据。例如,使用C语言编写一个简单的TCP服务器程序,可以使用以下代码作为起点: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> int main(int argc, char *argv[]) { int sockfd, newsockfd, portno; socklen_t clilen; char buffer[256]; struct sockaddr_in serv_addr, cli_addr; int n; // 创建TCP套接字 sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) error("ERROR opening socket"); // 初始化服务器地址结构 bzero((char *) &serv_addr, sizeof(serv_addr)); portno = 8080; serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = INADDR_ANY; serv_addr.sin_port = htons(portno); // 绑定套接字到本地地址 if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) error("ERROR on binding"); // 监听连接请求 listen(sockfd,5); clilen = sizeof(cli_addr); // 接受连接请求,创建新的套接字 newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); if (newsockfd < 0) error("ERROR on accept"); // 循环接收和发送消息 while(1) { bzero(buffer,256); n = read(newsockfd,buffer,255); if (n < 0) error("ERROR reading from socket"); printf("Here is the message: %s\n",buffer); n = write(newsockfd,"I got your message",18); if (n < 0) error("ERROR writing to socket"); } return 0; } ``` 这个简单的TCP服务器程序会监听本地8080端口,并且在接收到客户端连接请求后,接收并回复客户端发送的消息。你可以根据自己的需求,修改代码以适应不同的TCP/IP应用场景。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值