Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial 求解:PHP socket 的问题

求解:PHP socket 的问题

Jun 23, 2016 pm 02:00 PM

下面这段测试代码,我在本地用appserv搭建的环境,执行无误。而且可以用客户端连上去并执行。

<?php //Reduce errorserror_reporting(~E_WARNING); //Create a UDP socketif(!($sock = socket_create(AF_INET, SOCK_DGRAM, 0))){    $errorcode = socket_last_error();    $errormsg = socket_strerror($errorcode);         die("Couldn't create socket: [$errorcode] $errormsg \n");} echo "Socket created \n"; // Bind the source addressif( !socket_bind($sock, "localhost" , 9999) ){    $errorcode = socket_last_error();    $errormsg = socket_strerror($errorcode);         die("Could not bind socket : [$errorcode] $errormsg \n");} echo "Socket bind OK \n"; //Do some communication, this loop can handle multiple clientswhile(1){    echo "Waiting for data ... \n";         //Receive some data    $r = socket_recvfrom($sock, $buf, 512, 0, $remote_ip, $remote_port);    echo "$remote_ip : $remote_port -- " . $buf;         //Send back the data to the client    socket_sendto($sock, "OK " . $buf , 100 , 0 , $remote_ip , $remote_port);} socket_close($sock);?>
Copy after login


但是,我放到vps上之后,它显示该端口是打开的,而且无法再次打开(第二遍执行这个脚本的时候,返回错误信息,端口已被占用)。我扫描端口发现9999确实已经是一个打开的UDP端口,但是,用客户端连得上去却没法完成通信(或许不可以发,或许不可以收,who knows。。。)

下面是phpinfo的结果,显示socket支持是打开的。(太长了,ctr+f后找到的socket都在这里了)
Configure Command	 './configure' '--prefix=/usr/local/php5' '--enable-force-cgi-redirect' '--enable-fastcgi' '--with-config-file-path=/usr/local/etc/php5/cgi' '--with-curl=/usr/local/lib' '--with-gd' '--with-gettext' '--with-jpeg-dir=/usr/local/lib' '--with-freetype-dir=/usr/local/lib' '--with-kerberos' '--with-openssl' '--with-mcrypt' '--with-mhash' '--with-mysql=/usr' '--with-mysqli=/usr/bin/mysql_config' '--with-pcre-regex=/usr/local' '--with-pdo-mysql=/usr' '--with-pear' '--with-png-dir=/usr/local/lib' '--with-xsl' '--with-zlib' '--with-zlib-dir=/usr/local/lib' '--enable-zip' '--enable-gd-native-ttf' '--with-iconv=/usr/local' '--enable-bcmath' '--enable-calendar' '--enable-ftp' '--enable-magic-quotes' '--enable-sockets' '--enable-mbstring'
Copy after login

Registered Stream Socket Transports	tcp, udp, unix, udg, ssl, sslv3, sslv2, tls
Copy after login

socketsSockets Support	enabled
Copy after login

default_socket_timeout	60	60
Copy after login


额,哪位大牛有过经验,跪谢啊~


回复讨论(解决方案)

换一个端口号

已打开的端口,就不能重复打开

我把localhost手动改成了服务器IP,然后加上了set_time_limit(2);

执行的结果和上面一样。扫描显示端口已打开,但是客户端无法通信。

不过,我很快看到了这个:

The server encountered an internal error or misconfiguration and was unable to complete your request
Copy after login


换一个端口号

已打开的端口,就不能重复打开
恩,每次都有换的。

The server encountered an internal error or misconfiguration and was unable to complete your request
服务器遇到一个内部错误或配置错误,无法完成您的请求

The server encountered an internal error or misconfiguration and was unable to complete your request
服务器遇到一个内部错误或配置错误,无法完成您的请求

有什么建议没?比如查log,或者是怎么尝试?

看看防火墙什么的

9999 是已经打开的端口没错 但是你有没有9999这个端口有没有没其它的程序所占用 如果没有被其它的程序所占用那么可能考虑设置一下防火墙

否则的话 就换一个新的端口或者关闭占用9999端口的程序
也可以把那个程序换一个其它的端口

总之一个端口只能映射给一个程序

好吧,最后尝试无果,VPS上的防火墙也没法动。就这样吧。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Framework Security Features: Protecting against vulnerabilities. Framework Security Features: Protecting against vulnerabilities. Mar 28, 2025 pm 05:11 PM

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

See all articles