Solved

Multiple requests with a single socket using fsockopen and ssl.

Posted on 2008-06-23
3
1,396 Views
Last Modified: 2013-12-25
I am writing a time sensitive script to submit socket-based (XML) requests at a certain time, in competition with other people running similar scripts.  There are time limits on how often I can send requests and a limit that I can only have one connection open at a time.  This leads me to belive that it's possible to have more than one request per connection. Is this correct? if so, how do I implement it?

Or, is it the case that a socket always get closed when a response is sent?

My best guess at implementation is below, but feels far from right.

Thanks!

$fp = fsockopen(example.com, 443);
 

$out = "GET /page1.html HTTP/1.1\r\n";

$out .= "Host: https://example.com\r\n";

$out .= "Connection: Close\r\n\r\n";

fwrite($fp, $out);

  while (!feof($fp)) {

  echo fgets($fp, 128);

}
 
 

$out = "GET /page2.html HTTP/1.1\r\n";

$out .= "Host: https://example.com\r\n";

$out .= "Connection: Close\r\n\r\n";

fwrite($fp, $out);

  while (!feof($fp)) {

  echo fgets($fp, 128);

}
 

fclose($fp);

Open in new window

0
Comment
Question by:mohrc
3 Comments
 
LVL 20

Expert Comment

by:virmaior
ID: 21855792
I don't believe what you are doing will work..

it would depend on how their socket is coded.

At the very least, you will need to send an EOF (or equivalent device) to tell it when to end a statement.  

As written, you are not signaling the server that a new request is being made so the subsequent requests will not work.
0
 
LVL 51

Accepted Solution

by:
ahoffmann earned 250 total points
ID: 21868300
> $fp = fsockopen(example.com, 443);
are you "speaking" plain text on a SSL port? Just wondering?

Back to you question:
> .. it's possible to have more than one request per connection.
yes, use:

$out .= "Connection: keep-alive\r\n\r\n";


but depends on the server if it allows it.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
The viewer will learn how to dynamically set the form action using jQuery.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now