PHP multiple sockets

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
paulfynch
Forum Newbie
Posts: 6
Joined: Thu Nov 17, 2005 1:41 pm

PHP multiple sockets

Post by paulfynch »

Hey all, first time here, good forum.

Okay...
I want to basically ask you guys if this is "doable". I'm sure it is. but I'm lost.

I have a class from flumpcakes.co.uk called MSN Messenger Class. Basically this connects to the MSN notification server, then the switchboard to handle messages.


Then I have modified a simple php socket server to handle an incoming connection from a flash client, and, after a simple authorization, i call a function from the MSN class.

So I know this is wrong, I'm just kinda hacking things together here.

What's happening is the simple socket server that handles the flash client (and returns data to the flash client) stops at the MSN class function (used to start the whole MSN connection and listen for MSGs) because the MSN class function enters it's own (while) loop to handle incoming and outgoing data from the MSN switchboard.

I can receive messages from the MSN notification server still, because I am in that loop, but I lose the ability to receive messages from the flash client, and pass them along, so to speak.

Basically I am trying to create a proxy or gateway from my flash client to the msn switchboard server.

What would be the proper way to do this?

Here are the files I am using:
My simple socket server << I added dummy login info (cuz you can't have mine ;)

MSN Messenger Class Zip

I am running the socket server from the CLI on a Unix host, if you think it matters.

Bigtime thanks in advance, I will be happy to share the flash and code when I'm done.

Cheers!
paulfynch
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

What would be the proper way to do this?
As far as I understand it the proper way would be to have two open sockets (one to the switchboard and one to the flash client) and pass them both to socket_select. It leads you to implementing some kind of socket manager. This will require you to modify msn client class to use your socket manager instead of simple fsockopen, though.
paulfynch
Forum Newbie
Posts: 6
Joined: Thu Nov 17, 2005 1:41 pm

Update

Post by paulfynch »

Hey all, I don't know what key combo I hit, but my browser took off, and I have to rewrite this post :\ oh well

Okay, update

Here is the original part of his function that maintains a connection with the MSN switchboard( function rx_data )

Code: Select all

while (! feof($this->fp))
{
	$data = $this->_get();
	if ($data)
	{//...handle the data
_get() function looks like this:

Code: Select all

function _get()
{
	if ($data = @fgets($this->fp, 4096))
	{
		if ($this->debug) echo "<div class=\"r\"><<< $data</div>\n";
		return $data;
	}
	else
	{
		return false;
	}
}
So what I tried to do is this:

Code: Select all

while (! feof($this->fp))
{
	$data = $this->_get();
	$flash_data = $this->_client();//my own function

	if ($data)
	{
and now my _client() function:

Code: Select all

function _client()
{
	if($client_data = @socket_read($this->flash_client, 1024))
	{
		if ($this->debug) echo "Client Sent: ".$client_data;
		return $client_data;
	}
	else
	{
		return false;
	}
}
I passed the resource for the flash client back with the original socket server:

Code: Select all

$msn = new msn;//constructor
if ($msn->connect($msn_un, $msn_pw)){//establish a connection and authenticate
	//echo 'Connecting to MSN.'; //CLI debug stuff
	socket_write($client[$i]['sock'],"Connecting to Network...".chr(0));  //send message back to flash client
	$msn->flash_client = $client[$i]['sock']; //pass client resource (so i can use it in the class)
	$msn->rx_data(); //start listening for messages from the switchboard
Okay, I hope you're still with me. I struggle with classes sometimes, and socket connection stuff MOST of the time ;)
So, it seems to hang when i try to create my own variable $flash_data. *sigh* I'm stumped. I'll keep trying. Thanks for having a look.


Cheers!
pf
paulfynch
Forum Newbie
Posts: 6
Joined: Thu Nov 17, 2005 1:41 pm

socket_select

Post by paulfynch »

Hey, thanks for the reply Weirdan,

I'm looking into it right now, thanks for the suggestion. Now, can socket_select manage the MSN class socket *he* creates with fsockopen and the one I create with socket_create? (Is that a dumb question?) Man, my head is swirlin with all this socket stuff...

I think I am just missing the one big *something* that ties all this together. *shrugs* Back to the manuals...

Thanks again
pf
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: socket_select

Post by Weirdan »

paulfynch wrote:Now, can socket_select manage the MSN class socket *he* creates with fsockopen and the one I create with socket_create?
I don't think so, manual says that socket_select accepts arrays of socket resources and fopen returns file resource.

But it never hurts to try ;)
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

This sounds like a blocking and non-blocking I/O issue.
What's happening is the simple socket server that handles the flash client (and returns data to the flash client) stops at the MSN class function (used to start the whole MSN connection and listen for MSGs) because the MSN class function enters it's own (while) loop to handle incoming and outgoing data from the MSN switchboard.
What's most likely happening is that the engine is stuck in that loop (that loop is blocking). Stuck isn't really the correct word, but since it's not what you want to happen, it's appropriate.

A better design would be an event loop. One of the things that can happen in this loop is that it would check for events that happen on a listenting socket. That said socket_select() sounds like it could be what you're after. Just have the event loop come back at interval and check it's status.

Now the more I think about it, an even better idea would be to hack that MSN class function and use it's while loop as an event loop then (somehow) associate your flash client functionality with it. In other words, use the MSN class function as our event loop just as I describe in the paragraph above.

Cheers,
BDKR
paulfynch
Forum Newbie
Posts: 6
Joined: Thu Nov 17, 2005 1:41 pm

Update

Post by paulfynch »

Thanks for the responses, every little bit helps...

I keep wondering if I'm trying to do something unorthodox here...

I found an interesting article by Wez Furlong, a php core programmer that kinda talks about what I want to do. I am starting to understand the blocking/non-blocking issue, but still don't know how to approach the solution. This will require some testing. His article shows a way to query a number of servers asynchronously, and handle the returned data. He appears to use socket_create to initiate the outgoing connection as opposed to fsockopen. I tried to set the stream_set_blocking to 0 on the instances where fsockopen is called in the MSN class. I guess this is part of my nature to want to hack things together first, before finally building the thing properly ;) Needless to say, it didn't work.


:?: So, now I'm wondering, can one initiate a listener socket, and an outgoing socket connection at the same time (in a similar fashion as one would initiate multiple listener instances, say, for a chat server), and handle them with socket_select or otherwise?

This will require further testing. Thoughts or suggestions are welcome. I will post any findings.

Cheers! :D
Thanks for your comments so far,
pf
paulfynch
Forum Newbie
Posts: 6
Joined: Thu Nov 17, 2005 1:41 pm

Update

Post by paulfynch »

-bump-

So, does anyone have an idea how to initate a listener socket and an outgoing connection in the same routine?

Thanks
pf
Post Reply