Jump to content

joel2k8

Members
  • Posts

    22
  • Joined

  • Last visited

    Never

Everything posted by joel2k8

  1. Hi I recently created a socket server in PHP and a client in actionscript, what the client does is send the X and Y and there ID to the server and then the server sends it back to the clients connected, my problem is it works but the X and Y start messing about and it duplicates sometimes, i'm pretty sure it is to do with the PHP server, this is my code: Server: <?php /* Raymond Fain Used for PHP5 Sockets with Flash 8 Tutorial for Kirupa.com For any questions or concerns, email me at [email protected] or simply visit the site, www.php.net, to see if you can find an answer. */ // E_ALL is a named constant (bit of 2047) // error_reporting(E_ALL) ensures any errors will be displayed error_reporting(E_ALL); // set_time_limit() is set to 0 to impose no time limit set_time_limit(0); // ob_implicit_flush() will push the whole message to flash (including any extra markups like terminating zeros) ob_implicit_flush(); // address set to internal port for the socket to bind to and port set to something above 1024 so flash will connect $address = '192.168.1.71'; $port = 9999; //---- Function to Send out Messages to Everyone Connected ---------------------------------------- // $allclient is an array of resource id's that php will refer to for the sockets // $socket contains the resource id for the flash client who sent the message // $buf is just the message they sent, we are sending to everyone using a foreach loop function send_Message($allclient, $socket, $buf) { foreach($allclient as $client) { $phoneChunks = explode("=", $buf); $pid = $phoneChunks[0]; $x = $phoneChunks[1]; $y = $phoneChunks[2]; // socket_write uses the $client resource id (which calls on the socket) and sends it our message (which is the second flag) socket_write($client, "$pid=$x=$y"); echo "$pid=$x=$y \n"; } } //---- Start Socket creation for PHP 5 Socket Server ------------------------------------- // socket_create function is pretty simple, depending on what you want it to do, you can just rig it for that, // AF_INET is a "kind of" default address/protocol family that I have used, // then socket_create takes and uses SOCK_STREAM, another default available socket type, // and uses a SOL_TCP protocol to have a guaranteed data sending. if (($master = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) < 0) { echo "socket_create() failed, reason: " . socket_strerror($master) . "\n"; } // this little beauty is hard to understand, but very useful to our kind of chat // we want to be able to reuse our sockets, so we set our $master socket with a level at SOL_SOCKET, // with it reusable (SO_REUSEADDR), and a "1" in the mixed optval spot to make it true. socket_set_option($master, SOL_SOCKET,SO_REUSEADDR, 1); // now we need to bind our socket to the address and port specified. if (($ret = socket_bind($master, $address, $port)) < 0) { echo "socket_bind() failed, reason: " . socket_strerror($ret) . "\n"; } // to allow our socket server to listen for incoming calls made by our flash client, we need // to use socket_listen on our $master socket. The next flag indicates that only 5 calls at a time. // any others will be refused until there is sufficient room to process a new call. if (($ret = socket_listen($master, 5)) < 0) { echo "socket_listen() failed, reason: " . socket_strerror($ret) . "\n"; } // this will allow an easy way to keep track of sockets connected to our server $read_sockets = array($master); //---- Create Persistent Loop to continuously handle incoming socket messages --------------------- while (true) { $changed_sockets = $read_sockets; // socket_select is used here to see if there were any changes with the sockets that were connected before // $num_changed_sockets is here so you can check to see if the change is true or not with a subsequent function $num_changed_sockets = socket_select($changed_sockets, $write = NULL, $except = NULL, NULL); // we run a foreach function on $changed_sockets to see whether it needs to be added (if new), determine the message it sent // or determine if there was a socket disconnect foreach($changed_sockets as $socket) { // now if the $socket currently being checked is the $master socket, we need to run some checks // if not then we will skip down to else and check the messages that were sent if ($socket == $master) { // socket_accept will accept any incoming connections on $master, and if true, it will return a resource id // which we have set to $client. If this did not work then return an error, but if it worked, then add in // $client to our $read_sockets array at the end. if (($client = socket_accept($master)) < 0) { echo "socket_accept() failed: reason: " . socket_strerror($msgsock) . "\n"; continue; } else { array_push($read_sockets, $client); } } else { // socket_recv just receives data from $socket as $buffer, with an integer length of 2048, and a mystery flag set to 0 // that mystery flag has no real documentation so setting it to 0 will solve it as others have done. // we've also set it as a var $bytes in case we need to ensure data sending with socket_write, which is optional $bytes = socket_recv($socket, $buffer, 2048, 0); // if the $bytes we have is 0, then it is a disconnect message from the socket // we will just search for it as an index and unset that socket from our $read_sockets and // finish up with using socket_close to ensure it is closed off if ($bytes == 0) { $index = array_search($socket, $read_sockets); unset($read_sockets[$index]); socket_close($socket); }else{ // we need to make sure $read_sockets isn't messed with, so setting up a new variable called $allclients // will ensure this. We then shift the array so that our $master socket is not included to the count when // we send our data to all the other sockets in $allclients. $allclients = $read_sockets; array_shift($allclients); // just a simple call on our premade function 'send_Message' with $allclients, $socket (current one), and $buffer (the message) send_Message($allclients, $socket, $buffer); } } } } ?> This is my ActionScript 2, but I'm pretty sure this is not the problem: _global.server_connected = false; _global.mySocket = new XMLSocket(); mySocket.onConnect = function(success) { if (success) { msgArea.htmlText += "<b>Server connection established!</b>"; server_connected = true; } else { msgArea.htmlText += "<b>Server connection failed!</b>"; } }; mySocket.onClose = function() { msgArea.htmlText += "<b>Server connection lost</b>"; }; //incoming data from server. var depthb = 6000; var myArray = new Array(); mySocket.onData = function(msg) { //msgArea.htmlText += msg; myArray = msg.split('='); if(_global.pid != myArray[0]) { if(_root["players_" + myArray[0]]) { //if players exist if(_root["players_" + myArray[0]]._x != myArray[1] || _root["players_" + myArray[0]]._y != myArray[2]) { _root["players_" + myArray[0]]._x = myArray[1]; _root["players_" + myArray[0]]._y = myArray[2]; } } else { _root.players.duplicateMovieClip("players_" + myArray[0], 6000); //_root["players_" + myArray[0]]._x = myArray[1]; //_root["players_" + myArray[0]]._y = myArray[2]; } } }; //connect to server. _global.mySocket.connect("192.168.1.71", 9999); If you are good with sockets and such then you should easily be able to spot the problem on why it's buggy, I cannot see what is wrong and really need the client and server to communicate properly. It's OK when you connect alone, but when someone else joins it will start being glitchy and not going to the correct X and Y and then there will be duplicates and such.. If you do not understand I will try and get a demo up and show you what I mean. Thanks.
  2. Yeah.. the problem is solved, as i said in my last reply, i accidently put my text in the quote. thanks again for the help.
  3. I got it to work, before the code was: strpos($filecont, $name) I changed to: strpos($filecont, "$name") Now it works how it should, but for some reason i would of thought it would work with out the " ". Nevermind, thanks for the help, problem is now soved! If you still can't get it to work, posting example data would help.
  4. Hmm.. nevermind it was me using getting text from an XML file and using it as the string to search.. still don't understand why that would stop it from working.. thanks for helping anyway!
  5. Yes, i just want to see the string exist and if does do something.. though the code you gave me doesnt seem to work, but it is a php file that i am getting the information from: test.php <a href="http://google.com/">Google</a> bhjb njkm, knklmnnkl hello <a href="#">test</a> <a href="http://google.com/">Google</a> <a href="http://google.com/">Google</a> <a href="http://google.com/">Google</a> then i try and check to if it exists by using: $old = 'hello'; $filename = 'test.php'; $file = file_get_contents($filename); if(strpos($file, $old)) { //string exists } it doesn't seem to work, does it have to be a txt file? i can write to the php file but can't seem to find the string.. thanks
  6. Hello, Okay for some reason i cannot do this, when i open a file in php and search for string, it doesn't seem to work, some codes i tried: $filename = 'example.txt'; $searchfor = 'hello'; $fh = fopen($filename, 'r'); $olddata = fread($fh, filesize($filename)); if(strpos($olddata, $searchfor)) { //fount it } else { //can't find it } fclose($fh); (above it just sample text, i was hoping you could give me sample code that will work) I tried many others like strstr, stristr, preg_replace etc etc, doesn't seem to find it.. this is my text file: sdfsdfsdfsdfsdf[b]hello[/b]sdvsdf dfadfsdfsdfsddfsdf sdfsdfsdfs sdffffffffffffffffffffdv vsvdsdsf I don't want to find it by line, i want to simply open the text file and find the string.. thanks..
  7. Alright well Hello, I currently have a table that shows a background image and I have 1 TD with 100% width and height and in that TD I have a DIV that moves by margin-left and margin-top via JavaScript and the height is approx 20px by 20px, so basically I have a map with a character that moves when keys are pressed. My problem is I'm having trouble setting limits (where the character cannot go inside the TD). etc if it reachs a X and Y then it would stop, but it's not that easy, I currently have this in a IF function when the keys are pressed: var x; var y; var exclude = { x: [50, 50, 50, 50, 50], y: [50, 60, 70, 80, 90] }; if (exclude.x.indexOf(x) !== -1 && exclude.y.indexOf(y) !== -1){ // dismiss } else { //run main code.. } But that code doesn' work correctly because I want the numbers in exclude.x to match the oppisite of exclude.y so 50 and 50, 50 and 60 but instead its like this 50 and 50 or 60 or 70 or 80 or 90. I just want it to be set so it matches the number... x: [1, 2, 3, 4, 5], y: [1, 2, 3, 4, 5] Hope you can help I've been having trouble for awhile now, thanks.
  8. Well hello everyone, What I am trying to do is update a query using time or date instead off using Cronjobs. Like if I want to update a query every 5mins what would be the best thing I could do? I have tried things such as: if ((date('s')) == 300) { mysql_query("UPDATE users SET money = money + 1000"); } Yes it does work accept you have to refresh on the exact time to get the money, but I want to update the users without everybody refreshing because if they don't refresh it wont update :-\ I have tried different things but I'm not good when it comes to time and date. Any suggestions?... thanks if you help me out
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.