PHP-Sockets: only mozilla works with open connection?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dennis Hueckelheim

    PHP-Sockets: only mozilla works with open connection?

    Hallo everybody,

    I have a problem with a little socket script.


    ----- snipp -----
    $out_clients[$i]['socket'] = socket_accept($ socket_out);
    $http_welcome = "HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n";
    $http_welcome .= "<html><head><t itle>test</title></head><body><h1> Test</h1>";
    socket_write($o ut_clients[$i]['socket'], $http_welcome);
    ----- snipp -----

    Mozilla works fine: If I open http://server:2323 ($socket_out listens
    on this port), I'll get a connection, Mozilla displays the simple page
    and the connection keeps open. Every time I write new data into the
    socket, Mozilla displays them.

    But MS IE doesn't work! I only see a blank page. If I close the socket
    after sending the header and page, IE will displays the page.

    Has anyone an idea what my problem is?

    Greetz, TIA


    Dennis
  • Guillaume Brocker

    #2
    Re: PHP-Sockets: only mozilla works with open connection?

    Dennis Hueckelheim wrote:
    [color=blue]
    > ----- snipp -----
    > $out_clients[$i]['socket'] = socket_accept($ socket_out);
    > $http_welcome = "HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n";
    > $http_welcome .= "<html><head><t itle>test</title></head><body><h1> Test</h1>";
    > socket_write($o ut_clients[$i]['socket'], $http_welcome);
    > ----- snipp -----
    >
    > Mozilla works fine: If I open http://server:2323 ($socket_out listens
    > on this port), I'll get a connection, Mozilla displays the simple page
    > and the connection keeps open. Every time I write new data into the
    > socket, Mozilla displays them.
    >
    > But MS IE doesn't work! I only see a blank page. If I close the socket
    > after sending the header and page, IE will displays the page.
    >
    > Has anyone an idea what my problem is?[/color]

    Since you have not specified the *Content-length* field in the http
    header, IE is still waiting for data before displaying the result where
    Mozilla doesn't. When you close the socket on the server side, IE knows
    that no more data will be delivered and then it displays at last the result.

    So, just add a *Content-length* header field containing the length in
    bytes of the entity that will be sent and your are done.

    --
    Guillaume Brocker

    Comment

    • CountScubula

      #3
      Re: PHP-Sockets: only mozilla works with open connection?

      "Dennis Hueckelheim" <dennis.hueckel heim@issdh.com> wrote in message
      news:4f11b945.0 402180531.4c83f 93a@posting.goo gle.com...[color=blue]
      > Hallo everybody,
      >
      > I have a problem with a little socket script.
      >
      >
      > ----- snipp -----
      > $out_clients[$i]['socket'] = socket_accept($ socket_out);
      > $http_welcome = "HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n";
      > $http_welcome .=[/color]
      "<html><head><t itle>test</title></head><body><h1> Test</h1>";[color=blue]
      > socket_write($o ut_clients[$i]['socket'], $http_welcome);
      > ----- snipp -----
      >
      > Mozilla works fine: If I open http://server:2323 ($socket_out listens
      > on this port), I'll get a connection, Mozilla displays the simple page
      > and the connection keeps open. Every time I write new data into the
      > socket, Mozilla displays them.
      >
      > But MS IE doesn't work! I only see a blank page. If I close the socket
      > after sending the header and page, IE will displays the page.
      >
      > Has anyone an idea what my problem is?
      >
      > Greetz, TIA[/color]



      As someone pointed out, IE does not know the content length, however, you
      may not know it either, becouse the page may be dynamic, generaly in the
      case of a php server listening to a port.

      What you can do is send a chunked output, which is like lots of little
      content-lengths,

      here is a revised version of your snippet:

      $out_clients[$i]['socket'] = socket_accept($ socket_out);
      $http_header = "HTTP/1.0 200 OK\r\n" .
      "Accept-Ranges: bytes\r\n" .
      "Transfer-Encoding: chunked\r\n" .
      "Content-type: text/html\r\n\r\n";
      socket_write($o ut_clients[$i]['socket'], $http_header);

      $http_body = "<html><head><t itle>test</title></head><body><h1> Test</h1>";
      chunkWrite($htt p_body);

      $http_body = <I>More Stuff</I>";
      chunkWrite($htt p_body);


      function chunkWrite($out _clients[$i]['socket'], $string)
      {
      $hxLen = dechex(strlen($ string)) . "\r\n";
      socket_write($s ock,$hxLen);
      socket_write($s ock,$string);
      }


      --
      Mike Bradley
      http://www.gzentools.com -- free online php tools


      Comment

      • CountScubula

        #4
        Re: PHP-Sockets: only mozilla works with open connection?

        Sorry, I messed up.
        [color=blue]
        > here is a revised version of your snippet:
        >
        > $out_clients[$i]['socket'] = socket_accept($ socket_out);
        > $http_header = "HTTP/1.0 200 OK\r\n" .
        > "Accept-Ranges: bytes\r\n" .
        > "Transfer-Encoding: chunked\r\n" .
        > "Content-type: text/html\r\n\r\n";
        > socket_write($o ut_clients[$i]['socket'], $http_header);[/color]



        Ooops, change this:
        "HTTP/1.0 200 OK"
        to this
        "HTTP/1.1 200 OK"


        --
        Mike Bradley
        http://www.gzentools.com -- free online php tools


        Comment

        • Dennis Hueckelheim

          #5
          Re: PHP-Sockets: only mozilla works with open connection?

          "CountScubu la" <me@scantek.hot mail.com> wrote in message news:<UrPYb.144 26$yb7.11479@ne wssvr29.news.pr odigy.com>...[color=blue]
          > Sorry, I messed up.
          >[color=green]
          > > here is a revised version of your snippet:
          > >
          > > $out_clients[$i]['socket'] = socket_accept($ socket_out);
          > > $http_header = "HTTP/1.0 200 OK\r\n" .
          > > "Accept-Ranges: bytes\r\n" .
          > > "Transfer-Encoding: chunked\r\n" .
          > > "Content-type: text/html\r\n\r\n";
          > > socket_write($o ut_clients[$i]['socket'], $http_header);[/color]
          >
          >
          >
          > Ooops, change this:
          > "HTTP/1.0 200 OK"
          > to this
          > "HTTP/1.1 200 OK"[/color]

          Hm, nice idea, but it doesn't work. Same as before: Mozilla works
          fine, IE don't want work. But now i get more than a blank page. Now I
          get an "Action canceld"-IE error page (sorry, I only know the correct
          english name for that page).

          Also I added some more prints to my script. This is what my script do
          after I opened the URL (http://server:2323) for ONE TIME!!!

          ---- snipp ----
          Create PID-File, PID: 10274
          Try to create sockets
          OutputSocket created, listing on port 2323
          InputSocket created, listing on port 2324
          Setup client input and output handlers
          Server ready. Waiting for connections...
          New OutputClient: xxx.xxx.xxx.xxx :17607 as $pmc->out_clients[0]
          Send $hxLen to $socket
          Send $message to $socket
          Send $hxLen to out_client[0]
          Send $message to out_client[0]
          New OutputClient: xxx.xxx.xxx.xxx :17608 as $pmc->out_clients[1]
          Send $hxLen to $socket
          Send $message to $socket
          Send $hxLen to out_client[0]
          Send $message to out_client[0]
          Send $hxLen to out_client[1]
          Send $message to out_client[1]
          ---- snipp ----

          You see: I press enter for one time but IE will open two connections.
          As little info:

          If write data via $socket it's the html-page. If I write it via
          $out_client[i], i wrote "<i>New client</i><br>".

          Any idea whats wrong?

          lg,
          Dennis

          Comment

          • CountScubula

            #6
            Re: PHP-Sockets: only mozilla works with open connection?

            "Dennis Hueckelheim" <dennis.hueckel heim@issdh.com> wrote in message
            news:4f11b945.0 402181543.644ba fcc@posting.goo gle.com...[color=blue]
            > Hm, nice idea, but it doesn't work. Same as before: Mozilla works
            > fine, IE don't want work. But now i get more than a blank page. Now I
            > get an "Action canceld"-IE error page (sorry, I only know the correct
            > english name for that page).
            >
            > Also I added some more prints to my script. This is what my script do
            > after I opened the URL (http://server:2323) for ONE TIME!!![/color]


            Sorry, I have been packing ang getting ready for business trip, so I have
            not been all here, anyways, I found another mistake on the code I posted:

            chunkWrite should read:
            function chunkWrite($soc k, $string)
            {
            $hxLen = dechex(strlen($ string)) . "\r\n";
            socket_write($s ock,$hxLen);
            socket_write($s ock,$string);
            socket_write($s ock,"\r\n");
            }


            sorry, I didn't understand the rest of your question. I tested this
            chunkWrite function with IE, and it apears to work for me.



            --
            Mike Bradley
            http://www.gzentools.com -- free online php tools



            Comment

            • Dennis Hueckelheim

              #7
              Re: PHP-Sockets: only mozilla works with open connection?


              "CountScubu la" <me@scantek.hot mail.com> schrieb im Newsbeitrag
              news:NWWYb.1461 4>[color=blue]
              >
              > Sorry, I have been packing ang getting ready for business trip, so I have
              > not been all here, anyways, I found another mistake on the code I posted:
              >
              > chunkWrite should read:
              > function chunkWrite($soc k, $string)
              > {
              > $hxLen = dechex(strlen($ string)) . "\r\n";
              > socket_write($s ock,$hxLen);
              > socket_write($s ock,$string);
              > socket_write($s ock,"\r\n");
              > }
              >[/color]

              Wow, it works! ;-) Very much thunks.


              Comment

              • CountScubula

                #8
                Re: PHP-Sockets: only mozilla works with open connection?

                Your welcome

                Sockets and http protocol are one of my forte's :)

                --
                Mike Bradley
                http://www.gzentools.com -- free online php tools
                "Dennis Hueckelheim" <dennis.hueckel heim@issdh.com> wrote in message
                news:c122na$1dd 24b$1@ID-139877.news.uni-berlin.de...[color=blue]
                >
                > "CountScubu la" <me@scantek.hot mail.com> schrieb im Newsbeitrag
                > news:NWWYb.1461 4>[color=green]
                > >
                > > Sorry, I have been packing ang getting ready for business trip, so I[/color][/color]
                have[color=blue][color=green]
                > > not been all here, anyways, I found another mistake on the code I[/color][/color]
                posted:[color=blue][color=green]
                > >
                > > chunkWrite should read:
                > > function chunkWrite($soc k, $string)
                > > {
                > > $hxLen = dechex(strlen($ string)) . "\r\n";
                > > socket_write($s ock,$hxLen);
                > > socket_write($s ock,$string);
                > > socket_write($s ock,"\r\n");
                > > }
                > >[/color]
                >
                > Wow, it works! ;-) Very much thunks.
                >
                >[/color]


                Comment

                • Dennis Hueckelheim

                  #9
                  Re: PHP-Sockets: only mozilla works with open connection?


                  "CountScubu la" <me@scantek.hot mail.com> wrote[color=blue]
                  > Your welcome
                  >
                  > Sockets and http protocol are one of my forte's :)
                  >[/color]

                  Okay, than I hope you can help me with my (I hope the last) problem with
                  this socket story.

                  I created a signal handler for SIGHUP to shutdown my script on a nice way.
                  One of the tasks in the shtudown process is to close all sockets.

                  ---- snipp ----
                  socket_shutdown ($in_socket);
                  socket_close($i n_socket);
                  socket_shutdown ($out_socket);
                  socket_close($o ut_socket);
                  ---- snipp ----

                  I think there is no mistake (and there is no error message), but if a stop
                  my script and start it again within a few seconds I got an error while
                  binding the socket ("Address already in use") and I have to wait some time
                  (up to more than a minute).

                  I already tried to "unbind" the socktes

                  ---- snipp ----
                  socket_bind($in _socket, "0", "0");
                  socket_bind($ou t_socket, "0", "0");
                  ---- snipp ----

                  but I got an error (as hoped for). Whats wrong?

                  lg,
                  Dennis


                  Comment

                  Working...