I had a quick go at this last night but failed. I'm trying to make a quiz where multiple people can open a telnet session and a question is asked and the quickest correct answer wins the point and then it moves onto another question.
I'm able to do everything besides handling the network connections. Is this going to be a mammoth task to get working or just not possible?
Thanks, Ste
How possible is it to have multiple connections over sockets
Moderator: General Moderators
Re: How possible is it to have multiple connections over sockets
Why is the data passed via a telnet session? Are these all trusted users?
You can do multiple sessions with sockets. You might find cURL a little easier to work with. Debugging it might boarder on mammoth, but it can be done.
You can do multiple sessions with sockets. You might find cURL a little easier to work with. Debugging it might boarder on mammoth, but it can be done.
-
impulse()
- Forum Regular
- Posts: 748
- Joined: Wed Aug 09, 2006 8:36 am
- Location: Staffordshire, UK
- Contact:
Re: How possible is it to have multiple connections over sockets
Yes they're all trusted users. It's an internal application for the boring afternoons 
CuRL wouldn't keep a persistent connection would it? I'm using a telnet connection so it's live interactive data. I don't want any disconnections and then the user to have to reconnect for any reason.
CuRL wouldn't keep a persistent connection would it? I'm using a telnet connection so it's live interactive data. I don't want any disconnections and then the user to have to reconnect for any reason.
-
impulse()
- Forum Regular
- Posts: 748
- Joined: Wed Aug 09, 2006 8:36 am
- Location: Staffordshire, UK
- Contact:
Re: How possible is it to have multiple connections over sockets
I've just found this piece of code on php.net
It's a live chat script which is all I should need to continue from here.
Thanks,
Code: Select all
#!/usr/bin/php -q
<?php
error_reporting(E_ALL);
set_time_limit(0);
ob_implicit_flush();
$address = '127.0.0.1';
$port = 8888;
function handle_client($allclient, $socket, $buf, $bytes) {
foreach($allclient as $client) {
socket_write($client, "$socket wrote: $buf");
}
}
if (($master = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) < 0) {
echo "socket_create() failed: reason: " . socket_strerror($master) . "\n";
}
socket_set_option($master, SOL_SOCKET,SO_REUSEADDR, 1);
if (($ret = socket_bind($master, $address, $port)) < 0) {
echo "socket_bind() failed: reason: " . socket_strerror($ret) . "\n";
}
if (($ret = socket_listen($master, 5)) < 0) {
echo "socket_listen() failed: reason: " . socket_strerror($ret) . "\n";
}
$read_sockets = array($master);
while (true) {
$changed_sockets = $read_sockets;
$num_changed_sockets = socket_select($changed_sockets, $write = NULL, $except = NULL, NULL);
foreach($changed_sockets as $socket) {
if ($socket == $master) {
if (($client = socket_accept($master)) < 0) {
echo "socket_accept() failed: reason: " . socket_strerror($msgsock) . "\n";
continue;
} else {
array_push($read_sockets, $client);
}
} else {
$bytes = socket_recv($socket, $buffer, 2048, 0);
if ($bytes == 0) {
$index = array_search($socket, $read_sockets);
unset($read_sockets[$index]);
socket_close($socket);
} else {
$allclients = $read_sockets;
array_shift($allclients); // remove master
handle_client($allclients, $socket, $buffer, $bytes);
}
}
}
}Thanks,
Re: How possible is it to have multiple connections over sockets
CURLOPT_MAXCONNECTS is the number of persistant connections allowed.impulse() wrote:CuRL wouldn't keep a persistent connection would it?
CURLOPT_PROTOCOLS is where you can select telnet for your protocol.
The function you posted should work fine for telnet too with TCP sockets.
-
impulse()
- Forum Regular
- Posts: 748
- Joined: Wed Aug 09, 2006 8:36 am
- Location: Staffordshire, UK
- Contact:
Re: How possible is it to have multiple connections over sockets
I'm going to continue using the code snippet from php.net as the basics are already written for me.
I am struggling getting the the it to loop continuously based upon time rather than a socket making a change. For this to work the server would need to continually loop as quickly as possible (answering quiz question quicker gets extra points) but at the moment a loop is only performed once a client sends some data. Is this a limitation of PHP sockets?
Thanks,
I am struggling getting the the it to loop continuously based upon time rather than a socket making a change. For this to work the server would need to continually loop as quickly as possible (answering quiz question quicker gets extra points) but at the moment a loop is only performed once a client sends some data. Is this a limitation of PHP sockets?
Thanks,
Re: How possible is it to have multiple connections over sockets
Yeah the server has to poll the sockets for data. There might be another way but I'm pretty sure you would have to do something on the client side. You could timestamp the session so you know what time it is on each machine, when the question was asked, then when they reply, add a timestamp again and compare the elapsed time with their answer on for each machine.
This was the main reason I asked why you were using telnet because it might be better to manage the timed response on the client side, rather than the server, but the timestamps might work for you.
This was the main reason I asked why you were using telnet because it might be better to manage the timed response on the client side, rather than the server, but the timestamps might work for you.