As a non-low-level programmer, it is very difficult to deeply understand the internal implementation mechanism of socket. We only need to understand that socket is a set of functions encapsulated by the operating system to implement process communication. It is enough to create and call it. .
The language characteristics and positioning of PHP determine that it is only suitable for socket clients, not for socket servers.
Because socket is mainly oriented to the bottom layer and network service development, the server side is generally implemented in languages such as C or Java. This can better operate the bottom layer and solve problems encountered in network service development. There are also mature and complete solutions to problems (such as concurrency, blocking, etc.), but PHP is obviously not suitable for this application scenario. (Recommended study: PHP video tutorial)
In fact, PHP operates the MySQL database through socket. This is precisely because the socket shields the underlying protocol, making network services Interconnection between them becomes simple.
In addition to sockets implemented in traditional server-side languages, with the popularity of HTML5, WebSockets implemented in browser clients are also gradually emerging. This is worthy of attention. FlashSocket is also a good solution. .
To operate the socket on the client, you can use functions such as fsockopen, socket_create or stream_socket_client. If it is PHP5, it is recommended to use stream_socket_client.
Socket interactive application example: using socket to submit a form
Create a new test.php file and send it to http://demo.com/index .php?id=1 Submit form data, the code is as follows:
<?php $data = array('comment'=>'this is a robot comment'); $data = http_build_query($data); $out = "POST http://demo.com/index.php?id=1 HTTP/1.1\r\n"; // 通过POST方式发送数据 $out .= "Host: demo.com\r\n"; $out .= "Content-type: application/x-www-form-urlencoded; charset=UTF-8\r\n"; $out .= "Content-length: ".strlen($data)."\r\n"; $out .= "User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:48.0) Gecko/20100101 Firefox/48.0"."\r\n"; $out .= "Connection: close"."\r\n"."\r\n"; // 注意:此处有两个 \r\n $out .= $data."\r\n"; // 正文数据 $fp = fsockopen("demo.com", 80, $errno, $errstr, 30); // 创建socket客户端连接 // $fp = stream_socket_client("tcp://demo.com:80", $errno, $errstr, 30); 推荐这种写法 fwrite($fp, $out); // 向服务器发送数据 while (!feof($fp)) { echo fgets($fp, 1280); // 读取服务器响应的数据 } fclose($fp); // 关闭socket连接 ?>
You need to pay attention to the following points:
The first parameter of fsockopen can also use the IP address , do not bring http:// string, unless using SSL, etc.
Request headers do not necessarily need to bring all header fields, generally only need to bring a few core headers
At the last header, there are two line breaks after Connection
Pay attention to encoding issues
The above is the detailed content of Why is php not suitable for sockets?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Chinese version
Chinese version, very easy to use

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
