Jump to content

Multiple persistent sockets per hostname:port


boen_robot

Recommended Posts

BACKGROUND:I've recently been working on a project that's now almost finished, except I have this one known issue I hope to tackle...The project is a client for a certain kind of server. Naturally, there is authentication involved that I deal with at connection time, like:

$client1 = new Client(HOSTNAME, USERNAME1, PASSWORD, PORT);//$client1 is now connected to HOSTNAME:PORT and logged in as USERNAME1

However, I wanted to also have the ability to have persistent connections (via another argument), and this is where things get interesting.I can have a persistent connection to a hostname:port combo without problems, but I can't have multiple hostname:port connections depending on the username. That is,

$client1 = new Client(HOSTNAME, USERNAME1, PASSWORD, PORT, true);//$client1 is now connected to HOSTNAME:PORT and logged in as USERNAME1

works, but it only works for USERNAME1. Trying to pass another user for a second connection will silently be ignored in favor of the previous user, i.e.

$client1 = new Client(HOSTNAME, USERNAME1, PASSWORD, PORT, true);//$client1 is now connected to HOSTNAME:PORT and logged in as USERNAME1$client2 = new Client(HOSTNAME, USERNAME2, PASSWORD, PORT, true);//$client2 is now connected to HOSTNAME:PORT... but is logged in as USERNAME1

This behavior is expected, since I use pfsockopen() to make the connection, and then I use ftell() to check if login is necessary. I thought about saving the username in some fashion (e.g. file), and then switching logins back and forth as each Client instance made requests. However, the problem is the protocol I'm implementing doesn't allow login switches - once you're connected, your first action is to login, and you can't login again on the same connection.QUESTION:Is there any way I can make multiple persistent socket connections for the same hostname:port combo? Some search led me to this thingy, but I was hoping for something that could be achieved with bundled PHP extensions (the socket extension that is...), even if it would require a lot more effort on my part than just that simple constructor.

Link to comment
Share on other sites

Found it!For anyone interested... there seems to be an undocumented feature in stream_socket_client() where you can append something (URI style) after specifying the hostname and port, in the form "tcp://{$host}:{$port}/{$key}". PHP creates a new connection for each distinct $key.Note that this feature isn't available for pfsockopen() for some reason.(talk about trial&error...)

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...