Common socket instances in php

Source: Internet
Author: User
Tags error code error handling php tutorial socket

Code Extension to obtain remote host socket information */
Function getpeername ()
{
$ Remote_ip = $ remote_port = 0; // defines the ip address.
Socket_getpeername ($ this-> fd, $ remote_ip, $ remote_port); // return host information
Return array ($ remote_ip, $ remote_port); // returns an array
}

?>

<? Php Tutorial
If (false = ($ socket = @ socket_create (af_inet, sock_stream, sol_tcp) // if the socket fails to be created
{
Die ("couldn't create socket, error code is :". socket_last_error (). ", error message is :". socket_strerror (socket_last_error (); // output error information and error code
}
?>

<? Php
/* The following code expands to obtain the local host socket information */
Function getsockname ()
{
$ Remote_ip = $ remote_port = 0; // defines the ip address.
Socket_getsockname ($ this-> fd, $ remote_ip, $ remote_port); // return host information
Return array ($ remote_ip, $ remote_port); // returns an array
}
?>


<? Php
/* Define the array to be read */
$ Read = array ($ socket1, $ socket2 );
/* Perform the operation */
$ Num_changed_sockets = socket_select ($ read, $ write = null, $ response t = null, 0 );
If ($ num_changed_sockets = false)
{
/* Error handling */
}
Else if ($ num_changed_sockets> 0)
{
/* At least at one of the sockets something interesting happened */
}

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.