/    Sign up×
Community /Pin to ProfileBookmark

my socket server ignores 2nd client

ok…i’m a total noob when it comes to sockets.

that said, i have the script below working with a flash client. i can send a message every 1/13 of a second and get a response within 80 ms. HOWEVER, i want my script to allow more than one client to connect at a time and i want the script to pass information between the two (more more clients).

i use this script as follows
1) run php from command line to create socket listen
2) connect via telnet from another machine
3) run flash client which also connects to same domain/port

can someone tell me why my script seems to accept a 2nd connection, but it doesn’t ECHO anything when the second user connects? also, the 2nd client cannot send any messages through the socket….or that is to say the client doesn’t respond.

here’s the script:

[code=php]
<?

// don’t timeout
set_time_limit (0);

// set some variables
$host = “199.227.90.72”;
$port = 1234;

// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die(“Could not create
socketn” . socket_strerror(socket_last_error()));

// bind socket to port
$result = socket_bind($socket, $host, $port) or die(“Could not bind to
socketn”);

// start listening for connections
$result = socket_listen($socket, 3) or die(“Could not set up socket
listenern”);

echo “Waiting for connections…n”;

// accept incoming connections
// spawn another socket to handle communication
$spawn = socket_accept($socket) or die(“Could not accept incoming
connectionn”);

echo “Received connection requestn”;

// write a welcome message to the client
$welcome = “Roll up, roll up, to the greatest show on earth!n? ”;
socket_write($spawn, $welcome, strlen ($welcome)) or die(“Could not send
connect stringn”);

do
{
//echo “loop STARTn”;
// read client input
$input = socket_read($spawn, 1024, PHP_BINARY_READ) or die(“Could not read inputn”);
if (trim($input) != “”)
{
// echo “Received input: $inputn”;

// if client requests session end
if (trim($input) == “END”)
{
// close the child socket
// break out of loop
socket_close($spawn);
break;
}
// otherwise…
else
{
// reverse client input and send back
// $output = strrev($input) . “”;
$output = trim($input) . “”;
socket_write($spawn, $output . “? “, strlen($output)) or die(“Could
not write outputn”);
// echo “Sent output: ” . trim($output) . “n”;
}
}

} while (true);

// close primary socket
socket_close($socket);
echo “Socket terminatedn”;
?>[/code]

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@sneakyimpauthorApr 26.2004 — ok, more specifically, the script will accept the first client no problem. it responds perfectly.

when a second client tries to connect to the port, the connection attempt seems to work (telnet says i'm connected, flash gets successful socket connect) but the server script doesn't ever echo the 'connection request received' line and it doesn't respond to any messages sent by the additional connections.

any help would be much appreciated.
×

Success!

Help @sneakyimp spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...