Solving: Problems with PHP sockets

Source: Internet
Author: User
Tags vps
The following test code, I built in the local appserv environment, execute without error. and can be connected and executed with the client.
!--? 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, the This loop can handle multiple clientswhile (1) {echo ' Waiting for data ... \ n ';    Receive some data $r = Socket_recvfrom ($sock, $buf, 0, $remote _ip, $remote _port); echo "$remote _ip: $remote _port--".         $buf; Send back the data to the client socket_sendto ($sock, "OK". $buf, 0, $remote _ip, $remote _port);} Socket_close ($sock);? -->


However, after I put it on the VPS, it shows that the port is open and cannot be opened again (the second time the script is executed, the error message is returned and the port is occupied). I scan the port found 9999 is indeed an open UDP port, but, with the client can not complete the communication (may not be able to send, perhaps not to receive, who knows ... )

The following is the result of Phpinfo, which shows that socket support is open. (Too long, the socket found after ctr+f is here)
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 '

Registered Stream Socket transportstcp, UDP, UNIX, UDG, SSL, SSLv3, SSLV2, TLS

Socketssockets supportenabled

default_socket_timeout6060


Well, which Daniel has experience, kneeling Xie Ah ~


Reply to discussion (solution)

Change a port number

Opened port, cannot be opened repeatedly

I manually changed localhost to the server IP, and then added Set_time_limit (2);

The result of execution is the same as above. The scan display port is open, but the client cannot communicate.

However, I soon saw this:

The server encountered an internal error or misconfiguration and is unable to complete your request


Change a port number

Opened port, cannot be opened repeatedly
Well, every time there's a change.

The server encountered an internal error or misconfiguration and is unable to complete your request
The server encountered an internal error or configuration error and could not complete your request

The server encountered an internal error or misconfiguration and is unable to complete your request
The server encountered an internal error or configuration error and could not complete your request

Do you have any suggestions? Like checking log, or how to try it?

Look at the firewall or something

9999 is the port that is already open yes, but do you have 9999? This port has no other programs to occupy if it is not occupied by other programs, you might consider setting up a firewall

Otherwise, switch to a new port or close the 9999-port program.
You can also change that program to a different port.

In short, a port can only be mapped to a program

Well, the last attempt was fruitless, and the firewall on the VPS couldn't move. That's it.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.