php networking

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
saryon
Forum Newbie
Posts: 8
Joined: Fri Jul 05, 2002 2:59 am
Location: Leiden, Netherlands
Contact:

php networking

Post by saryon »

I created a little server toy in PHP, using the few examples i could
find on the net. But i seem to have run into a little problem.
Every time i connect, everything goes fine.
When i connect a second time (without disconnecting the first), i have to
first close the first connection before the second connection does anything
at all.
Does anyone know how i can get PHP to create a multi-connection server,
instead of a single-connection one?

Sar
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post by llimllib »

hmmm...that's an interesting problem...since you can't create threads (to my knowledge) in PHP manually, and you wouldn't want to fork the process even if you could, I can't see how you would solve this except by writing a C extension to PHP. While doable, it certainly would not be easy - although, hopefully, there's a workaround I haven't considered.
saryon
Forum Newbie
Posts: 8
Joined: Fri Jul 05, 2002 2:59 am
Location: Leiden, Netherlands
Contact:

Post by saryon »

I'm not sure that threading is necessary, because there are various c-programs which aren't
threaded, yet can still handle multiple connections by going through the list of sockets in a
round-robin style (like muds do).
But this would require the ability to have multiple connections to the same socket, and that
seems to be something that is hard to do in PHP.
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post by llimllib »

do you have any code for that behavior? I'd love to see it; what socket library does it use?
ame12
Forum Newbie
Posts: 5
Joined: Thu Jul 04, 2002 8:22 am

File sharing?

Post by ame12 »

Are you trying to do something like P2P file sharing? The free BadBlue web server does all of this for you - plus run PHP scripts. It supports the Gnutella protocol in either private or public networks.

So you could, in theory, run a PHP script that searches an entire Gnutella network for a specific file and then download it. The PHP ShareOffice library ( http://badblue.com/helpphpo.htm ) is probably a good place to start, since it calls into the server for purposes of returning Excel or Access data. You could modify some of the PHP scripts to instead execute a P2P search on network.
saryon
Forum Newbie
Posts: 8
Joined: Fri Jul 05, 2002 2:59 am
Location: Leiden, Netherlands
Contact:

multi connection socket works !!!!!!!!!!!!!!!!!!!!!!!!!! :)

Post by saryon »

woohooo :D :) :D :) :D
ok, i have it so far that it actually accepts multiple connections.
I did this by using socket_select, and going through the list of sockets.
the only problem is now, that connection #2 needs to send data, so that
connection 1 can send data again.
This kinda sucks, but i'm trying to figure out how to fix that problem.....

pseudocode:

Code: Select all

<socket setting up>
for($i = 0 ; $i < max_sockets ; $i++)
     clientlist&#1111;$i]
do
&#123;
        socketlist&#1111;0] = listensocket
        set all clientsockets in socketlist
        nready = socket_select($listensocket, null, null, 0 0)
        if(inarray(lsitensocket, socketlist)
        &#123;
               loop through socketlist
               &#123;
                           if(client isn't a resource)
                                        create new socket
                           else
                                         send/receive data
               &#125;
        &#125;
&#125; while(true)
saryon
Forum Newbie
Posts: 8
Joined: Fri Jul 05, 2002 2:59 am
Location: Leiden, Netherlands
Contact:

Re: File sharing?

Post by saryon »

ame12 wrote:Are you trying to do something like P2P file sharing? The free BadBlue web server does all of this for you - plus run PHP scripts. It supports the Gnutella protocol in either private or public networks.

So you could, in theory, run a PHP script that searches an entire Gnutella network for a specific file and then download it. The PHP ShareOffice library ( http://badblue.com/helpphpo.htm ) is probably a good place to start, since it calls into the server for purposes of returning Excel or Access data. You could modify some of the PHP scripts to instead execute a P2P search on network.

what i am trying to do is set up a simple server, which allows people to connect to, chat or
something, and then disconnect.
i have no real intention to make it some kind of fileswapping thing, because there are already
about 9279471947366149400 different clients out there, which do the same thing, and
besides i'm too lazy to actually go into that code and try to figure out how the
clients talk to eachother ;)
It's mostly a toy, trying to figure out how i can get sockets to really work nicely with php,
and maybe, some day, i'll make something more interesting out of it.
a webserver/chatserver/mailserver/whatever.
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post by llimllib »

congrats sary, good job
ame12
Forum Newbie
Posts: 5
Joined: Thu Jul 04, 2002 8:22 am

Good job

Post by ame12 »

Yes, good job on the socket code - that's interesting stuff.

Regarding writing a web server, etc. - the only caution is on the security side, there's a fair amount of security stuff (URL filtering) that a server has to do to ensure that users aren't opening up "gaping security holes" on their PCs. So be careful if you do end up writing that for real usage...
saryon
Forum Newbie
Posts: 8
Joined: Fri Jul 05, 2002 2:59 am
Location: Leiden, Netherlands
Contact:

Re: Good job

Post by saryon »

ame12 wrote:Yes, good job on the socket code - that's interesting stuff.

Regarding writing a web server, etc. - the only caution is on the security side, there's a fair amount of security stuff (URL filtering) that a server has to do to ensure that users aren't opening up "gaping security holes" on their PCs. So be careful if you do end up writing that for real usage...
thanks :)


but......real usage? *rofl* right....
first i want it to actually try and do something ;)
whatever happens then....and whatever i make of it...we'll see.
Post Reply