what is socket in php? And at what condition i should go for socket connection?

14,441

Solution 1

In order to understand sockets I think it's important to understand networking principles. Especially the Internet Protocol and Transmission Control Protocol.

The Transmission Control Protocol is a way of breaking up a message into smaller chunks, and addressing them in such a way that the chunks can be reliably re-assembled at the receiving end. The Internet Protocol is a way of routing these chunks through the Internet.

A Socket is just a programming object that manages the details of these protocols for you. You configure the socket to connect to a given port on a given IP address. The socket manages the rest: chunking, packaging, and labeling the data. The socket encapsulates all the protocol details so that you can abstract them away and act as if you are creating a "connection" from one computer to another. As a developer, you use sockets when you need to exchange information with another computer over the Internet.

For me, the idea of a socket and what it might be used for didn't make sense until I studied computer networking. (Especially the protocols themselves, not necessarily the practical, technician side of things.) You can start with the Wikipedia articles on TCP and IP. And you can try to read individual, piecemeal articles on the web. But frankly, networking is such a huge topic that I don't think anything short of a cohesive, semester-long course or a quality textbook would be enough to truly answer this question (and to correct the gaps, oversimplifications, and exceptions that I used to keep this answer simple.)

Solution 2

I know it's too late, but I think this link will help anyone else who wants to get when to use web socket

websocket

Solution 3

You need to understand the concept of socket programming. To get a better idea.

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.

Client application sends message($message) to server($host) and the server application receives it from the client through a port($port).

The client.php runs and sends the message from a client machine. The server.php runs on the server machine which receives the message.

Try these links for examples and how to run the server and client files.

http://www.binarytides.com/php-socket-programming-tutorial/
http://www.devshed.com/c/a/php/socket-programming-with-php/

Share:
14,441
Sam
Author by

Sam

PHP Beginner

Updated on June 08, 2022

Comments

  • Sam
    Sam almost 2 years

    I have already gone through some tutorials for socket but i couldn't get what it does. I want to know what sockets do and why is it used. This is the code I have referred.

    client.php
    <?php
    $host    = "localhost";
    $port    = 1024;
    $message = "Hello Server";
    echo "Message To server :".$message;
    // create socket
    $socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create              socket\n");
    // connect to server
    $result = socket_connect($socket, $host, $port) or die("Could not connect to  server\n");  
    // send string to server
    socket_write($socket, $message, strlen($message)) or die("Could not send data to server\n");
    // get server response
    $result = socket_read ($socket, 1024) or die("Could not read server response\n");
    echo "Reply From Server  :".$result;
    // close socket
    socket_close($socket);
    ?>
    
    server.php
    <?php
    // set some variables
    $host = "localhost";
    $port = 1024;
    // don't timeout!
    set_time_limit(0);
    // create socket
    $socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
    // bind socket to port
    $result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n");
    // start listening for connections
    $result = socket_listen($socket, 3) or die("Could not set up socket listener\n");
    
    // accept incoming connections
    // spawn another socket to handle communication
    $spawn = socket_accept($socket) or die("Could not accept incoming connection\n");
    // read client input
    $input = socket_read($spawn, 1024) or die("Could not read input\n");
    // clean up input string
    $input = trim($input);
    echo "Client Message : ".$input;
    // reverse client input and send back
    $output = strrev($input) . "\n";
    socket_write($spawn, $output, strlen ($output)) or die("Could not write  output\n");
    // close sockets
    socket_close($spawn);
    socket_close($socket);
    ?>
    

    So I couldn't get the idea of where to enter the server code and client code. Usually we write server code on what it should do while getting user input.So i am extremely confused about this. Can anyone help me? Thanks in advance