[Original] using PHP to build a socket Service Based on swoole extension (with PHP extension installation steps)

Source: Internet
Author: User
In a recent project of the company, a socket service needs to be built using PHP. PHP is not suitable for services because it needs to use efficient and stable TCP protocol for data communication in cooperation with third parties. After several attempts, I finally chose the open-source PHP Extension: swoole, a multi-thread asynchronous Server written in C. Binary data communication comes with php.

In a recent project of the company, a socket service needs to be built using PHP. PHP is not suitable for services because it needs to use efficient and stable TCP protocol for data communication in cooperation with third parties. After several attempts, I finally chose the open-source PHP Extension: swoole, a multi-thread asynchronous Server written in C. Binary data communication comes with php.

In a recent project of the company, a socket service needs to be built using PHP.

PHP is not suitable for services because it needs to use efficient and stable TCP protocol for data communication in cooperation with third parties. After several attempts, I finally chose the open-source PHP Extension: swoole, a multi-thread asynchronous Server written in C.

Binary data communication uses the pack () and unpack () functions provided by php. Note that when you use pack () to pack letters, you need to input the corresponding ASCII code value.

A shell script for disabling the socket service is provided (used when the code is updated and the socket service needs to be restarted ):

#! /Bin/shecho "start kill socket... "PID = 'netstat-anp | grep 10001 | awk' {printf $7} '| cut-d/-f1 'echo" pid: $ PID "kill-50" $ PID "echo" Killed pid 10001 "# assume that the socket service port is 10001 kill-USR1

PS: PHP extension installation steps (http://coolerfeng.blog.51cto.com/133059/98460 ):
1. First, go to the source code directory of the extension to be installed.
Cd/tmp/phpext/swoole
2. Generate the configure file in the swoole folder.

#/Usr/local/php5/bin/phpize
Processing ing:
PHP Api Version: 20041225
Zend Module Api No: 20060613
Zend Extension Api No: 220060519

This prompt can be expanded.

3. Configure compilation and Installation

#./Configure -- with-php-config =/usr/local/php5/bin/php-config
# Make

# Make install

4. Generate a swoole. so file under/opt/php/lib/php/extensions.

5. Edit php. ini. (If php is installed in/usr/local/php5, add php. ini to/usr/local/php5/lib/php. ini.
Extension_dir = "/usr/local/php5/lib/php/extensions/no-debug-non-zts-20060613 /"
Extension = "swoole. so"

6. Restart apache and run php-m or phpinfo () to check whether swoole extensions are successfully loaded.

[Technical Summary]

1. Use the TCP/IP protocol

TCP/IP, Http, Socket difference: http://jingyan.baidu.com/article/08b6a591e07ecc14a80922f1.html

2. Packaging and parsing binary: Use the pack () and unpack () functions provided by php.

Http://blog.csdn.net/zhuoxiong/article/details/7362179

Http://www.cnblogs.com/wanzibox/articles/1283110.html

Http://www.neatstudio.com/show-2327-1.shtml

3. Build the socket service: Use swoole Extension

Swoole Official Website: http://www.swoole.com/,http://wiki.swoole.com/wiki/index/prid-1
Swoole getting started tutorials and documentation: https://github.com/LinkedDestiny/swoole-doc

4. Start and Stop the socket service: shell script

Linux & Shell: http://blog.csdn.net/wklken/article/category/1138797

Shell online manual:

Http://shouce.jb51.net/shell/index.html

Linux Online manual:

Http://www.hi-docs.com/linux.html

Http://man.linuxde.net/

5. business logic: cookie verification and Redis Cache

Http://www.cnblogs.com/imxiu/category/545814.html

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.