Table of Contents
TCP/IP Connection
Home php教程 php手册 Php的Socket初步

Php的Socket初步

Jun 06, 2016 pm 07:52 PM
php socket Work Compare

工作实在比较闲,就来写写博客吧。顺便整理下。环境:windows PHP在WIN下操作socket最好用命令行运行程序,至于为什么,自己可以体会下。 首先运行php 程序的路径,出现了这个提示表示你的php没有加入环境变量path下面。 然后找到你的php安装目录,我的是这

工作实在比较闲,就来写写博客吧。顺便整理下。环境:windows

PHP在WIN下操作socket最好用命令行运行程序,至于为什么,自己可以体会下。

Php的Socket初步

首先运行php + 程序的路径,出现了这个提示表示你的php没有加入环境变量path下面。

然后找到你的php安装目录,我的是这个

Php的Socket初步

然后复制上面的路径,右键点击我的电脑-属性-高级-环境变量,添加到环境变量中,注意有个;号,看下图

Php的Socket初步

然后确定,保存。再次运行cmd,OK成功了。不会出现那个提示了。

接下来就是主题了,先放代码

Php的Socket初步

<?php <span>//<span>确保在连接客户端时不会超时</span><span>set_time_limit</span>(0);

<span>$ip</span> = '127.0.0.1';
<span>$port</span> = 1935;

<span>/*</span><span>
 +-------------------------------
 *    @socket通信整个过程
 +-------------------------------
 *    @socket_create
 *    @socket_bind
 *    @socket_listen
 *    @socket_accept
 *    @socket_read
 *    @socket_write
 *    @socket_close
 +--------------------------------
 </span><span>*/</span>

<span>/*</span><span>----------------    以下操作都是手册上的    -------------------</span><span>*/</span>
<span>if</span>((<span>$sock</span> = socket_create(AF_INET,SOCK_STREAM,SOL_TCP)) echo "socket_create() 失败的原因是:".socket_strerror(<span>$sock</span>)."\n";
}

<span>if</span>((<span>$ret</span> = socket_bind(<span>$sock</span>,<span>$ip</span>,<span>$port</span>)) echo "socket_bind() 失败的原因是:".socket_strerror(<span>$ret</span>)."\n";
}

<span>if</span>((<span>$ret</span> = socket_listen(<span>$sock</span>,4)) echo "socket_listen() 失败的原因是:".socket_strerror(<span>$ret</span>)."\n";
}

<span>$count</span> = 0;

<span>do</span> {
    <span>if</span> ((<span>$msgsock</span> = socket_accept(<span>$sock</span>)) echo "socket_accept() failed: reason: " . socket_strerror(<span>$msgsock</span>) . "\n";
        <span>break</span>;
    } <span>else</span> {
        
        <span>//</span><span>发到客户端</span>        <span>$msg</span> ="测试成功!\n";
        socket_write(<span>$msgsock</span>, <span>$msg</span>, <span>strlen</span>(<span>$msg</span>));
        
        <span>echo</span> "测试成功了啊\n";
        <span>$buf</span> = socket_read(<span>$msgsock</span>,8192);
        
        
        <span>$talkback</span> = "收到的信息:<span>$buf</span>\n";
        <span>echo</span> <span>$talkback</span>;
        
        <span>if</span>(++<span>$count</span> >= 5){
            <span>break</span>;
        };
        
    
    }
    <span>//</span><span>echo $buf;</span>    socket_close(<span>$msgsock</span>);

} <span>while</span> (<span>true</span>);

socket_close(<span>$sock</span>);
?>
Copy after login

Php的Socket初步

这是socket的服务端代码。然后运行cmd,注意是自己的程序存放路径啊。

Php的Socket初步

没有反映,对现在服务端的程序已经开始运行,端口已经开始监听了。运行netstat -ano可以查看端口情况,我的是1935端口

Php的Socket初步

看,端口已经处于LISTENING状态了。接下来我们只要运行客户端程序即可连接上。上代码

Php的Socket初步

<?php <span>error_reporting(<span>E_ALL</span>);
<span>set_time_limit</span>(0);
<span>echo</span> "<h2 id="TCP-IP-Connection">TCP/IP Connection</h2>\n";

<span>$port</span> = 1935;
<span>$ip</span> = "127.0.0.1";

<span>/*</span><span>
 +-------------------------------
 *    @socket连接整个过程
 +-------------------------------
 *    @socket_create
 *    @socket_connect
 *    @socket_write
 *    @socket_read
 *    @socket_close
 +--------------------------------
 </span><span>*/</span>

<span>$socket</span> = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
<span>if</span> (<span>$socket</span> echo "socket_create() failed: reason: " . socket_strerror(<span>$socket</span>) . "\n";
}<span>else</span> {
    <span>echo</span> "OK.\n";
}

<span>echo</span> "试图连接 '<span>$ip</span>' 端口 '<span>$port</span>'...\n";
<span>$result</span> = socket_connect(<span>$socket</span>, <span>$ip</span>, <span>$port</span>);
<span>if</span> (<span>$result</span> echo "socket_connect() failed.\nReason: (<span>$result</span>) " . socket_strerror(<span>$result</span>) . "\n";
}<span>else</span> {
    <span>echo</span> "连接OK\n";
}

<span>$in</span> = "Ho\r\n";
<span>$in</span> .= "first blood\r\n";
<span>$out</span> = '';

<span>if</span>(!socket_write(<span>$socket</span>, <span>$in</span>, <span>strlen</span>(<span>$in</span>))) {
    <span>echo</span> "socket_write() failed: reason: " . socket_strerror(<span>$socket</span>) . "\n";
}<span>else</span> {
    <span>echo</span> "发送到服务器信息成功!\n";
    <span>echo</span> "发送的内容为:<font color="red"><span>$in</span></font> <br>";
}

<span>while</span>(<span>$out</span> = socket_read(<span>$socket</span>, 8192)) {
    <span>echo</span> "接收服务器回传信息成功!\n";
    <span>echo</span> "接受的内容为:",<span>$out</span>;
}


<span>echo</span> "关闭SOCKET...\n";
socket_close(<span>$socket</span>);
<span>echo</span> "关闭OK\n";
?>
Copy after login

Php的Socket初步

Php的Socket初步


至此客户端已经连接上服务端了。这只是个小例子,很多东西知道原理其他就好做了。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

See all articles