2

I am new in http connections. The thing I want to realize is that the server should send some data (notifications) to the client periodically by persistent connection.

I wrote a code in server side by php like:

<?php

set_time_limit(0);
header('Connection: keep-alive');
$i = 0;

while($i < 10){

    echo "Hello$i<br/>";
    sleep(5);
    $i++;
}

?>

and tried to connect to the server by java:

public static void main(String[] args) throws Exception {

    URL oracle = new URL("http://localhost/connection.php");
    URLConnection yc = oracle.openConnection();
    BufferedReader in = new BufferedReader(new InputStreamReader(
            yc.getInputStream()));
    String inputLine;
    while ((inputLine = in.readLine()) != null)
        System.out.println(inputLine);
    in.close();
}

I expected to get content from the server every five seconds like following:

Hello0<br/>
Hello1<br/>
...

but instead of this the java client is waiting 50 seconds. and printing:

Hello0<br/>Hello1<br/>Hello2<br/>Hello3<br/>Hello4<br/>Hello5<br/>Hello6<br/>Hello7<br/>Hello8<br/>Hello9<br/>

I want the server send notifications itself. instead of the client connect to the server every five seconds.

Vanguard
  • 1,336
  • 1
  • 15
  • 30
  • You're never flushing your connection server-side. – chrylis -cautiouslyoptimistic- Aug 21 '16 at 05:04
  • @chrylis and how to do that? If server flushes it won't it just lose connection? – Vanguard Aug 21 '16 at 05:08
  • before the line with `sleep(5)`, add `flush();` and `ob_flush();` – Aryeh Beitz Aug 21 '16 at 06:57
  • @user3548935 I have tried already. but java anyway waiting 50 seconds and then printing again. or i should change the java code too? – Vanguard Aug 21 '16 at 07:05
  • Take out the while loop and sleep from the php code. Instead, have the java code poll the php file every 5 seconds. Assuming the php file will have different data each time. – Aryeh Beitz Aug 21 '16 at 07:48
  • requests only go in 1 direction in http, and that's from the client to the server. You can't guarantee what a client, or a proxy will do to the traffic you are generating. The common way to do this is with long polling. – Adrien Aug 21 '16 at 21:48

2 Answers2

0

Flushing the connections seemed really good idea. But I think I found better solution. Instead of keeping connection with unlimited timeout, I think it is better to make persistent connection with 5 (N minutes) minutes timeout. It is better because when the user will be offline unexpectedly, the server will keep the connection alive anyway. and it is not good. That's why I am going to make 5 (this number is optional) connections for notification. That is the server will use first one for notification and closes the connection after sending request, and the rest 4 connections will be on duty. When the client (or java client) will receive the notification, it will make new connection to fill missing part or the connection times out. and the client will be notified immediately every time (of course if connected to the internet). If someone has better solution I will be happy to see that.

Vanguard
  • 1,336
  • 1
  • 15
  • 30
0
  1. It's really unnecessary to add Connection: keep-alive header in response for HTTP/1.1 server, UNLESS for backward compatibility.
  2. No matter how long or many times you sleep in that loop, it's seen as ONE request by the client nevertheless.
  3. with that being said, your client snippet, in fact, only make ONE request to http://localhost/connection.php, and it's impossible to reuse URLConnection in order to dispatch another request(achieving persistent).

to sum up:

Persistent Connection behaviour is handled at transport layer (TCP), more specifically, you are required to reuse a client socket for multiple request to the same host plus some other requirements specified in HTTP/1.1.

Go and find some projects that are suitable for your needs, don't reinvent the wheel.

Jerry Chin
  • 657
  • 1
  • 8
  • 25