HomePHP Emulating a Basic Web Server with Sockets in PHP
Emulating a Basic Web Server with Sockets in PHP
Do you think that programming sockets with PHP is really hard work? Not at all. If you’re still not convinced, read this final part of the series “Handling sockets with PHP.” In three parts, this series shows how to work with low-level sockets in PHP. It introduces some of the typical tasks, such as creating sockets, as well as reading and writing socket data.
For those who haven’t yet read the second tutorial of this series, allow me to describe in a few words the topics I treated in it. Since using sockets in PHP is a pretty straightforward experience, in the previous article I explained how to create a basic TCP server, which was capable of handling multiple requests coming from the same client application. In this way I eliminated a considerable limitation exposed in the first article.
Despite the rather primitive functionality of the mentioned TCP server, I hope the example I developed before served as a way for you to get a more intimate grounding in how to program low-level sockets in PHP, and of course contributed to expanding your general knowledge of PHP programming.
Also, this sample TCP server used some of the typical socket-related PHP functions, in order to create the corresponding end-point communication channels, as well as handling streams of data for reading and writing client input. With reference to demonstrating how to get the server working, I used Microsoft’s Telnet application included in Windows XP, but because of the versatility of the TCP server, you can use your own client program without having to modify the original socket application.
Having reminded you of the key points regarding the sample TCP server I created in the previous tutorial, it’s time to pay attention to the subject of this final part of the series. In this article, I’ll show you how to use low-level sockets to emulate the functionality of a basic Web server on your own testing machine.
Want to see how this server will be created? Fine, let’s do it together!