HmmZ 0 Report post Posted June 5, 2005 This tutorial is based on a question written in the PHP programming board 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: resource socket_create ( int domain, int type, int protocol) and a more familiar seen example (php style): $socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP); *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_INET - IPv4 Internet based protocols. TCP and UDP are common protocols of this protocol family. 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, SOL_TCP is a constant protocol of TCP. NOTE: You can also use getprotobyname("TCP"), or simply "0" instead of SOL_TCP,whatever rocks your boat. Connection with a socketYou created the socket, now you need a connection to ultimately use it. socket_connect is the function that enables you to connect to other computers using a socket, the syntax for this function: bool socket_connect ( resource socket, string address [, int port]) 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 6667NOTE: 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 $socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP); $connection = socket_connect($socket,'irc.freenode.net',6667); Reading from a socketReading 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: string socket_read ( resource socket, int length [, int type]) let's expand our current codes with this function: $socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);$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: int socket_write ( resource socket, string buffer [, int length]) 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: $socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP); // Create the Socket$connection = socket_connect($socket,'irc.freenode.net',6667); // Connect to freenodesocket_write($socket,"USER RHAP RHAP RHAP :RHAP\r\n"); // Send the Username to freenodesocket_write($socket,"NICK MyFirstSocket \r\n"); // Change our nicknamesocket_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: $socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP); 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: bool socket_bind ( resource socket, string address [, int port]) 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: socket_bind($socket,'localhost',1111); 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: socket_bind($socket,'111.111.111.111',1111); 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 = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); // Create our socket.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 connectionswhile($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. Share this post Link to post Share on other sites
HmmZ 0 Report post Posted June 7, 2005 Any feedback? Share this post Link to post Share on other sites
Takeshi 0 Report post Posted June 7, 2005 Nice tutorial. I agree that it is an underused feature. An even more underused feature is "command-line php" which can be used to create server and other applications under *nix console.PHP isn't just a web scripting language. Its a full-blown programming language. Share this post Link to post Share on other sites
ausnrl 0 Report post Posted June 8, 2005 Seems quite interesting il try it one day!!!! Share this post Link to post Share on other sites
marcp 0 Report post Posted June 16, 2005 That's a great tutorial! Thanks.I've been trying to make a PHP ping function. The problem I have is that Windows doesn't give PHP permission to use raw sockets. Do you know if there's a way to allow PHP to use ICMP? Or is there another way I can check to see if a system is up/down via PHP? Share this post Link to post Share on other sites