Sockets in PHP

1
4739

Today we will be learning about sockets, how the connection between client and server is established and how they transmit messages to each other in this Sockets in PHP tutorial.

Sockets are mainly used for Interprocess communication. This is done in client-server model. In the client-server model, client sends a request to the server and the server replies to the request by sending a response. Due to this socket is also called as the entry-point of a machine in a network. But this requires that both of them have a connection between them. This connection is not physical, it is created virtually when the communication is to be done and disconnected when the communication ends.
This connection is established using the IP address and the port number. IP address is the address of the particular computer machine in a network, through which we can locate the machine. Port number is the identification of a network application within that particular computer.
The IP address and Port number together form a socket. So when we give an IP address 192.168.2.2 and port number 80, it will connect to the application on port number 80 in the computer having IP address 192.168.2.2.
The connection of client and server can be established with protocol’s such as TCP or UDP. But TCP is a connection oriented protocol and used for reliable transmission.
Let us learn how to establish connection between client and server and how to transmit messages to each other step by step.

  • Client-side program:
    • Steps to create a client side program:
      • Creating a client-side socket using socket_create() function.
      • Connect to the server using socket_connect() function.
      • Send request to the server using socket_send() or socket_write() function.
      • Receive the response from server if any using socket_recv() or socket_read() function.
      • Close the socket using socket_close() function.
    • Now let us see these steps in detail:
    • Creating a client socket:
      • To create a client socket socket_create() function is used. This function returns the socket which is further used as the client socket. The function is used as follows:
      • In the socket_create() function the parameters determine the following things:
      • AF_INET : this is an IPv4 address family.
      • SOCK_STREAM : this is a connection oriented TCP protocol.
      • 0 : this is the IP protocol.
      • Thus we have to pass type of address family, type of the protocol and protocol parameters in the function socket_create().
    • Connecting to server:
      • Next step is to connect to the server. But to connect to the server we need the IP address of the server and the port number of the application with which we want to work.
      • So let us specify the IP address and port number and use them in the socket_connect() function.
      • In the above code the $host variable contains the server’s IP address and the $port variable contains the port number of the application on the server.
      • Remember that the port numbers 0 – 1023 are reserved for various applications and hence the ports from 1024 to 65535 are available for programmers.
      • The socket is now connected to server using socket_connect() function with the client socket which is to be connected to the server, IP address of the server and port number of the application as its parameters.
    • Sending message or request to server:
      • To send a message or request to the server we use socket_send() function.
      • Actually sending a message means writing to socket, hence we can even use write() function to write and send the message as we do in files.
      • Let us see the code to send a message to the server:
      • Here, we are sending the name of the user to server using the socket_send() function with client socket and the variable containing the name as its parameters.
    • Receiving response from server:
      • To receive the response from server we use socket_read() function.
      • It takes the client socket and the port number as parameters.
      • It is defined as follows:
      • The response sent by server is stored in variable $ans.
    • Closing the client socket:
      • After completing all the communication, when it is time to disconnect the connection the function socket_close() is called to close the client socket. It is shown below:
    • The complete client program is shown below:
  • Server-side program:
    • Steps to create a server side program:
      • Creating a server-side socket using socket_create() function.
      • Binding the created socket to the IP address and port number of the server itself using socket_bind() function .
      • After binding the socket wait for the clients to connect using socket_listen() function.
      • Accept the incoming clients connection using socket_accept() function.
      • Read the request from the client socket using socket_read() function.
      • Send the response to client by socket_send() or socket_write() function.
      • Close the socket using socket_close() function.
    • Now let us see these steps in detail:
    • Creating a server socket:
      • To create a server socket socket_create() function is used. This function returns the socket which is further used as the server socket. The function is used as follows:
      • In the socket_create() function the parameters determine the following things:
      • AF_INET : this is an IPv4 address family.
      • SOCK_STREAM : this is a connection oriented TCP protocol.
      • 0 : this is the IP protocol.
      • Thus we have to pass type of address family, type of the protocol and protocol parameters in the function socket_create().
    • Binding the socket:
      • The socket created at the server side is not yet bound to the server.
      • To bind it to the server, it has to be bound to its IP address and port number. The socket_bind() function is used for this purpose.
      • The socket is bound to the server as follows:
      • This socket serves all the clients.
    • Waiting for the clients:
      • The server after binding becomes ready and waits for the clients to connect.
      • This waiting process done using function socket_listen().
      • The number of clients server can handle at a time is specified in the socket_listen() function.
      • The socket_listen() function is used as follows:
      • The socket_listen() function has the socket which is waiting for clients and number of clients it can handle as parameters.
      • The above server socket can handle 12 clients.
    • Accepting the incoming clients:
      • When the client requests for a connection to the server socket, the socket_accept() function accepts its connection. This creates a new socket which will be responsible for the communication of that particular client only.
      • Suppose while the first client’s communication is on and a new client connects to the server, the socket_accept() function will create another socket instance for that particular client to continue its communication. This socket can also be called as the particular client’s socket on server.
      • It is done as follows:
    • Reading the request of client:
      • The request sent by the client is to be read in order to process it. This is done using socket_read() function.
      • This input from the client is processed accordingly and then sent as a response to client again.
      • The request is read as follows:
      • The socket_read() function has the server-side client socket and the port number as its parameters.
    • Sending response to client:
      • After processing the request and preparing the response, it is then sent to the client using either socket_send() or socket_write() function.
      • It is shown below:
      • The socket_write() function contains the server-side client socket and the response as its parameters.
      • When the response is written to the server-side client socket, it is taken to the client- side socket.
    • Closing the socket:
      • When the client disconnects the connection from server, the socket which was created to connect with it when its connection was accepted by server is closed using socket_close() function.
      • This is done as follows:
    • The complete server program is shown below:
    • One more thing, the port numbers in the server as well as client programs should match.

Thus we tried to understand the server and client processes during their interprocess communication in this Sockets in PHP tutorial.

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here