473,791 Members | 3,059 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4830
"Gabriele Farina" <da******@exten ding-php.net> wrote in message
news:Rj******** ***********@new s4.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******@exten ding-php.net> wrote in message
news:Rj******** ***********@new s4.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******@exten ding-php.net> wrote in message
news:Rj******** ***********@new s4.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******@exten ding-php.net> wrote in message
news:6E******** ***********@new s3.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 $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
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_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" <da******@exten ding-php.net> wrote in message
news:xF******** ***********@new s4.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
2499
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 System.Net.Sockets about multiple sockets on the same port. This happens even with a single listener in multiple Win32 processes. Will I get better results by using a Socket instance with BeginAccept? Should I run multiple threads, or do the asynchronous...
4
18137
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 as I find appropriate. I am using the socket asynchronously by calling the BeingSend and BeginReceive calls. I would like to be able to call shutdown and close asynchronously if possible.
6
4448
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 handles multiple connections. Client programs will make a connection and pass text strings to the service, which will then return a value for each of the strings. It's unlikely the service will ever have more than 100 simultaneous connections and...
5
15104
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 one management console/monitor that receives messages and keeps track of keep-alive polling. The monitor created a new WinSock connection for each client that logged on. These were put into an array of controls. I'm sure this can also be done with...
7
7578
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, uses the same server port. I assumed that a new connection will receive a unique port. If this is the way its suppose to work, is it a performance issue? Is it possible that connections from the same IP will connect on the same server port? I...
2
7127
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 short while now using a server program a former coworker started. The program listens on a port for incomming connections. When a valid connection is made (we send this init string into the socket from the clients) the server closes the socket so...
4
10165
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. Also, I need the service to accept multiple incoming requests. Here is the code: public void Listen() { #region Commented Code
11
8619
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 mechanisms. I use a non-blocking approach for this, using the call to 'poll' to support the async mechanism - rather than the 'begin' and 'end' functions. I already found that connecting doesn't set the "isconnected" variable correctly...
35
9366
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 500000 to 3200000 of a file whose size is say 20MB... how do i request a download which starts directly at 500000th byte... thank u cheers
0
9666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10419
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10201
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9987
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7531
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6770
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4100
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3709
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2910
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.