473,326 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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
Jul 17 '05 #1
7 4811
"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
"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
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
"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Paul A. Steckler | last post by:
I need to write a TCP/IP server in C# that can handle multiple connections. My first try was to use TCPListener instances in multiple .NET threads. Of course, I got an exception from...
4
by: Chris Tanger | last post by:
Context: C# System.Net.Sockets Socket created with constructor prarmeters Internetwork, Stream and TCP everything else is left at the default parameters and options except linger may be changed...
6
by: Quiet Man | last post by:
Hi all, I'm designing a fairly simple service that will run on W2K/SP4 and W2K3 servers. It's job is to be a very specialized database server that listens on a given IP address / TCP port and...
5
by: Rotzooi | last post by:
Hi, In the past I created a VB6 application that was capable of accepting multiple client connections for status logging over the internet using simple non-Windows clients (GSM/GPRS). There is...
7
by: Sharon | last post by:
Hi all, I've implemented a TCP server using the Socket async methods. When connecting to the server from 3 instances of hyper terminal, i've noticed that each of the newly created server sockets,...
2
by: jasonsgeiger | last post by:
From: "Factor" <jasonsgeiger@gmail.com> Newsgroups: microsoft.public.in.csharp Subject: Multiple Clients, One port Date: Wed, 19 Apr 2006 09:36:02 -0700 I'm been working with sockets for a...
4
by: sracherla | last post by:
I am trying to write a simple windows service that accepts an incoming request; receives a string input and sends a string output. I need this connection to stay alive until the client closes it....
11
by: atlaste | last post by:
Hi, In an attempt to create a full-blown webcrawler I've found myself writing a wrapper around the Socket class in an attempt to make it completely async, supporting timeouts and some scheduling...
35
by: keerthyragavendran | last post by:
hi i'm downloading a single file using multiple threads... how can i specify a particular range of bytes alone from a single large file... for example say if i need only bytes ranging from...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.