[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);

优质内容筛选与推荐>>
1、实时流Streaming大数据:Storm,Spark和Samza
2、Wireshark抓取soap消息
3、Java IO教程
4、MySQL 获取物理表的主键字段
5、最新的delphi Realthinclient服务端框架,插件式


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号