Sockets

Surprising as this may come to some of you, your connection to the Internet is not handled by well-trained pixies with semaphore flags. Instead, things are much more detailed, involving protocols, ports, and sockets - these form the core of your connection to the Internet.

While it is out of the remit of this book to into too much detail about network infrastructure, you do at least need to know what protocols, ports, and sockets are. Protocols are like languages, defining how two computers can talk to each other, and there are hundreds of protocols to perform all varieties of operations - there is a protocol for file transfer (File Transfer Protocol, FTP), a protocol for transferring web pages (Hypertext Transfer Protocol, HTTP), a protocol for network management (Simple Network Management Protocol, SNMP), and many more.

Each protocol has a set of ports that it uses - these are theoretical openings in your computer's Internet connection where clients can connect to, are numbered 1 to 65535, of which the first 1023 are considered reserved to administrative users. By default your PC "listens" to no ports, meaning that it ignores all incoming connections. However, if you run a web server, it will open up port 80 - this is the port for HTTP, where your web server will listen for requests for web pages. The first 1023 ports are mostly used already, which means if you want to use a port for a new service you have written, it is best that you use a number above 1024.

Sockets are the literal connectors between a port and a program, sort of how a plug socket connects an appliance to the electricity grid in your house. Management of sockets in PHP comes in two flavours: easy and hard. As per usual, the easy option is not as flexible as the hard option, but it is much faster to get started with. We are going to be covering both here because both have their own uses.

 

Want to learn PHP 7?

Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!

If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!

Next chapter: Sockets are files >>

Previous chapter: Networks

Jump to:

 

Home: Table of Contents

Copyright ©2015 Paul Hudson. Follow me: @twostraws.