Inspiration
Introduction
Sockets, an underused function in PHP, is a function that enables you to open connection to other peoples computers and vice versa. By sending commands to the operating server, it will first see if there's response, then, it sends or receives data that the operating server has available.
Creation of a socket
Of course, a first step in using sockets, is creating it, this is done by:
and a more familiar seen example (php style):
*The first parameter you see in the brackets, AF_INET is the domain. The PHP Manual states the following regarding domains, there are 2 domains:
AF_UNIX - Local communication protocol family. High efficiency and low overhead make it a great form of IPC (Interprocess Communication).
AF_INET is the domain that is used for internet_based sockets, so you'll most likely be using this most often.
*The second parameter in the brackets, SOCK_STREAM is the type of socket, SOCK_STREAM is full duplex, meaning you can read ánd write to the socket.
*The last parameter in the brackets, SOL_TCP is the protocol, PHP knows 3 major protocols, icmp, udp and tcp[i], SOL_TCP is a constant protocol of TCP.
Connection with a socket
You created the socket, now you need a connection to ultimately [i]use it.
socket_connect is the function that enables you to connect to other computers using a socket, the syntax for this function:
The socket you created a few lines ago was assigned to the $socket variable, wich we can easily use in the socket connection syntax, now that we have both the create line aswell as the connection, we can connect to something, in this tutorial we will be connecting to an IRC server, on port 6667
NOTE: The port and irc server has been taken from an item i've read about sockets, since i don't have my own irc server, and this connection can be tested in reality
$connection = socket_connect($socket,'irc.freenode.net',6667);
Reading from a socket
Reading is of course an important element of sockets, as they are the actual interactivity with the client versus host, the socket_read() is the function that reads from a socket, the syntax for this function:
let's expand our current codes with this function:
$connection = socket_connect($socket,'irc.freenode.net',6667);
while($data = socket_read($socket,2046,PHP_NORMAL_READ))
//listen for any data, and echo that data out
{
echo $data;
}
NOTE: PHP_NORMAL_READ is a type of reading that will stop whenever the line terminates with a \r\n.
Writing to a socket
So far we made a socket, created a connection, and gave our script the socket_read() function. Now we need to reply to these readings with socket_write(). The syntax for this function:
Of course, the resource socket is the creation of the socket, wich was assigned to the $socket variable, the string buffer is simply what you want to write to the socket. let's add this to our existing code:
$connection = socket_connect($socket,'irc.freenode.net',6667); // Connect to freenode
socket_write($socket,"USER RHAP RHAP RHAP :RHAP\r\n"); // Send the Username to freenode
socket_write($socket,"NICK MyFirstSocket \r\n"); // Change our nickname
socket_write($socket,"JOIN #TEST \r\n"); // Join the channel PHPFREAKS!!!
while($data = socket_read($socket,2046)) // read whatever IRC is telling us
{
echo $data;
}
USER, NICK and JOIN are all irc commands that enable you to define the username, the nickname and what channel to join, in this example it's the channel #TEST, as you may have noticed, \r\n have been used, wich ends a line.
Listening for connections on your own computer
So far we created a socket, connected to a socket, written and read from a socket, now we are going to create a socket for users to connect and how to accept them when they try to connect, first we recreate our socket, no difference from the first socket creation:
Now comes the difference, instead of connection to a socket, you bind it to your own machine, socket_bind() is our function, with as syntax:
as you can see, the parameters are pretty much the same as our other socket, just in a different function (socket_bind), let's translate this into PHP:
this binds the socket to the localhost on port 1111. A normal bind of course doesn't usually connect to localhost, so we'll replace that with a simple IP address: 111.111.111.111, the function would then look like this:
Now that you've bound your socket to your computer, let's listen from incoming connections, in this tutorial the way we're using the listen function can only accept 1 connection at a time. socket_listen() will listen and socket_accept can accept the connection. Let's add this to our socket code:
socket_bind($socket,'111.111.111.111',1111); //bind the socket to our IP address, on port 1111.
socket_listen($socket); // listen for any incoming connections
while($connection = socket_accept($socket)) // accept any incoming connection and write to the socket.
{
socket_write($connection,'You\'ve successfully connected to my computer!\r\n');
}
in this code, the socket listens for connections and once it detects one, it will send 'You've successfully connected to my computer' to the socket.
Try it with your own IP on port 1111
That concludes an introduction in PHP Sockets, the socket you just made, connected to, bound to, listened to, read and wrote to, is basically an IRC Bot (don't worry it's nothing illegal
Anyway, I hoped this would make you understand a bit about what sockets do and how they are used in PHP, an IRC bot is just an example of what a socket can do. Feel free to experiment and see what comes out, you never know what great things you come up with
I hope this helped you a bit.

