WebSocket connection settings for PHP on Mac

Source: Internet
Author: User
After installing the socket extension of PHP, you can use the various socket functions of PHP, first learn about the functions of the relevant parameters and use, such as I already know that a server can be connected through the WebSocket domain name is 192.168.15.192 The port number is 9001, and you can use the phone to understand how the socket is connected.

1. # # First Call when the connection between the two should have a telephone; Socket on both sides have the socket number is the domain name;

$socket _create = socket_create ($domain, $type, $protocal); # # Resource Type

$error = Socket_last_error ($socket _create); # # If the connection is successful, an int (0) connection failure will return an error code

$strerror = Socket_strerror ($error) # # Read the text description of the error

# # put on the official note the link to this function http://php.net/manual/en/function.socket-create.php

# # The first parameter $domain is the protocol that specifies the socket to use

There are three options available, and I'm using the first kind of af_inet:

Af_inet Internet protocol based on IPV4

Af_inet6 IPV6 Internet-based protocols

Af_unix Local Communication protocol

# # The second parameter $type is the connection method

There are five options available, and I'm using the first kind of sock_stream:

SOCK_STREAM provides a sequential socket TCP protocol based on this type

SOCK_DGRAM support Packet UDP protocol based on this type

Sock_seqpacket provides sequential transmission for fixed maximum-length datagrams reads the entire packet per call

SOCK_RAW provides the original network protocol access can be manually constructed by the user any protocol commonly used to perform ICMP requests (such as a terminal ping request)

SOCK_RDM does not guarantee a reliable sequencing

# # # The third parameter $protocal is set to specify the specific protocol domain; You can call Getprotobyname ($name) to retrieve the value, $name is the string can be "IP", "ICMP", "GGP", "TCP", "EGP", "Pup", "UDP", "HMP", "XNS-IDP", "RDP", " RVD ", I used Getprotobyname (" TCP "), returned 6, corresponding to the sol_tcp this constant.

The third parameter should be 2, and I don't know why there are so many $name:

SOL_TCP # # I just used this constant test to be successful

SOL_UDP # # have not tried please Google yourself ...

Other constants can refer to the official Constants definition manual http://php.net/manual/en/sockets.constants.php

2. Dial the call to the other party; Equivalent to making a connection request

$result = Socket_connect ($socket _create, "192.168.15.192", "9001");

# # Three parameters First write Socket_create resource second write domain name third write port number

3. Reading information

while ($out = Socket_read ($create _socket, $length)) {var_dump ($out);}

# # Socket_read is a minimum of 2 parameters

The first parameter is a create_socket resource

The second parameter writes the length of the data that you want to receive in bytes

The third argument is that the type can not be written by default is Php_binary_read this constant

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.