PHP client socket connection to the EXE server

Source: Internet
Author: User
Tags aliyun
PHP client socket connection EXE Server related issues PHP runtime environment: Windows Server 2012 R2 VL (Aliyun-QingDao) + PHP5.4.44 + MYSQL 5.6 + IIS8.5 + URL Rewrite 2.0 + FastCGI
Server EXE running environment: Windows Server 2012 R2 VL (Aliyun-QingDao) + e language 5.3
function send($in){try{$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array("sec" => 5, "usec" => 0));socket_set_option($socket, SOL_SOCKET, SO_SNDTIMEO, array("sec" => 180, "usec" => 0));$result = socket_connect($socket, '127.0.0.1', 9999);$out = "";socket_write($socket, $in, strlen($in));$out = socket_read($socket, 10240);socket_close($socket);return $out;}catch(Exception $e){exit ("《script》alert('ERR::0');《script》");}}

This is my code. it is used normally. if a visitor opens this page, and another visitor opens this page, the second user will report a 500 Server Error. after some great posts, I found that the connection to the server failed, but my server EXE does not limit the number of connections, and how can I connect the first to the second? Is a PHP-CGI.EXE process capable of initiating only one Socket connection at the same time?
Supplement: The first user opens the webpage and initiates a Socket, but the connection is not closed after the socket is not processed. The second user accesses the page and creates a Socket. an error is returned when the second user fails to connect.
All errors are indicated in the row where the socket_connect statement is located.


Reply to discussion (solution)

How are you started as a server program?

Since you already have exception handling, you should not hide the exception information.
The error message should also be provided when an error occurs.
In this way, we can determine the cause of the error. are you right?

You can echo your exception information, or use socket_strerror (socket_last_error ($ socket) to output the socket-generated error.

If the server-side program is exe, double-click it. I didn't make it a system service... The error message returned by PHP is unexpected error on socket_connect at line 5. Connection failed, all of them are unexpected... Where should I put socket_strerror (socket_last_error ($ socket?

How are you started as a server program?

Since you already have exception handling, you should not hide the exception information.
The error message should also be provided when an error occurs.
In this way, we can determine the cause of the error. are you right?

The program on the server is started by double-clicking exe without writing system service items. In addition, I did not handle exceptions before. this was added later. just ignore it.
function send($in){        $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);        socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array("sec" => 5, "usec" => 0));        socket_set_option($socket, SOL_SOCKET, SO_SNDTIMEO, array("sec" => 180, "usec" => 0));        $result = socket_connect($socket, '127.0.0.1', 9999);        $out = "";        socket_write($socket, $in, strlen($in));        $out = socket_read($socket, 10240);        socket_close($socket);        return $out;}

You can echo your exception information, or use socket_strerror (socket_last_error ($ socket) to output the socket-generated error.

The error message returned by PHP is unexpected error on socket_connect at line 5.


You can echo your exception information, or use socket_strerror (socket_last_error ($ socket) to output the socket-generated error.

The error message returned by PHP is unexpected error on socket_connect at line 5.


if ($result == false)         echo socket_strerror(socket_last_error($socket));

if ($result == false)         echo socket_strerror(socket_last_error($socket));


You don't need it anymore. it's already fixed. Thank you.


if ($result == false)         echo socket_strerror(socket_last_error($socket));


You don't need it anymore. it's already fixed. Thank you.

Share your experiences and learn from them.

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.