How to send and receive data to the socket server in PHP, socket sending and receiving_PHP tutorial

PHP中文网
Release: 2016-07-13 10:09:07
Original
1182 people have browsed it


How PHP sends and receives data to the socket server, socket sending and receiving


The example in this article describes the method of PHP sending and receiving data to the socket server . Share it with everyone for your reference. The details are as follows:

To send data to other programs in PHP, you need to use the socket function of PHP to instantiate it. Let’s briefly look at an example. The code is as follows:

Code As follows:

/*socket收发数据 
    @host(string) socket服务器IP 
    @post(int) 端口 
    @str(string) 要发送的数据 
    @back 1|0 socket端是否有数据返回 
    返回true|false|服务端数据 
*/ 
function sendSocketMsg($host,$port,$str,$back=0){ 
        $socket = socket_create(AF_INET,SOCK_STREAM,0); 
        if ($socket < 0) return false; 
        $result = @socket_connect($socket,$host,$port); 
        if ($result == false)return false; 
        socket_write($socket,$str,strlen($str));
        if($back!=0){ 
            $input = socket_read($socket,1024); 
            socket_close ($socket);     
            return $input; 
        }else{ 
            socket_close ($socket);     
            return true;     
        }     
}
Copy after login


The second parameter of socker_read is used to specify the number of bytes to be read. You can use it to limit the size of data obtained from the client.

Introduction to sock function

函数名 描述
socket_accept() 接受一个Socket连接
socket_bind() 把socket绑定在一个IP地址和端口上
socket_clear_error() 清除socket的错误或者最后的错误代码
socket_close() 关闭一个socket资源
socket_connect() 开始一个socket连接
socket_create_listen() 在指定端口打开一个socket监听
socket_create_pair() 产生一对没有区别的socket到一个数组里
socket_create() 产生一个socket,相当于产生一个socket的数据结构
socket_get_option() 获取socket选项
socket_getpeername() 获取远程类似主机的ip地址
socket_getsockname() 获取本地socket的ip地址
socket_iovec_add() 添加一个新的向量到一个分散/聚合的数组
socket_iovec_alloc() 这个函数创建一个能够发送接收读写的iovec数据结构
socket_iovec_delete() 删除一个已经分配的iovec
socket_iovec_fetch() 返回指定的iovec资源的数据
socket_iovec_free() 释放一个iovec资源
socket_iovec_set() 设置iovec的数据新值
socket_last_error() 获取当前socket的最后错误代码
socket_listen() 监听由指定socket的所有连接
socket_read() 读取指定长度的数据
socket_readv() 读取从分散/聚合数组过来的数据
socket_recv() 从socket里结束数据到缓存
socket_recvfrom() 接受数据从指定的socket,如果没有指定则默认当前socket
socket_recvmsg() 从iovec里接受消息
socket_select() 多路选择
socket_send() 这个函数发送数据到已连接的socket
socket_sendmsg() 发送消息到socket
socket_sendto() 发送消息到指定地址的socket
socket_set_block() 在socket里设置为块模式
socket_set_nonblock() socket里设置为非块模式
socket_set_option() 设置socket选项
socket_shutdown() 这个函数允许你关闭读、写、或者指定的socket
socket_strerror() 返回指定错误号的详细错误
socket_write() 写数据到socket缓存
socket_writev() 写数据到分散/聚合数组

Note: The socket_read function will keep reading the shell client data until it encounters n, t or characters. The PHP script regards these characters as the end of the input.

Hope What is described in this article will be helpful to everyone’s PHP programming.


http://www.bkjia.com/PHPjc/947218.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/947218.htmlTechArticlePHP to socket The method for the server to send and receive data, socket sending and receiving. This article describes the method of PHP sending and receiving data to the socket server. Share it with everyone for your reference. The details are as follows: In PHP you need to...


Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!