How do i program a socket in php?
Show
Socket programming is a way of connecting two nodes on a network to communicate with each other. One socket(node) listens on a particular port at an IP, while other socket reaches out to the other to form a connection. The server forms the listener socket while the client reaches out to the server. Image From google search Now are Develop a client to send a stringmessage to server and server to return reverse of the same message to the client. PHP SERVER
$host = “127.0.0.1”; // your localhost IP
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die(“Could not create socket\n”);
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”);
After getting bound with IP and port server waits for the client to connect. Till then it keeps on waiting. $result = socket_listen($socket, 3) or die(“Could not set up socket listener\n”);
This function accepts an incoming connection request on the created socket. After accepting the connection from the 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 a message from the client socket and then send back reverse of the received message to the client socket again.
$input = socket_read($spawn, 1024) or die(“Could not read input\n”);
$output = strrev($input) . “\n”;
socket_write($spawn, $output, strlen ($output)) or die(“Could not write output\n”);
socket_close($spawn); PHP CLIENT The first two steps are the same as in the server.
$host = “127.0.0.1”;
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die(“Could not create socket\n”);
$result = socket_connect($socket, $host, $port) or die(“Could not connect toserver\n”); 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 socket to server socket is established in this step.
socket_write($socket, $message, strlen($message)) or die(“Could not send data to server\n”); In this step, client socket data is sent to the server socket.
$result = socket_read ($socket, 1024) or die(“Could not read server response\n”);
socket_close($socket);Complete Code SERVER (server.php) CLIENT (client.php) After creating the above files (server.php and client.php), do as follows:
My Publis article List Multi-threading in PHPLaravel Read / Write Connections use multi-databaseSocket Programming in PHPHow do I install a PHP socket?go to your php installation directory in windows it can be something like this c:\xampp\php.. the goto ext direction in it php\ext\. check if your have php_sockets.dll in that directory.. after that open php.ini file which will be in your php folder.. next search for ;extension=php_sockets.dll.. What is socket programming?Socket programming shows how to use socket APIs to establish communication links between remote and local processes. The processes that use a socket can reside on the same system or different systems on different networks. Sockets are useful for both stand-alone and network applications.
What is socket command?The socket command may be used to open either the client or server side of a connection, depending on whether the -server switch is specified. Note that the default encoding for all sockets is the system encoding, as returned by encoding system.
How socket is implemented in laravel?Step 1: Configure Redis. To configure Redis into Laravel – you will need to install the package using the below command: ... . Step 2: Create Event And Fire Event. ... . Step 3: Install Required Packages. ... . Step 4: Configure Socket.IO. ... . Step 5: Setup Websocket And Consume Data. ... . Conclusion.. |