Introduction to and use of the PHP socket (socket connection) extension

Source: Internet
Author: User

The PHP socket extension is based on the popular BSD sockets, which implements the underlying interface to the socket communication function, which can be used as a socket server as a client.

When using these functions, be aware that although many of them have the same name as the C function, the declarations are likely to be different. Do not avoid confusion, read the function description carefully.

Not familiar with socket programming can find a lot of useful information in the UNIX manual, there are many C socket programming tutorials, simple modification can be applied to PHP socket programming.

First step: Open the socket

Go to PHP.ini to open Extension=php_sockets.dll, then restart Apache to take effect. If you do not open or restart Apache, you will receive an error with call to undefined function socket_create ().

Step two: Using the socket

The PHP sockets workflow is:

1. Create Socket Socket_create ()

2. Try to create a socket connection target IP socket_connect (), note, here only write IP, only write IP, I write localhost is wrong.

3. Get the results of the connection, if the connection is successful, we can continue to do what we want to do, if it fails, we do a follow-up thing to do.

4. Close Socket Socket_close ()

Example of a PHP socket request:

<?php
Error_reporting (E_all);

echo "<H2>TCP/IP connection process

/* Get the Web server port */
$service _port = getservbyname (' www ', ' tcp ');

/* Get 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 successfully \ n";
}

echo "Attempted to connect ' $address ' via ' $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 "Connection port succeeded.". " <br> ";
}

$in = "head/http/1.1". " <br> ";
$in. = "Host:www.wangtuizhijia.com". " <br> ";
$in. = "Connection off". " <br> ";
$out = ";

echo "Send HTTP Head request ...". " <br> ";
Socket_write ($socket, $in, strlen ($in));
echo "Success". " <br> ";

echo "Response message:". " <br> ";
while ($out = Socket_read ($socket, 2048)) {
echo $out. " <br> ";
}

echo "Close the socket ...". <br> ";
Socket_close ($socket);
echo "end. \ n";
?>

Results:

TCP/IP connection process

Socket_create Create successful attempt to connect ' 103.76.85.67 ' via ' 80 ' port ...
The connection port was successful.
Send HTTP HEAD request ...
Success
Response Information:
http/1.1-Bad Request content-length:311 content-type:text/html; Charset=us-ascii server:microsoft-httpapi/2.0 Date:mon, 06:17:10 GMT connection:close
Close socket ...
End.

My blog: PHP socket (socket connection) extension introduction and how to use

Introduction and usage of the 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.