Welcome to Findnerd. Today we are going to discuss socket programing in PHP.
First question which arise is what is socket?
Socket is an end point in two way communication. Two way communication has client and server sides. Client creates a socket to communicate with server. you can write the data to that socket and transfer the same. If we talk about the socket programing in PHP then PHP provides different types of functions to accomplish it. In socket programming we create a socket using socket_create.
Please have a look.
if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
echo "socket creation failed: " . socket_strerror(socket_last_error()) . "\n";
}
First parameter specify the protocol family to be used by socket. There are different types of protocol families. Please have a look.
A) AF_INET : Based on IPv4 internet and TCP/UDP comes under it
B) AF_INET6 : Based on IPv6 internet and TCP/UDP is common protocols of this family.
C) AF_UNIX : used in local communication protocol family.
Second parameter specify the type of communication used by the socket. There are different types of socket types and they are mentioned below:
A) SOCK_STREAM: sequenced, reliable, full-deplex, connection based byte stream. TCP protocol comes under it.
B) SOCK_DGRAM : connectionless, unreliable messages of a fixed maximum length. UDP comes under it.
C) SOCK_SEQPACKET : Supports two way connection based data tramission path for datagrams of fixed maximum length, sequenced and reliable.
D) SOCK_RAW: Supports raw network protocol access. This special type of socket can be used to construct any type of protocol.
E) SOCK_RDM : supports reliable datagram layer that does not guarantee ordering.
Third parameter specify the protocol to be used with protocol families. Different type of protocols are icmp,udp and tcp.
In the end you can simply choose the parameters options as per the requirement. This function will create the socket to start the communication. In next blog we will discuss the other functions as well.
0 Comment(s)