PDA

View Full Version : Cannot connect using Flash xmlsocket


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);
?>

Random1340
08-03-2004, 07:44 PM
Anyone? Anything at all? I am using php 5.0, apache2.0 running on linux platform and macromedia flash 2004 prof. I cannot get flash to connect to my php server.

freddycodes
08-03-2004, 09:01 PM
Might I suggest starting with a socket server that you know will work, and then you can write your own, using snippets from it.

Look at patServer http://www.php-tools.de

Random1340
08-03-2004, 09:34 PM
I have downloaded and successfully set up electroserver demo - I tried using patServer - they all work great, when I connect using any other app. My question is why can't I get flash to connect to the server? There has to be something small that I'm missing - this seems like much too trivial a task that I can't get it to connect.

Random1340
08-04-2004, 06:16 PM
After many hours I have figured out that I need to change my socket_bind() line of code in the php script to this:

socket_bind($sock, "0.0.0.0", $port) or die('Could not bind to address\n');

That seemed to have solved my problem.