How PHP sockets are tested

Source: Internet
Author: User
PHP socket how to test
This post was last edited by d6965921d on 2014-07-13 09:57:41

 Error_reporting (E_all);
Set_time_limit (0);
Ob_implicit_flush ();

$address = ' 127.0.0.1 ';
$port = 10005;
Creating ports
if ($sock = Socket_create (Af_inet, sock_stream, sol_tcp) = = = = False) {
echo "Socket_create () Failed:reason:". Socket_strerror (Socket_last_error ()). "\ n";
}

Binding
if (Socket_bind ($sock, $address, $port) = = = = False) {
echo "Socket_bind () Failed:reason:". Socket_strerror (Socket_last_error ($sock)). "\ n";
}

Listening
if (Socket_listen ($sock, 5) = = = = False) {
echo "Socket_bind () Failed:reason:". Socket_strerror (Socket_last_error ($sock)). "\ n";
}

do {
Get a link
if ($msgsock = socket_accept ($sock)) = = = = False) {
echo "Socket_accepty () Failed:reason:". Socket_strerror (Socket_last_error ($sock)). " \ n ";
Break
}
Welcome Send to Client
$msg = "Server Send:welcome
";
Socket_write ($msgsock, $msg, strlen ($msg));
Echo ' read client message\n ';
$buf = Socket_read ($msgsock, 8192);
$talkback = "received message: $BUF \ n";
Echo $talkback;
if (false = = = Socket_write ($msgsock, $talkback, strlen ($talkback))) {
echo "Socket_write () failed reason:". Socket_strerror (Socket_last_error ($sock)). " \ n ";
} else {
Echo ' send success ';
}
Socket_close ($msgsock);
} while (true);
Close socket
Socket_close ($sock);


?>

How does this code work? I opened the page and I couldn't open it.
How do I test the effect of this socket I've written? I would like to test the successful re-write the socket client before the HTTP direct input URL will know ... The socket really doesn't know how to test it.
------Solution--------------------
Try a different port.
  • 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.