unable to connect to tcp://0.0.0.0:2346(Address already in use)

yaxiaomu

http_server_test.php 代码

use Workerman\Worker;
require_once __DIR__ . '/Autoloader.php';

// Worker::$daemonize = true;
// 创建一个Worker监听2345端口,使用http协议通讯
$http_worker2 = new Worker("http://0.0.0.0:2346");

$http_worker2->name='25k_step2222';

$http_worker2->user='root';

$http_worker2->count = 3;

$http_worker2->onMessage = function($connection, $data)
{
    // 向浏览器发送hello world
    $connection->send('hello world from worker2');
};

// 启动4个进程对外提供服务
$http_worker1 = new Worker("http://0.0.0.0:2345");
$http_worker1->count = 5;
$http_worker1->name='25k_step1';
$http_worker1->user='www-data';
// 接收到浏览器发送的数据时回复hello world给浏览器
$http_worker1->onMessage = function($connection, $data)
{
    // 向浏览器发送hello world
    $connection->send('hello world from worker 1');
};

// 运行worker
Worker::runAll();

非deamon模式,PHP 7.0.18-0ubuntu0.16.04.1 (cli) ( NTS )
以下为自己打印的日志
oot@ubuntu:/home/tb/wwwshare/Workerman-master# php http_test.php start

init 也走一次
状态收集文件在/tmp/_home_tb_wwwshare_Workerman-master_http_test.php.status
日志文件在/home/tb/wwwshare/Workerman-master/../workerman.log
pid文件在=/home/tb/wwwshare/Workerman-master/../_home_tb_wwwshare_Workerman-master_http_test.php.pid
当前pid1646
当前所有worker实例{"000000001f58c239000000000dddedce":{"id":0,"name":"25k_step2222","count":3,"user":"root","group":"","reloadable":true,"reusePort":false,"onWorkerStart":null,"onConnect":null,"onMessage":{},"onClose":null,"onError":null,"onBufferFull":null,"onBufferDrain":null,"onWorkerStop":null,"onWorkerReload":null,"transport":"tcp","connections":[],"protocol":null,"workerId":"000000001f58c239000000000dddedce"},"000000001f58c23b000000000dddedce":{"id":0,"name":"25k_step1","count":5,"user":"www-data","group":"","reloadable":true,"reusePort":false,"onWorkerStart":null,"onConnect":null,"onMessage":{},"onClose":null,"onError":null,"onBufferFull":null,"onBufferDrain":null,"onWorkerStop":null,"onWorkerReload":null,"transport":"tcp","connections":[],"protocol":null,"workerId":"000000001f58c23b000000000dddedce"}}
initd只走一次,不管new了多少个woker
timer_init pcntl_signal,— 为进程: 1646安装一个sigalrm信号
pcntl_signal(SIGALRM 完毕
我启动 Workerman start 用 DEBUG 模式
先获取下进程pid1634
我启动时没有用了-d模式启动,那这句话会输出
没有设置demoniz,直接返回2-1
workername: 25k_step2222 changelength: 12
socketname: http://0.0.0.0:2346 changemaxlength: 19
当前用户id: 0 当前进程执行用户 {"name":"root","passwd":"x","uid":0,"gid":0,"gecos":"root","dir":"\/root","shell":"\/bin\/bash"}人为设置的当前进程用户: root
没有reuseport,调用listen 在initWorkers
listen begin================================================listen begin
transport: tcp scheme: http address : //0.0.0.0:2346
当前scheme http不在{"tcp":"tcp","udp":"udp","unix":"unix","ssl":"tcp"}那默认transport全都是:tcp
this的protocal设置为 :\Protocols\Http
\Protocols\Http这个类不存在,我需要再设置一下
this的protocal设置又被设置为 :\Workerman\Protocols\Http
所以到现在我的local_socket为: tcp://0.0.0.0:2346
PHP Warning: stream_socket_server(): unable to connect to tcp://0.0.0.0:2346 (Address already in use) in /home/tb/wwwshare/Workerman-master/Worker.php on line 2440

root@ubuntu:/home/tb/wwwshare/Workerman-master# netstat -anp |grep 2345
tcp        0      0 0.0.0.0:2345            0.0.0.0:*               LISTEN      1635/0.0.0.0:2346
root@ubuntu:/home/tb/wwwshare/Workerman-master# netstat -anp |grep 2346
tcp        0      0 0.0.0.0:2345            0.0.0.0:*               LISTEN      1635/0.0.0.0:2346
tcp        0      0 0.0.0.0:2346            0.0.0.0:*               LISTEN      1635/0.0.0.0:2346
unix  3               STREAM     CONNECTED     18804    1635/0.0.0.0:2346   
unix  3               STREAM     CONNECTED     18805    1635/0.0.0.0:2346   
root@ubuntu:/home/tb/wwwshare/Workerman-master# 

oot@ubuntu:/home/tb/wwwshare# kill 1635
root@ubuntu:/home/tb/wwwshare#  对进程1635发送了SIGTERM 信号(平滑 重启)

已经看了手册 :http://doc3.workerman.net/faq/workerman-start-fai
如果执行了reload 或者restart 也不行,删除pid文件也不行,killallphp 也不行 kill pid内的进程已经无效,关掉当前会话窗口也不行。。求教。。出现这个原因是啥?生产环境也有类似的问题。

最后解决办法是用top,发现一个php占用内存达到70%以上,kill 掉之后,一切正常。

7832 2 0
2个回答

walkor 打赏

unable to connect to tcp://0.0.0.0:2346(Address already in use) 就是端口被占用了。

netstat -anp |grep 2346 找到对应进程,看下是什么进程,
利用命令 ps auxf 看下进程关系是什么,是谁创建的这个进程。
如果确实是没用的进程,kill -9就杀死了。

  • 暂无评论
workerstar

要么端口已被其他程序占用,更换端口;要么就是有多个start_*.php文件,多个文件都用一个端口自然报错。

  • suanboy 2022-01-25

    肯定有个tcp服务端已经在跑了,找到这个进程,把进程结束掉,就能正常启动了

年代过于久远,无法发表回答
×
🔝