Manageing multiple socket connections

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gabriele Farina

    Manageing multiple socket connections

    Hi,

    I'd like to manage multiple socket connection without having to use
    Threads. I'd like to manage every connection when it is returned from
    socket_Accept() , without having to wait for the current running
    tonnection to stop. How can I do that?

    bye
  • CountScubula

    #2
    Re: Manageing multiple socket connections

    "Gabriele Farina" <darkbard@exten ding-php.net> wrote in message
    news:Rjh2c.5671 $O31.242634@new s4.tin.it...[color=blue]
    > Hi,
    >
    > I'd like to manage multiple socket connection without having to use
    > Threads. I'd like to manage every connection when it is returned from
    > socket_Accept() , without having to wait for the current running
    > tonnection to stop. How can I do that?
    >
    > bye[/color]

    Set socket non blocking, and use one socket to listen and others to accept.

    You will have to build your own scheduling system to handle multiple
    sockets, thus psuedo threads.

    Possible yes, difficult to exmplain too. You must have a good sense of how
    to multi thread an app when no multithread systems are inplace, for example,
    using DOS back in the days with just a standard C compiler.

    Basicaly, you will need to make a schedular, stacks, output ques, input
    ques, and have your proccessing of data seperate from transactions, as if
    your proccessing was a thread of its own.

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


    Comment

    • CountScubula

      #3
      Re: Manageing multiple socket connections

      "Gabriele Farina" <darkbard@exten ding-php.net> wrote in message
      news:Rjh2c.5671 $O31.242634@new s4.tin.it...[color=blue]
      > Hi,
      >
      > I'd like to manage multiple socket connection without having to use
      > Threads. I'd like to manage every connection when it is returned from
      > socket_Accept() , without having to wait for the current running
      > tonnection to stop. How can I do that?
      >
      > bye[/color]

      Ok, I was thinking, testing a bit, you could write good loop/schedular that
      will turn your app into an event driven system, thus easing the seperation
      of the proccesing/management.

      when data arrives on a socket, you could have it do a call back to your own
      function, with a socket id, and data.

      when writing data to the sockets, remeber to write to all sockets with a
      schedular thus eliminating any holdup from large bits of data going to one
      socket. (example, run a loop and send a couple of bytes from each out buffer
      to each socket)


      Ok, i'm done, it was just my 2 cents.

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


      Comment

      • Gabriele Farina

        #4
        Re: Manageing multiple socket connections

        CountScubula ha scritto:[color=blue]
        > "Gabriele Farina" <darkbard@exten ding-php.net> wrote in message
        > news:Rjh2c.5671 $O31.242634@new s4.tin.it...
        >[color=green]
        >>Hi,
        >>
        >>I'd like to manage multiple socket connection without having to use
        >>Threads. I'd like to manage every connection when it is returned from
        >>socket_Accept (), without having to wait for the current running
        >>tonnection to stop. How can I do that?
        >>
        >>bye[/color]
        >
        >
        > Ok, I was thinking, testing a bit, you could write good loop/schedular that
        > will turn your app into an event driven system, thus easing the seperation
        > of the proccesing/management.
        >
        > when data arrives on a socket, you could have it do a call back to your own
        > function, with a socket id, and data.
        >
        > when writing data to the sockets, remeber to write to all sockets with a
        > schedular thus eliminating any holdup from large bits of data going to one
        > socket. (example, run a loop and send a couple of bytes from each out buffer
        > to each socket)
        >
        >
        > Ok, i'm done, it was just my 2 cents.
        >
        > --
        > Mike Bradley
        > http://www.gzentools.com -- free online php tools
        >
        >[/color]


        Ok, tnx a lot ... I read your post, but I can't understand how can I do
        correctly what you said to me.
        Pheraps is should work like this:
        I have to save and array of reading and writing connection, and then
        manage anyone of them using a callback function. I should use
        socket_select to emulate select() system call, but I can't undestand
        correctly the code I have to write.

        Could you post here some exaple code so I can edit it to fit my needs?
        I got a good knowledge of PHP, but I lack in socket management ...

        tnx a lot for the answer, bye

        Comment

        • CountScubula

          #5
          Re: Manageing multiple socket connections

          "Gabriele Farina" <darkbard@exten ding-php.net> wrote in message
          news:6Ei2c.5083 $z23.217710@new s3.tin.it...[color=blue]
          > (super snip)
          > Ok, tnx a lot ... I read your post, but I can't understand how can I do
          > correctly what you said to me.
          > Pheraps is should work like this:
          > I have to save and array of reading and writing connection, and then
          > manage anyone of them using a callback function. I should use
          > socket_select to emulate select() system call, but I can't undestand
          > correctly the code I have to write.
          >
          > Could you post here some exaple code so I can edit it to fit my needs?
          > I got a good knowledge of PHP, but I lack in socket management ...
          >
          > tnx a lot for the answer, bye[/color]


          OK, I had a long drinking night, so I will write some easy part here, spin
          this off into other parts.

          the array $socketID[] will hold a resource ID for each socket
          and the string $funCallBackSck Recv hold the name of the call back function
          for an event driven recieve

          when you need to write data, use function sckWrite() to send data, it will
          que it up in a buffer and return imediately


          // main management loop
          // everything has to be done in slices, thus psuedo thread
          // this wil ensure all sockets *apear* to send/recieve data concurently

          $funCallBackSck Recv = "callBack_Socke tRecieve";

          while (true)
          {
          global $funCallBackSck Recv;
          global $socketID;
          global $socketBufferOu t;

          foreach ($socketID as $sckID)
          {

          // some code here

          // more code here

          // process socket recieved data
          if ($buf = socket_read ($msgsock, 2048))
          {
          $funCallBackSck Recv($sckID,$bu f);
          }


          // proccess socket output buffers, send 1024 bytes each
          if ($socketBufferO ut[$sckID] != "")
          {
          $out = substr($socketB ufferOut[$sckID],0,1024);
          $socketBufferOu t[$sckID] = substr($socketB ufferOut[$sckID],1024);
          socket_write($s ckID,$out);
          }

          // end of foreach socketID
          }

          // end of while true
          }


          // buffer output
          function sckWrite($sckID ,$strData)
          {
          global $socketBufferOu t;
          $socketBufferOu t[$sckID] .= $strData;
          }

          // this is a function to proccess received data on a socket
          // its a psuedo event driven function
          function callBack_Socket Recieve($sckID, $data)
          {
          // do something here with incoming data
          }


          OK, off the top of my head, I would say add another array called $sckState[]
          to use as a way to track the current state of a socket, ie: "WAITING" "BUSY"
          "DONE" etc... depends on the app you are trying to make, and use this state
          to detirmine whether it should be acepting connections etc...

          if you look here:
          http://www.gzentools.com/snippetview...l&v=mxmail.php you can see an
          example of how I use variable $mState to hold the status of a socket. (only
          single socket in this case)

          Ok, my head hurts, I hope this is enough to get you off and running. Also,
          if I may ask, and it is no secret, what type of service are you writing?
          There might be an easier way. Sometimes you can spawn a child process that
          is in its own thread.

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


          Comment

          • Gabriele Farina

            #6
            Re: Manageing multiple socket connections

            CountScubula ha scritto:
            [color=blue]
            > Ok, my head hurts, I hope this is enough to get you off and running.[/color]
            Also,[color=blue]
            > if I may ask, and it is no secret, what type of service are you writing?
            > There might be an easier way. Sometimes you can spawn a child process that
            > is in its own thread.[/color]

            Ok, I think I understand what does you mean.
            I have to write an HTTP server because I need some features I can't add
            to Apache or other servers (I don't want to spend time writing C code
            and understanding apache-mod-system) ... I don't know if and HTTP server
            needs threads, but I think it should be better to make a single thread
            manage only a defined amount of connections; so I don't know if your
            method is good to solve my problems, because I don't know if I gain
            advantages using it. If you can, give me some theoric help! :)

            bye

            Comment

            • CountScubula

              #7
              Re: Manageing multiple socket connections

              ok, two things, fist some more theory, and mind numbing stuff :)

              I am currently working on an app the has 125 sockets (single thread)

              I then have a loop that is endless, like what I showed you, and within that
              loop I have a huges select case (swtich in php)

              I only do small bits of proccessing on each interation of the loop, thus
              giving a good apearance of psuedo threads (my way of saying, not a real
              thread, but looks damn good!)

              here is a snippet of what I am working on:

              case "STATE_CONNECTE D"
              sockWrite(index ,"Connected, RBS v1.0 \r\n"
              .state = "STATE_SENT_HEA D"

              case "STATE_CONNECTI NG"
              if (socketConnecte d(index)) {
              .state = "STATE_CONNECTE D"
              }

              case "STATE_READ Y"
              .state = "STATE_CONNECTI NG"
              socketConnect(i ndex)


              as you can see, no one socket can dominate, becouse it will loop through all
              125 sockets before it can proccess the next state of a socket.

              Ok, now for the easy second part.
              If you just want some functionality that apache doesnt have, wrap it into a
              php script, and call it a day. :)
              If you can give me more, I could possibly point you in the right direction.


              --
              Mike Bradley
              http://www.gzentools.com -- free online php tools
              "Gabriele Farina" <darkbard@exten ding-php.net> wrote in message
              news:xFu2c.9109 $O31.422515@new s4.tin.it...[color=blue]
              > CountScubula ha scritto:
              >[color=green]
              > > Ok, my head hurts, I hope this is enough to get you off and running.[/color]
              > Also,[color=green]
              > > if I may ask, and it is no secret, what type of service are you writing?
              > > There might be an easier way. Sometimes you can spawn a child process[/color][/color]
              that[color=blue][color=green]
              > > is in its own thread.[/color]
              >
              > Ok, I think I understand what does you mean.
              > I have to write an HTTP server because I need some features I can't add
              > to Apache or other servers (I don't want to spend time writing C code
              > and understanding apache-mod-system) ... I don't know if and HTTP server
              > needs threads, but I think it should be better to make a single thread
              > manage only a defined amount of connections; so I don't know if your
              > method is good to solve my problems, because I don't know if I gain
              > advantages using it. If you can, give me some theoric help! :)
              >
              > bye[/color]


              Comment

              • CountScubula

                #8
                Re: Manageing multiple socket connections

                oops, change the line
                if ($buf = socket_read ($msgsock, 2048))
                to read
                if ($buf = socket_read ($sckID, 2048))


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


                Comment

                Working...