PHP Networks
Networks
The Internet is really just one big network, albeit the largest and most convoluted one ever created, and there are a variety of different protocols that allow communication. The most common protocol is HTTP, followed by FTP, but there are literally dozens of others, each with their own specific purpose.
PHP has a number of ways to work over a network - the most common protocols have special functions to make often-used functionality easy, but it is possible to use PHP to write any kind of data over any kind of protocol.
The topics covered in this chapter are:
*
What sockets are, and basic socket use
*
How to use sockets outside of HTTP
*
How to create a basic server using PHP
*
Creating a web server
*
Helpful network-related functions
*
HTTP-specific and FTP-specific functions
*
The Curl library
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.
The Internet is really just one big network, albeit the largest and most convoluted one ever created, and there are a variety of different protocols that allow communication. The most common protocol is HTTP, followed by FTP, but there are literally dozens of others, each with their own specific purpose.
PHP has a number of ways to work over a network - the most common protocols have special functions to make often-used functionality easy, but it is possible to use PHP to write any kind of data over any kind of protocol.
The topics covered in this chapter are:
*
What sockets are, and basic socket use
*
How to use sockets outside of HTTP
*
How to create a basic server using PHP
*
Creating a web server
*
Helpful network-related functions
*
HTTP-specific and FTP-specific functions
*
The Curl library
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.
Comments
Post a Comment