[PHP-Socket] Socket Programming in PHP
Simple Client-Server socket program in PHP
Introduction
Sockets are used for interprocess communication. Interprocess communication is generally based on client-server model. In this case, client-server are the applications that interact with each other.
Interaction between client and server requires a connection. Socket programming is responsible for establishing that connection between applications to interact.
By the end of this tip, we will learn how to create a simple client-server in PHP. We will also learn how client application sends message to server and receives it from the same.
Using the Code
Aim: Develop a client to send a string message to server and server to return reverse of the same message to client.
PHP SERVER:
Step 1: Set variables such as "host" and "port"
$host = "127.0.0.1";
$port = 5353
// No timeout
set_time_limit(0);
Port number can be any positive integer between 1024 - 65535
Step 2: Create Socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
Step 3: Bind the socket to port and host
Here the created socket resource is bound to IP address and port number.
$result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n");
Step 4: Start listening to the socket
$result = socket_listen($socket, 3) or die("Could not sest up socket listener\n");
Step 5: Accept incoming connection
This function accepts incoming connection request on the created socket. After accepting the connection from client socket, this function returns another socket resource that is actually responsible
for communication with the corresponding client socket. Here "$spawn" is that socket resource which is responsible for communication with client socket.
$spawn = socket_accept($socket) or die("Could not accept incoming connection\n");
So far, we have prepared our server socket but the script doesn't actually do anything. Keeping to our aforesaid aim, we will read message from client socket and then send back reverse message to
the client socket again.
Step 6: Read the message from the Client socket
$input = socket_read($spawn, 1024) or die("Could not read input\n");
Step 7: Reverse the message
$output = strrev($input) . "\n";
Step 8: Send message to the client socket
socket_write($spawn, $output, strlen($output)) or die("Could not write output\n");
Close the socket
socket_close($spawn);
socket_close($socket);
This completes with the server. Now we will learn to create PHP client.
PHP CLIENT
The first two steps are the same in ther server.
Step 1: Set variables such as "host" and "port"
$host = "127.0.0.1";
$port = 5353;
// No Timeout
set_time_limit(0);
Note: Here the port and host should same as defined in server.
Step2 : Create Socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
Step 3: Connect to the server
$result = socket_connect($socket, $host, $port) or die("Could not connect to server");
Here unlike server, client socket is not bound with port and host. Instead it connects to server socket, waiting to accept the connection from client socket. Connection of client to server socket is established in this step.
Step 4: Write to server socket
socket_write($socket, $message, strlen($message)) or die("Could not send data to server\n");
Step 5: Read the response from the server
$result = socket_read($socket, 1024) or die("Could not read server response\n");
echo "Reply From Server:" . $result;
Step 6: Close the socket
socket_close($socket);
[PHP-Socket] Socket Programming in PHP的更多相关文章
- Python socket – network programming tutorial
原文:https://www.binarytides.com/python-socket-programming-tutorial/ --------------------------------- ...
- [Socket]Socket文件传输
1.Server import java.io.DataInputStream; import java.io.FileOutputStream; import java.io.IOException ...
- [Socket]Socket聊天小程序
一个简单是Socket聊天小程序,读写操作在不同的线程中.服务器端采用线程池. 1.Server import java.io.IOException; import java.net.ServerS ...
- [Socket]Socket进程间的通信
转自:http://blog.csdn.net/giantpoplar/article/details/47657303 前面说到的进程间的通信,所通信的进程都是在同一台计算机上的,而使用socket ...
- ResourceWarning: unclosed <socket.socket fd=864, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('10.100.x.x', 37321), raddr=('10.1.x.x', 8500)>解决办法
将代码封装,并使用unittest调用时,返回如下警告: C:\python3.6\lib\collections\__init__.py:431: ResourceWarning: unclosed ...
- socket socket讲解
socket socket讲解 一.socket是何物? 参考百度百科: http://baike.baidu.com/link?url=4YNURsJLEaL0II79C68gPUoYKliXWJ ...
- Python socket & socket server
socket 网络上的两个程序通过一个双向的通信连接实现数据的交换,这个连接的一端称为一个socket(套接字). 建立网络通信连接至少要一对socket.socket是对TCP/IP的封装 使用方法 ...
- dotnet调用node.js写的socket服务(websocket/socket/socket.io)
https://github.com/jstott/socketio4net/tree/develop socket.io服务端node.js,.里面有js写的客户端:http://socket.io ...
- socket系列之客户端socket——Socket类
假设TCP套接字服务器端已经建立好并正在监听客户端的连接了,那么客户端就可以通过Socket类来发起连接.客户端发起一个连接请求后,就被动地在等待服务器的响应.这个类同样位于java.net包中,包含 ...
- 执行Socket socket = new Socket(ip, port);时抛出个异常:android.os.NetworkOnMainThreadException解决办法
首先,确认你的android版本是4.0之后再用此方法解决,因为在4.0之后在主线程里面执行Http请求才会报这个错,也许是怕Http请求时间太长造成程序假死的情况吧.Android在4.0之前的版本 ...
随机推荐
- 在python中处理XML
XML是实现不同语言或程序之间进行数据交换的协议,XML文件格式如下: <data> <country name="Liechtenstein"> < ...
- HTML4.01和XHTML1.0和XHTML1.1的一些区别
接触web前端以来,一直使用的都是html5,因此一直没搞明白HTML4.01和XHTML1.0和XHTML1.1之间的区别,今天在看<精通CSS>一书,有简单介绍这几个,在这儿记录下. ...
- 7.arm汇编 bic和orr指令
1. bic BIC指令的格式为: BIC{条件}{S} 目的寄存器,操作数1,操作数2 BIC指令用于清除操作数1的某些位,并把结果放置到目的寄存器中. 操作数1应是一个寄存器, 操作数2可以是一 ...
- archlinux更新错误
问题1 初始化下载: http://repo.archlinuxcn.org/x86_64/wps-office-10.1.0.5672_a21-2-x86_64.pkg.tar.xz 文件大小: 1 ...
- Python-内置类属性
Python内置类属性 __dict__ : 类的属性(包含一个字典,由类的数据属性组成) __doc__ :类的文档字符串 __name__: 类名 __module__: 类定义所在的模块(类的全 ...
- gdb调式
1.PCB版的相应目录下执行命令: gdbserver 10.18.13.84:5555 DvdPlayer 2.linux操作系统执行:(如果是android找到android项目路径下的gdb)m ...
- iOS设计模式-Block实现代理的逻辑
在A页面,点击跳转到B页面,B页面操作完,回到A页面,并刷新A页面的内容.典型的例子,就是在一个列表里,点击新增,跳到新增页面,新增完,把数据传回给列表页,并刷新列表页里的内容. 这个,我平时一般是通 ...
- 各种 starter poms (启动器)
starter包含了搭建项目,快速运行所需的依赖.它是一个依赖关系描述符的集合.当应用需要一种spring的服务时,不需要粘贴拷贝大量的依赖关系描述符.例如想在spring中使用redis,只需要在项 ...
- GridView中DropDownList
<asp:TemplateField HeaderText="下拉框"> <ItemTemplate> <cc1:MyDropDownList ID= ...
- hdu 5826 (物理) physics
题目:这里 题意:光滑的水平直线上有n个质量相等的小球,已知每个小球的初始位置,初始速度和方向,每个小球的每个时刻的加速度a都满足a*v=c,v是该时刻的速度,c是已知的 常数,小球之间的碰撞是完全碰 ...