Linux socket_bind() 无法绑定地址

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15041518/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 19:06:40  来源:igfitidea点击:

socket_bind() unable to bind address

phplinuxwebserverwebsocket

提问by Phorce

I'm trying to create a Web Socket Server for a small project I am working on.

我正在尝试为我正在处理的一个小项目创建一个 Web 套接字服务器。

I have set up the server (located at home) and have port forwarded 80 and 22. I read this tutorial: here

我已经设置了服务器(位于家里)并转发了 80 和 22 端口。我阅读了本教程:here

And whenever I ssh into my server to run "startDarmon.php" I get the following error:

每当我通过 ssh 进入我的服务器以运行“startDarmon.php”时,我都会收到以下错误:

PHP Warning:  socket_bind(): unable to bind address [98]: Address already in use in  
/var/www/server/socket.class.php on line 48
2013-02-23 14:15:38 System: Socket bound to localhost:8000.
2013-02-23 14:15:38 System: Start listening on Socket

This is what i think is preventing my client from connecting to the server. So in the startDarmon.php file I have:

这就是我认为阻止我的客户端连接到服务器的原因。所以在 startDarmon.php 文件中我有:

$WebSocket = new socketWebSocket('MY_IP_NOT_LAN_IP',8000);

And inside my client file, I have:

在我的客户端文件中,我有:

var host = "ws://MY_IP_NOT_LAN:8000/server/startDaemon.php";

Does anyone have any suggestions to why this is not allowing me to establish a connection?

有没有人对为什么这不允许我建立连接有任何建议?

采纳答案by Basile Starynkevitch

I'm guessing the issue is on the Linux server.

我猜问题出在 Linux 服务器上。

Run the netstat -a -p -ncommand under root (e.g. with sudo) to understand which process is using that port. Then perhaps do a setsockopt(2)with SO_REUSEADDR(see socket(7)for more).

netstat -a -p -n在 root 下运行命令(例如 with sudo)以了解哪个进程正在使用该端口。然后也许做一个setsockopt(2)with SO_REUSEADDR(更多信息请参见socket(7))。

The TCP protocol has some specified delays in minutes (eg keepalive, etc etc...). See e.g. tcp(7)

TCP 协议有一些指定的以分钟为单位的延迟(例如 keepalive 等...)。参见例如tcp(7)

I do suggest reading a good book on Linux system programming like Advanced Linux Programmingand perhaps some material on network programming.

我确实建议阅读一本关于 Linux 系统编程的好书,比如Advanced Linux Programming以及一些关于网络编程的材料。

回答by ekrembk

You can reuse an address using the setting below:

您可以使用以下设置重复使用地址:

if ( ! socket_set_option($sock, SOL_SOCKET, SO_REUSEADDR, 1)) 
{ 
    echo socket_strerror(socket_last_error($sock)); 
    exit; 
}

回答by BlackHyman

Another application is already using the port that you are trying to use.You can run

另一个应用程序已经在使用您尝试使用的端口。您可以运行

lsof -i:port_no

to make the port free.

使端口空闲。