Wednesday, March 13, 2013

Introduction to Socket Programming with PHP

Introduction to Socket Programming with PHP

Are you one of those sorry souls who has desperately tried to learn network programming in C, but never had the stamina to tame the wild beast? I was for a long time until I finally decided that I had to learn it -- not only for my own satisfaction, but because I bumped into a problem that required some network programming. After a few sweaty nights, I became completely overwhelmed. Then I realized that my favorite programming language of all time, PHP, included a basic socket implementation. Hallelujah! I was saved!
While PHP's primary goal is to be a high-quality programming language for developing web-based applications, it has many neat features that could convince you to use it for other tasks as well -- for example, network programming.

Understanding sockets

What PHP unfortunately won't do for you is give you a basic understanding of what sockets really are, and how you can use them to create communication between two computers. So we'll have to go through that part ourselves. It's really not that hard, and hopefully this article will make it even easier.
You know what a network is, right? Know what a socket is? Not really? Sockets are just an abstraction of a low-level feature of the operating system. However, we don't need to go that deep to use sockets in our programs. We just need to get a basic understanding of how the socket philosophy works.
To explain this, I'll compare the process to a videogame, a game that resembles the real world quite closely, except that in the game all computer networks are organized by small men. You can control these small men to do exactly what you want, but there is a drawback. These men don't have any intelligence of their own -- they will do exactly as they are told, no matter how wrong that happens to be.
Diagram of the game.
Figure 1: Controlling a network with small men.
I think we need a description of the game -- what are the goals here? To begin with, you need to make one man in "Computer I" go into the network connection and wait for some data to arrive. When the data does arrive, he's ready to open the hatch to let the data in. When this data comes through the hatch, someone has to lead it to the correct destination inside the computer. However, if you let the man in charge of the hatch do that job as well, who would make sure that the hatch is opened if additional data wants to get in?
There's actually a really simple solution to this problem: Just ask another man to lead whatever came in through the hatch to its destination inside the machine. In doing this, you are letting the first man continue to concentrate on his hatch job.
But we're not done yet: You also have to make one of the men in Computer II go to the hatch on his end to make sure the hatch is opened when something wants to get out. I'll play the first game, so you can see how it works. I start out by pushing a few buttons, and move the joystick around and voilĂ  -- we have Figure 2.
Diagram.
Figure 2: Here everything is in place.
But what is this?! Where's the third man? Take it easy, he'll be there. But wouldn't it be a waste of good working power to just let him stand there until something arrives? Wouldn't it be better to let him continue doing whatever it is he's doing and call for him when we actually have something for him to do?
After some more pushing, slamming, and banging around on the controls, I've told Computer II that I need to get some data, and have directed the man in Computer II open the hatch so the data can get out. In Figure 3, the data is on its way.
Diagram.
Figure 3: Data on its way!
As you can see, a chunk of data has now passed through the hatch of Computer II, and is on its way to "Computer I" (and don't wonder how that large chunk came through the tiny hatch!). This is coming along pretty well, just move the controls ... like ... that ... and if I push this ... and ... Ah! There you have it:
Diagram.
Figure 4: Data has arrived!
Here, I have managed to get the data from Computer II, send it through the network connection, and then into Computer I. I also called for a second man in Computer I to take care of the data. As you can see, he's just about to get the data in place. I must say, I'm pretty satisfied with myself.

No comments:

Post a Comment