Random1340
08-02-2004, 12:47 AM
Hey everyone. I am trying to connect to a socket server that I have written in PHP (Basically taken straight from online tutorials). If I create a simple PHP script to connect to this server everything works fine as expected (except I am unable to successfully loop socket_read() to handle multiple responces from the server - but thats another story). When I try to connect to the server using flash as the front end - I can never connect. Both my php server and flash file are on the same box. Any help or guidance would be greatly appreciated - thanks.
var socket:XMLSocket = new XMLSocket();
socket.connect("localhost", 9000);
socket.onConnect = function(success) {
success ? output.text += "Connected." : output.text += "Connection failed.";
};
#!/usr/local/bin/php -q
<?php
// Set time limit to indefinite execution
set_time_limit (0);
// Set the ip and port we will listen on
//$address = '192.168.0.4';
$address = 'localhost';
$port = 9000;
$input = "";
// Create a TCP Stream socket
$sock = socket_create(AF_INET, SOCK_STREAM, 0) or die ('Could not create socket\n');
// Bind the socket to an address/port
socket_bind($sock, $address, $port) or die('Could not bind to address\n');
// Start listening for connections
socket_listen($sock) or die ('Could not set up listener\n');
print ("Listening for clients...\n\n");
/* Accept incoming requests and handle them as child processes */
while ($input!="exit" || $input !="quit") {
$client = socket_accept($sock) or die ('Could not accept incoming connection\n');
print ("Client connected\n");
// Read the input from the client – 1024 bytes
$input = socket_read($client, 1024) or die ('Could not read input\n');
print ("Recieved message: ".$input."\n");
// Strip all white spaces from input
$output = ereg_replace("[ \t\n\r]","",$input).chr(0);
// Display output back to client
socket_write($client, $output, strlen($output)) or die ('Could not write output\n');
print ("Sent message back to client: $output\n\n");
}
// Close the client (child) socket
socket_close($client);
// Close the master sockets
socket_close($sock);
?>
var socket:XMLSocket = new XMLSocket();
socket.connect("localhost", 9000);
socket.onConnect = function(success) {
success ? output.text += "Connected." : output.text += "Connection failed.";
};
#!/usr/local/bin/php -q
<?php
// Set time limit to indefinite execution
set_time_limit (0);
// Set the ip and port we will listen on
//$address = '192.168.0.4';
$address = 'localhost';
$port = 9000;
$input = "";
// Create a TCP Stream socket
$sock = socket_create(AF_INET, SOCK_STREAM, 0) or die ('Could not create socket\n');
// Bind the socket to an address/port
socket_bind($sock, $address, $port) or die('Could not bind to address\n');
// Start listening for connections
socket_listen($sock) or die ('Could not set up listener\n');
print ("Listening for clients...\n\n");
/* Accept incoming requests and handle them as child processes */
while ($input!="exit" || $input !="quit") {
$client = socket_accept($sock) or die ('Could not accept incoming connection\n');
print ("Client connected\n");
// Read the input from the client – 1024 bytes
$input = socket_read($client, 1024) or die ('Could not read input\n');
print ("Recieved message: ".$input."\n");
// Strip all white spaces from input
$output = ereg_replace("[ \t\n\r]","",$input).chr(0);
// Display output back to client
socket_write($client, $output, strlen($output)) or die ('Could not write output\n');
print ("Sent message back to client: $output\n\n");
}
// Close the client (child) socket
socket_close($client);
// Close the master sockets
socket_close($sock);
?>