Home arrow PHP arrow Page 2 - Building A Document Request Protocol Part 2/2

Building A Document Request Protocol Part 2/2

In this article, Mitchell shows us how to implement our custom SARP protocol that we created yesterday. He uses PHP and its various socket functions to send SARP commands to our SARP server to both retrieve articles and add new articles to the servers Access database.

Author Info:
By: Mitchell Harper
Rating: 5 stars5 stars5 stars5 stars5 stars / 2
February 24, 2002
  1. · Building A Document Request Protocol Part 2/2
  2. · Connecting to our SARP server
  3. · Logging in
  4. · Using SARP commands
  5. · Adding new content using SARP commands
  6. · Conclusion

print this article

Building A Document Request Protocol Part 2/2 - Connecting to our SARP server
(Page 2 of 6 )

PHP supports socket connections using either the TCP or UDP protocols. The fsockopen function creates a socket connection to another computer, allowing us to specify that computers IP address, required port, and even a connection time out. Its signature looks like this:

int fsockopen (string hostname, int port [, int errno [, string errstr [, float timeout]]])

Fsockopen returns 1 if the socket was created successfully, and 0 if it failed. The errno and errstr variables are passed in by reference, and will contain the details of an error if it occurs.

To connect to our SARP server (remember that our "SARP server" is simply the VB app that we created in part one of this article running on the local machine), we can define some variables and use fsockopen to create a socket connection. Create a new file named sarp_test.php and save it in a directory that either Apache/IIS can process. Enter all of the code described over the next couple of sections into the file.


$xServer = "localhost";

$xPort = 2002;

$xTimeOut = 10;

$xUser = "XUser";

$xPass = "XPass";

$sarpHandle = fsockopen($xServer, $xPort, &$errNo, &$errStr, $xTimeOut);



die("Connection to \"$xServer\" failed.");




echo "Connected<br>";

As you can see, we're creating a socket that binds to port 2002 on the server and has a timeout of ten seconds, meaning that if a connection to the server can't be established within ten seconds, then fsockopen will return false.

If the socket couldn't be created, then the die() function outputs a connection failed message to the browser and terminates the script. On the other hand, if the connection succeeded, then we can place code inside of the else block to send data to and from that server.
blog comments powered by Disqus

- Removing Singletons in PHP
- Singletons in PHP
- Implement Facebook Javascript SDK with PHP
- Making Usage Statistics in PHP
- Installing PHP under Windows: Further Config...
- File Version Management in PHP
- Statistical View of Data in a Clustered Bar ...
- Creating a Multi-File Upload Script in PHP
- Executing Microsoft SQL Server Stored Proced...
- Code 10x More Efficiently Using Data Access ...
- A Few Tips for Speeding Up PHP Code
- The Modular Web Page
- Quick E-Commerce with PHP and PayPal
- Regression Testing With JMeter
- Building an Iterator with PHP

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials