PHP Socket (Socket connection) Extension introduction and usage, phpsocket

Source: Internet
Author: User
Tags website server

PHP Socket (Socket connection) Extension introduction and usage, phpsocket

The PHP socket extension is based on the popular BSD sockets and implements the underlying interface of the socket communication function. It can be used as a socket server like the client.

When using these functions, note that although many of them have the same name as C functions, the Declaration may be very different. Do not avoid confusion. Read the function description carefully.

If you are not familiar with socket programming, you can find a lot of useful information in the Unix manual. There are also many tutorials on C socket programming on the Internet. You can simply modify it to apply it to PHP socket programming.

Step 1: Enable socket

Open extension = php_sockets.dll in php. ini, and restart apache to take effect. If you do not enable or restart Apache, the Call to undefined function socket_create () error is returned.

Step 2: Use socket

PHP socket workflow:

1. Create socket socket_create ()

2. Try to connect to the target ip address socket_connect () through the created socket. Note that only ip addresses can be written here and only ip addresses can be written here. If I write localhost, it will be wrong.

3. Obtain the connection result. If the connection is successful, we can continue to do what we want to do. If the connection fails, we will do what we will do later.

4. Disable socket socket_close ()

PHP socket request example:

<? Php
Error_reporting (E_ALL );

Echo "

/* Obtain the website server port */
$ Service_port = getservbyname ('www ', 'tcp ');

/* Obtain the IP address of the server .*/
$ Address = gethostbyname ('www .wangtuizhijia.com ');

/* Create a TCP/IP socket .*/
$ Socket = socket_create (AF_INET, SOCK_STREAM, SOL_TCP );
If ($ socket = false ){
Echo "socket_create () failed: reason:". socket_strerror (socket_last_error (). "\ n ";
} Else {
Echo "socket_create created \ n ";
}

Echo "try to connect '$ address'..." Through' $ service_port 'port. "<br> ";
$ Result = socket_connect ($ socket, $ address, $ service_port );
If ($ result = false ){
Echo "socket_connect () Connection Failed: ($ result)". socket_strerror (socket_last_error ($ socket). "\ n ";
} Else {
Echo "port connected successfully.". "<br> ";
}

$ In = "HEAD/HTTP/1.1". "<br> ";
$ In. = "Host: www.wangtuizhijia.com". "<br> ";
$ In. = "Connection closed". "<br> ";
$ Out = '';

Echo "Send http head request..."... "<br> ";
Socket_write ($ socket, $ in, strlen ($ in ));
Echo "successful". "<br> ";

Echo "response information:". "<br> ";
While ($ out = socket_read ($ socket, 2048 )){
Echo $ out. "<br> ";
}

Echo "Disable socket...". "<br> ";
Socket_close ($ socket );
Echo "end. \ n ";
?>

Result:

TCP/IP connection process

Socket_create is created successfully. Try to connect to '2017. 76.85.67 'through '80' port '...
The port is successfully connected.
Send an http head request...
Successful
Response Information:
HTTP/1.1 400 Bad Request Content-Length: 311 Content-Type: text/html; charset = us-ascii Server: Microsoft-HTTPAPI/2.0 Date: Mon, 08 May 2017 06:17:10 GMT Connection: close
Close socket...
End.

 

My blog: Introduction and usage of PHP Socket (Socket connection) Extension

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.