本文实例讲述了PHP向socket服务器收发数据的方法。分享给大家供大家参考。具体如下:
在PHP要向其它程序发送数据,则需要使用到php的socket功能来实例了,下面我们简单看一个实例,代码如下:
代码如下:
/*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
$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;
}
}
socker_read的第二个参数用以指定读入的字节数,你可以通过它来限制从客户端获取数据的大小.
sock函数介绍
函数名 | 描述 |
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() | 写数据到分散/聚合数组 |
注意:socket_read函数会一直读取壳户端数据,直到遇见n,t或者字符,PHP脚本把这些字符看做是输入的结束符.
希望本文所述对大家的php程序设计有所帮助。

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


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

SublimeText3 English version
Recommended: Win version, supports code prompts!

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver CS6
Visual web development tools