Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Function and example Analysis of PHP SOCKET programming

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "the function and example analysis of PHP SOCKET programming". In the daily operation, I believe that many people have doubts about the function and case analysis of PHP SOCKET programming. The editor has consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts of "PHP SOCKET programming function and case analysis". Next, please follow the editor to study!

1. Preliminary knowledge

It has been rare to see how many people use php's socket module to do something, probably everyone locates it in the category of scripting language, but in fact, php's socket module can do a lot of things, including doing ftplist,http post submission, smtp submission, package and special message interaction (such as smpp protocol), whois query. These are more common queries.

In particular, what php's socket extension library can do is hardly worse than c.

Socket connection function of php

1. Socket integrated into the kernel

This series of functions can only do active connections and can not achieve port listening-related functions. And before 4.3.0, all socket connections could only work in blocking mode.

This series of functions include

Fsockopen,pfsockopen

For more information on these two functions, please refer to php.net 's user manual.

They all return a resource number. For this resource, you can use almost all functions that operate on files, such as fgets (), fwrite (), fclose (), etc. Note that all functions follow the rules of these functions when facing the network information flow, for example:

Fread () reads up to length bytes from the file pointer handle. This function stops reading files when it has finished reading length bytes, or when it reaches EOF, or (for network streams) when a packet is available, depending on which case it encounters first.

It can be seen that for the network flow, we must pay attention to the stop when a complete packet is fetched.

2. The socket function of php expansion module.

After php4.x, there is such a module. There is an extension=php_sockets.so on extension=php_sockets.dll,Linux.

When this module is opened, it means that php has powerful socket functions, including listen ports, switching between blocking and non-blocking modes, multi-client interactive processing, etc.

For a list of functions in this series, see http://www.php.net/manual/en/ref.sockets.php.

After reading this list, do you think it is very rich? However, it is a pity that this module is still very young and immature in many places, and the relevant reference documentation is very few: (

I'm also studying it, so I won't discuss it in detail for the time being. I'll just give you a reference article.

Http://www.zend.com/pecl/tutorials/sockets.php

two。 Use PHP socket extension

Server-side code:

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report