Home arrow PHP arrow Page 3 - An Introduction to Sockets in PHP

Reading and writing socket data: creating a simple web-based client application - PHP

Working with low-level sockets can be a painful task in PHP. Fortunately, PHP offers a nice library of socket handling functions to make your life easier. If you do a lot of network programming in PHP, you will want to check out this three-part series, which starts with the basics and takes you through more advanced approaches. This article is the first one in the series.

  1. An Introduction to Sockets in PHP
  2. The basics of low-level sockets: developing an illustrative example
  3. Reading and writing socket data: creating a simple web-based client application
  4. Reusing the TCP server: defining the "createSocketServer()" function and "SocketServer" class
By: Alejandro Gervasio
Rating: starstarstarstarstar / 33
May 22, 2006

print this article



Creating a client application to test the sample TCP server is a straightforward task. Essentially, I'll build a web-based script that will connect to the aforementioned server and send a string of data via an online form. In accordance with the logic implemented on the server, this string should be returned to the client in uppercase, and finally displayed on the browser.

Here's the snippet of code that creates a simple web-based client, handy for testing the TCP server in question:

// check if form was submitted
    // open client connection to TCP server
        trigger_error('Error opening socket',E_USER_ERROR);
    // write message to socket server
    // get server response
    // close socket connection
    echo '<h1>You entered the following message in
lowercase :'.$ret.'</h1>';
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-
8859-1" />
<h1>Enter your message here</h1>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
<input type="text" name="message" size="30" /><br />
<input type="submit" name="send" value="Send Value" />

As you can appreciate, the above script uses the PHP "fsockopen()" function, in order to connect to the recently created TCP server and inject into the socket the string entered in the online form. After receiving the uppercase string, the script displays this data and finishes its execution.

To understand the functionality of the sample TCP server, please take a look at the following screen shots, which illustrate both the client petition and the server response respectively:

As depicted above, the images show the TCP server in action. First, the client submits a message via the online form, and secondly the server turns the input to an uppercase string, which is finally echoed to the browser.

In addition, before I explain more concepts related to socket programming in PHP, I want to clarify one important point regarding the example: you should run the script that creates the TCP server from the PHP command line, instead of using your browser, in order to prevent the system from hanging. This can be done easily by using the CLI.exe file that comes with your PHP distribution and typing the following commands in your system shell:

q- tcpserver.php

The following screen shot shows how to start the TCP server using the PHP command line on a Windows system:

Assuming that the TCP server has been coded in a file called "tcpserver.php," the above command should run this file and start the server (the preceding q flag tells the PHP interpreter to suppress the "Content-type" HTTP header). After making sure the server is up and running, you can point your browser to the PHP file that builds the corresponding client, and run the pertinent file.

Right, now you see how the sockets I created in the previous section do their job, reading and writing the corresponding data. Since the example TCP server has been created with a few lines of procedural code, it'd be very convenient to encapsulate the whole source code within a compact function (and incidentally within a class), so the server can be reused as many times as needed.

If you're interested in learning how this can be achieved, please read the next few lines of the article.

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: