Re: Re: [PHP] Full-Duplex communication
| From: | Vinod Panicker | Date: | Fri, 24 May 2002 05:30:24 +0000 |
| Subject: | Re: Re: [PHP] Full-Duplex communication | ||
| Groups: | php.dev php.general | ||
| Request: | Send a blank email to php-dev+get-84385@lists.php.net to get a copy of this message | ||
LOL.. I like the color red BTW.
Let me explain my project more in detail.
This system is an Instant messaging system, with the backed running on apache / php / mysql. The front end client is a COM component that communicates with the backend, and interfaces with an MFC app.
Now since Instant messaging requires that the server should be able to send asynchronous messages to the client (Presence, IM's, notifications, etc), and HTTP does not allow asynchronous communication, the only solution is to have the client poll the server for messages.
Now i know that apache / php is not a good solution for an instant messaging server, but we needed it to be ready in a months time, and php is so amazing that we could get it up and running so fast. But it meant that we have to live with latency between messages because of the polling.
So i was thinking if there was some way in which i could avoid the polling and send data asynchronously to the client (Pls refer to my previous posts). Thats why i was looking at a way in which i could get hold of the socket to the client, so i could use it to send data directly.
Tx,
Vinod.
On Fri, 24 May 2002 Miguel Cruz wrote :
Not to be argumentative, but what you're trying to do is just so amazingly much more complicated than any of my suggestions. Maybe I haven't communicated it well, it maybe there's some requirement to your project that I don't understand. Why not just write a listener in PHP and redirect to it? It's basically no work. Handcrafting packets for this purpose is like building a car out of paper clips and cat hair because the car that's freely available to you is red - and you don't like the color red because your old girlfriend used to wear it a lot. miguel On 23 May 2002, Vinod Panicker wrote: What you said did make sense, and complements my knowledge of sockets. But what i'm not sure of is this - if i construct my own packet and send it across, presuming that i do have the ip address and port number of the client on which it is reading, will the client accept it as a legitimate packet? I suspect that since the packets would be having some kind of session identifier / sequence number. Getting the ip address and port number is no problem. I already have that getting stored on the server. But from what i know of sockets, i'm sceptical if the client will accept the packet. Guess the only way to go ahead is to try this out. Would someone pls pls pls write a PHP interface to libnet?? Tx, Vinod. On Thu, 23 May 2002 Evan Nemerson wrote :Click below to visit monsterindia.com and review jobs in India or Abroad http://monsterindia.rediff.com/jobsYou're right- this is getting interesting ;) http://www.packetfactory.net/libnet/manual/4.html#s4.1.5 Unless I'm mistaken, you don't need to actually hijack the socket- you merely need to write to the network. Check out section 3.1 of RFC793.There is source and destination port- that is how they are routed.Okayanyone PLEASE correct me if i'm wrong... My understanding is a socket is an interface to the kernel.Sobasically, you talk to a socket, which the kernel associates with sourceanddestination ports, and destination IP address. Thats why you can justwriteto a socket instead of explicitly stating all the information. Thekernelthen sends out then creates the packet and send it to the destination IP. libnet would allow you to bypass the socket phase, andmanuallycreate a socket. Think of a socket as a GUI for the network, andlibnet islike a console ;) Hope that helps, and once again anyone PLEASE correct any inaccuracies, since I want to know. On Tuesday 21 May 2002 23:53 pm, Vinod Panicker wrote:i'mThanks for the reply Miguel, but here i'm not trying toimplementmy own multi-threaded server - exactly the reason whyusingApacheApache / PHP. I could have made a listening server which is based on a multi-threaded or multi-forked model, but the time and complexities involved would be huge. Thats why I chose/toPHP. Now if what i'm asking for can be done, developers caneasilyleverage existing efficient server technologies (Apache)buildwouldtheir own App servers. I know that there is no existing function in PHP thatallowforit to retrieve the socket from Apache ;), all i'm askingis alisthack that would allow me to do it. I thought that i'd just as well post it on the mailingbeforebutdiving into the source code and trying to figure out formyself.No point trying to re-invent the wheel, right? Evan, that lib will allow me to create my own packets,whichalong.socket do i send it to? Thats been the question alltheI think this is getting really interesting :) Tx, Vinod. On Wed, 22 May 2002 Miguel Cruz wrote :I don't think you're going to get Apache to hand you(CGI)socket. However, you can write a program using the standalonePHPyourinterpreter that will act like a server - check out http://php.net/socket_create_listen for more info. You could redirect from your standard web server toatlistening PHP app running on another port. You'll then have to implementleast atosubset of the HTTP protocol in order to get browsers to talkyou.knowledgeUnfortunately, since you can't - to the best of my-functionsthreadingfork a PHP program, you're going to have to do your own homebrewenough.which will make life slightly complicated. miguel On 22 May 2002, Vinod Panicker wrote:It still seems like I havent made the problem clearI am aware of the print(), echo() and flush()andandexplainwhatthey do. It does not fit in as a solution. Let meApachemyproblem more elaborately - The client calls a PHP script, script_a.php on thereturnswebserver, using a Keep-Alive connection. The scriptsomeresponse to the client which it uses. Now since theconnection isa Keep-alive, apache still has it open for readingthatsendswriting.When the client wants to call other scripts, it justtherequest over the same connection. Now the thing isifscriptclient,theserver needs to send some ASYNCHRONOUS data to thewithoutthe client requesting for anything, a normal PHPwontbybeable to do it, since the script would get executedtheHTTPwebserver ONLY on a client request (coz thats the waytheworks).Now what i was thinking was - if i could get hold ofclient,socketthat is being used by apache to send data to theIprint(),scriptcouldeffectively write() to it, from a C++ app or a PHP(whichgets invoked from lets say another server).echo()isetcare functions that write to the output stream, whichopeneditcreatedasa result of the clients request, by the web server. I want the ability to write to a socket thats beenearlier- i want to steal it from Apache, so that i can usewhenworkandwhere i like. Functions like echo() and print() are not going tohere,socketiwill have to use write() so that i can specify thetojustwhichthe data has to be written! Hope the problem is understood now. Now for your question - When the client wants to send data to the server, ithasissue atoopen a socket connection with the web server, andGETaconnection,or aPOST request! if the connection is a keep-aliveandit has already been created, the client just has to doGETconnects()or aPOST without the need to connect(). This mechanism, where the client frequentlytoOnetheserver and checks for messages is called polling.waydoingconnectionofreducing the high overhead of this is to reuse theimprovementbyusing a keep-alive connection. A still betterwouldbe to remove the need for a poll altogether, bysosomething(thats what my question is all about) on the serverthat itprovidedcansend data asynchronously to the server. Tx, Vinod. On Wed, 22 May 2002 Bogdan Stancescu wrote :For your specific problem, I think Mr. Lemos hasaflush()viable solution (using print() or echo() andwhenevertoyouneed to, instead of grabbing the socket and write()it).datacommunicationMyproblem however is how you envision solving thetheother way around (i.e. when the CLIENT wants to sendtodataspecifiesthesomeplace.server). Bogdan Vinod Panicker wrote:Hi, Tx for your very prompt reply. Yeah, I'll post the solution as soon as I find itLet me outline the problem in more detail - Client (VC++) calls a PHP script on the server,somehowtheconnection type as Keep-Alive. The PHP script,server(still abig question) gets the socket on which the apachehasreceived the client request (so that it can sendtofromtheclient later) and stores it in a database. Now whenever another PHP script wants to send data asynchronously to the client, it gets the socketthethedatabase, and just calls a write() on it. Sincetheconnectionis still open (Keep-Alive), the client receivessuremessaginginformation,and doesnt have to poll the server periodically. The application of this is indeed destined for awell.product, and could benefit a lot of other areas asapache.The only thing that is needed is the socket fromSomeone somewhere knows how to get this done, i'm:)done,Possibly a hack into the PHP module can get thisi'mwell, soopento suggestions. Tx, Vinod. On Tue, 21 May 2002 Bogdan Stancescu wrote :Hi! I'm looking for an answer to your questions asifpleaseyoudo find a solution on other lists, could youpostforithere as well? Regarding the issue, your proposal wouldn't makeseefull-duplex as far as I understand since I don'thow(ontheconnectionclient would be able to send any data on the same_after_ getting connected. What are you using on the other end of the pipethewhereclient)? Plain HTML? Flash? Java? Something else? Bogdan Vinod Panicker wrote:Hi, We have developed a client-server applicationtheNowserverneeds to send asynchronous data to the client.sincepollweare using Apache/PHP/MySQL, the client needs tothearoundserver periodically for information. I was thinking if there was some way to getthisarebasicproblem. I understand that this is how thingsdatasupposed towork, but it would be just great if i could PUSHfromSoApachetheserver to the client, using HTTP. Since HTTP is a request/response based protocol,wouldnot send any data to the client asynchronously.what imaxKeep-Alivewasthinking was - If i tell the server to allowconnections, and increase the timeout value andconnection.requests,I would effectively have a constant TCPNowdatatheonly problem would be of sending asynchronoustowaytheclient. Solution? Here goes - If there was someinthatwhichi could get hold of the file descriptor(socket)isibeingused by apache to write data to the client, thencould,usingfroma PHP script also send any data to the clientthepollwithsocketfunctions of PHP since i already have the socketme.This would mean that the client doesnt have tothegetserverfor data any more... and if the connection doesforclosed,the client could reconnect to the server askingtoanotherkeep-alive connection. Now I know that this is probably the wrong placeputitbetter.such aquery - maybe the apache list would have beenButsince I'm using PHP out here, i thought i'd giveatheretry.Does the solution sound very outlandish? Areany_________________________________________________________socket?pitfalls? And finally, how do i get hold of the