By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,190 Members | 1,469 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,190 IT Pros & Developers. It's quick & easy.

Manageing multiple socket connections

P: n/a
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
Jul 17 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
"Gabriele Farina" <da******@extending-php.net> wrote in message
news:Rj*******************@news4.tin.it...
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


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
Jul 17 '05 #2

P: n/a
"Gabriele Farina" <da******@extending-php.net> wrote in message
news:Rj*******************@news4.tin.it...
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


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
Jul 17 '05 #3

P: n/a
CountScubula ha scritto:
"Gabriele Farina" <da******@extending-php.net> wrote in message
news:Rj*******************@news4.tin.it...
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

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

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
Jul 17 '05 #4

P: n/a
"Gabriele Farina" <da******@extending-php.net> wrote in message
news:6E*******************@news3.tin.it...
(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

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 $funCallBackSckRecv 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

$funCallBackSckRecv = "callBack_SocketRecieve";

while (true)
{
global $funCallBackSckRecv;
global $socketID;
global $socketBufferOut;

foreach ($socketID as $sckID)
{

// some code here

// more code here

// process socket recieved data
if ($buf = socket_read ($msgsock, 2048))
{
$funCallBackSckRecv($sckID,$buf);
}
// proccess socket output buffers, send 1024 bytes each
if ($socketBufferOut[$sckID] != "")
{
$out = substr($socketBufferOut[$sckID],0,1024);
$socketBufferOut[$sckID] = substr($socketBufferOut[$sckID],1024);
socket_write($sckID,$out);
}

// end of foreach socketID
}

// end of while true
}
// buffer output
function sckWrite($sckID,$strData)
{
global $socketBufferOut;
$socketBufferOut[$sckID] .= $strData;
}

// this is a function to proccess received data on a socket
// its a psuedo event driven function
function callBack_SocketRecieve($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
Jul 17 '05 #5

P: n/a
CountScubula ha scritto:
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.


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
Jul 17 '05 #6

P: n/a
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_CONNECTED"
sockWrite(index,"Connected, RBS v1.0 \r\n"
.state = "STATE_SENT_HEAD"

case "STATE_CONNECTING"
if (socketConnected(index)) {
.state = "STATE_CONNECTED"
}

case "STATE_READY"
.state = "STATE_CONNECTING"
socketConnect(index)
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" <da******@extending-php.net> wrote in message
news:xF*******************@news4.tin.it...
CountScubula ha scritto:
> 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.


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

Jul 17 '05 #7

P: n/a
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
Jul 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.