Jump to content

samoi

Members
  • Posts

    136
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

samoi's Achievements

Regular Member

Regular Member (3/5)

0

Reputation

  1. Hello guys, I'm trying to learn and understand the process of connecting a client to TCP connection! Now I'm trying to build a mobile chat application which I want it to connect to my server (hosted in media temple) which I hate about media temple is the way they name the variable we used to know like "localhost" to some random strange string! anyway that's not the topic. But you got the idea it's a chat app as the client on a mobile device! that will try to connect to my server. Now I have tried sooo many scripts, tutorials ... etc. But I didn't get it. I need to know the process or even get it to work so I'll be able to know what's going on from the code. Here's what I have (as of now) on the mobile (client side): // create the socekt object var socket = Titanium.Network.createTCPSocket({ hostName:"mywebsite.com", port:4446, // some port I just typed because I don't know what port! mode:Titanium.Network.READ_WRITE_MODE }); // connect button connectBtn.addEventListener('click', function() { if(socket.isValid == false){ try { socket.connect(); socket.listen(); messageLabel.text = 'Opened!'; } catch (e) { messageLabel.text = 'Exception: '+e; } } }); // reader listener socket.addEventListener('read', function(e) { Ti.API.info(JSON.stringify(e)); Ti.API.info(e['from'] + ':' + e['data'].text); }); //writer button writeButton.addEventListener('click', function() { try { socket.write("writing...this"); } catch (e) { alert(e); } }); //## //## Credits goes to people in: http://www.functionblog.com/?p=67=1#viewSource //## <?php // PHP SOCKET SERVER error_reporting(E_ERROR); // Configuration variables $host = "127.0.0.1"; $port = 4041; $max = 20; $client = array(); // No timeouts, flush content immediatly set_time_limit(0); ob_implicit_flush(); // Server functions function rLog($msg){ $msg = "[".date('Y-m-d H:i:s')."] ".$msg; print($msg."\n"); } // Create socket $sock = socket_create(AF_INET,SOCK_STREAM,0) or die("[".date('Y-m-d H:i:s')."] Could not create socket\n"); // Bind to socket socket_bind($sock,$host,$port) or die("[".date('Y-m-d H:i:s')."] Could not bind to socket\n"); // Start listening socket_listen($sock) or die("[".date('Y-m-d H:i:s')."] Could not set up socket listener\n"); rLog("Server started at ".$host.":".$port); // Server loop while(true){ socket_set_block($sock); // Setup clients listen socket for reading $read[0] = $sock; for($i = 0;$i<$max;$i++){ if($client[$i]['sock'] != null) $read[$i+1] = $client[$i]['sock']; } // Set up a blocking call to socket_select() $ready = socket_select($read,$write = NULL, $except = NULL, $tv_sec = NULL); // If a new connection is being made add it to the clients array if(in_array($sock,$read)){ for($i = 0;$i<$max;$i++){ if($client[$i]['sock']==null){ if(($client[$i]['sock'] = socket_accept($sock))<0){ rLog("socket_accept() failed: ".socket_strerror($client[$i]['sock'])); }else{ rLog("Client #".$i." connected"); } break; }elseif($i == $max - 1){ rLog("Too many clients"); } } if(--$ready <= 0) continue; } for($i=0;$i<$max;$i++){ if(in_array($client[$i]['sock'],$read)){ $input = socket_read($client[$i]['sock'],1024); if($input==null){ unset($client[$i]); } $n = trim($input); $com = split(" ",$n); if($n=="EXIT"){ if($client[$i]['sock']!=null){ // Disconnect requested socket_close($client[$i]['sock']); unset($client[$i]['sock']); rLog("Disconnected(2) client #".$i); for($p=0;$p<count($client);$p++){ socket_write($client[$p]['sock'],"DISC ".$i.chr(0)); } if($i == $adm){ $adm = -1; } } }elseif($n=="TERM"){ // Server termination requested socket_close($sock); rLog("Terminated server (requested by client #".$i.")"); exit(); }elseif($input){ // Strip whitespaces and write back to user // Respond to commands /*$output = ereg_replace("[ \t\n\r]","",$input).chr(0); socket_write($client[$i]['sock'],$output);*/ if($n=="PING"){ socket_write($client[$i]['sock'],"PONG".chr(0)); } if($n=="<policy-file-request/>"){ rLog("Client #".$i." requested a policy file..."); $cdmp="<?xml version=\"1.0\" encoding=\"UTF-8\"?><cross-domain-policy xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"http://www.adobe.com/xml/schemas/PolicyFileSocket.xsd\"><allow-access-from domain=\"*\" to-ports=\"*\" secure=\"false\" /><site-control permitted-cross-domain-policies=\"master-only\" /></cross-domain-policy>"; socket_write($client[$i]['sock'],$cdmp.chr(0)); socket_close($client[$i]['sock']); unset($client[$i]); $cdmp=""; } } }else{ //if($client[$i]['sock']!=null){ // Close the socket //socket_close($client[$i]['sock']); //unset($client[$i]); //rLog("Disconnected(1) client #".$i); //} } } } // Close the master sockets socket_close($sock); ?> I just want to understand how am I gonna implement this! I need to know how does it work! You help is much appreciated!
  2. I'm not positive of what you want exactly. But I will try to help! You need to fetch the row that mysql_query returned to echo out the vars $name, $name_id, $password! something like this will be good! <?php include 'db.inc.php'; $db = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD) or die ('Unable to connect. Check your connection parameters.'); mysql_select_db(MYSQL_DB, $db) or die(mysql_error($db)); $query = 'SELECT name_id, name, password FROM user WHERE name = "' . mysql_real_escape_string($_GET['name'], $db) . '"'; $result = mysql_query($query, $db) or die(mysql_error()); // something like this will do the trick! $name_id = $result[0]; $name = $result[1]; $password = $result[2]; ?> That's it!
  3. Hello guys, I have purchased a new book for PHP called Professional PHP 6. But the bad news is that the whole book was written for PostgreSQL NOT for MYSQL which I'm familiar with! I have this code, I have tried to do so many things to get it working! but nothing seems to have it working! Long story short, I have failed to convert the code to work with MySQL! Here is my code, in case some one will offer a help, or a reference to go to if some similar case comes up on my way. <?php class Widget { private $id; private $name; private $description; private $hDB; private $needsUpdating = false; public function __construct($widgetID) { //The widgetID parameter is the primary key of a //record in the database containing the information //for this object //Create a connection handle and store it in a private member variable //This code assumes the DB is called "parts" $this->hDB = pg_connect('dbname=parts user=postgres'); if(! is_resource($this->hDB)) { throw new Exception("Unable to connect to the database."); } $sql = "SELECT name, description FROM widget WHERE widgetid = $widgetID"; $rs = pg_query($this->hDB, $sql); if(! is_resource($rs)) { throw new Exception("An error occurred selecting from the database."); } if(! pg_num_rows($rs)) { throw new Exception("The specified widget does not exist!"); } $data = pg_fetch_array($rs); $this->id = $widgetID; } public function getName() { return $this->name; } public function getDescription() { return $this->description; } public function setName($name) { $this->name = $name; $this->needsUpdating = true; } public function setDescription($description) { $this->description = $description; $this->needsUpdating = true; } public function __destruct() { if($this->needsUpdating) { $sql = "UPDATE widget SET "; $sql .= "name = " . pg_escape_string($this->name) . ", "; $sql .= "description = " . pg_escape_string($this->description) . ""; $sql .= "WHERE widgetID = " . $this->id; $rs = pg_query($this->hDB, $sql); } pg_close($this->hDB); } } ?> FYI: I have tried mysql_pconnect ! results => failure! I have tried replacing the prefix "pg" to "mysql" or "mysqli"! results => failure! I have tried switching parameters, say in pg_query($resource, $query) TO mysql_query($query, $resource) results => failure too! Thank you in advance!
  4. Yeah that worked my friend I really appreciate it man I'm getting to love this wonderful forums people! Thank you, Thank you!
  5. Thank you my friend for your help. It doesn't really work! It keep saying "Syntax error (Missing operator) in query expression "(topics.topic_creator = users.user_id) INNER JOIN reply ON (reply.reply_by = users.user_id)" This is the problem I was facing any tries ? Thank you my friend once again
  6. Hi, I have three tables users -|--------<| topics -|--------<| replies users's PK is user_id topics PK is topic_id replies PK is reply_id What i want is to select user_id # 1. And then count all topics posted by him/her. Also, count all replies post by him/her! I am sick of trying. It didn't work! this is the syntax! SELECT users.user_type, count(topics.topic_creator) as Tc, count(reply.reply_by) as Rc FROM users, topics, reply WHERE users.user_id = 1 AND topics.topic_creator = 1 AND reply.reply_by = 1 GROUP BY users.user_id The real topics posted by user_id #1 is : 3 topics # of replies by that user is : 2 But it's displaying as result . topics = 6 , replies = 6! Any help is much much appreciated!
  7. Hello my friends. I'm running into deep trouble with this structure variable! this is the structure Structure StuRec Dim strName As String Dim strMNum As String Dim ArrScores() As Decimal Dim decAvg As Decimal Dim strLetter As String End Structure dim gBook(20) as StuRec ' ' ## handling and filling gBook() with 14 records let's say! ' ' By now gBook() has 14 records! ' I need to delete one record of gBook(). Say I need to delete gBook(3) for example! ' I did this dim Temp() as StuRec For i as integer = 0 To 14 if i = 3 then continue For temp(i).strName = gBook(i).strName 'strMNum as well and the others 'ArrScore(0) is number of scores that student got! temp(i).ArrScore(0) = gBook(i).ArrScore(0) For c as integer = 1 To gBook(i).ArrScore(0) temp(i).ArrScore(c) = gBook(i).ArrScore(c) Next c Next i the ArrScores() has maximum elements of 10 subscripts so i need to delete one recored of them. but it doesn't really seems to be working :S Your help is much much appreciated !
  8. First off, thank you for offering the help (thumb up) Well, I think I made a mistake in my explanation. Actually it's a bad idea for using JS for the most of the work of course. But I use it for some jquery effects and ajax calls . also, as you mentioned it's only a personal site, not a real live business project! I would be more careful for that I promise! However, here is a overview about what I have done already before coming here, which I think is the same thing you mention: Here is the index.php file for example: <html> <head> // includes all CSS files // includes jQuery framework! //includes all my JS custom functions! <title>INDEX.PHP</title> // THEN ? <script type="text/javascript"> $(function(){ $(".NavBarTab#contact").click(function(){ $(".GeneralContent").load('contactme.php'); }); }); </script> </head> <body> <div class="Navi"> <div class="NavBarTabs">Home</div> <div class="NavBarTabs" id="contact">Contact me</div> <div class="NavBarTabs">About</div> </div> <div class="GeneralContent"> //content! </div> </body> </html> And for the contact me: // No <html><head><body> tags at all! nor js codes embedded ! <div id="FormToggle"> // note: toggle which means there is an effect function I made in my index.php imported JS files! // some elements and events to be clicked to fire an ajax call! </div> That's almost it! I hope it's clear now! Any suggestions?
  9. Hello! I am developing my personal website! And I am trying to implement it with JS completely. which means I need the page to be one front end page. But the back-end pages would be the ones which do the work and calculations! In other words and more explaination: I would like to have one "index.php" page that contains only a nav bar which handles the mission. Once the page load it loads with the content of "home.php" (the landing page) if the "contact" tab is clicked then I would like to load the "ContactMe.php" page content. I have done this already! but the problem is: "home.php" uses alot of javascript functions and jquery plug-ins. when I load it, it looks messy that you think the JS is disabled in your machine :-\ Note: the loading has been done with those methods: .load(), $.ajax! -> same results! The conclusion: * "home.php" * "ContactMe.php" * "About.php" Is it possible to load those pages in the "index.php" and still have their JS functions function correctly and display as if they were accessed individually? The goal that I am trying to achieve is that I make my whole website for the visitor as "One Page Site"! I am challenging myself with it though! Thank you in advance for your informative posts! Please Note that I have only put the <html><head><body> tags in the index.php and removed them from the back-end pages such as home.php, contactme.php and about.php! for that sake that it works correctly! but with no hope!
  10. Oh I am so sorry my friend! I apologize it worked with else if statement ! it's crazy I know but this is what happened! var email = $("#CmtEmail").val(); var filter = /^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$/; var passme = false; var num = 1; if(email == ""){ passme = false; $("#CmtEmail").css({'background-position': '0 -45px'}, 0); return event.preventDefault(event); }else if(!filter.test(email)){ passme = false; num = 2; $("#CmtEmail").css({'background-position': '0 -45px'}, 0); return event.preventDefault(event); }else{ passme = true; $("#CmtEmail").css({'background-position': '0 -90px'}, 0); } console.log( passme + " the value and the email ("+ email +") and "+ num); I really appreciate your help! Thank you thank you thaaaaaaank you the most thanks!
  11. Hello! I am trying to validate name, email and comment inputs! I did the following! $("#CmtSub").click(function(event){ var email = $("#CmtEmail").val(); var filter = /'^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$'/; if( $("#CmtName").val() == "" ){ $("#CmtName").css({'background-position': '0 -45px'}, 0); return event.preventDefault(event); }else{ $("#CmtName").css({'background-position': '0 -90px'}, 0); } if(filter.test(email) && email != "") { $("#CmtName").css({'background-position': '0 -45px'}, 0); return event.preventDefault(event); }else{ $("#CmtName").css({'background-position': '0 -90px'}, 0); } //just to debug console.log("the value and the email ("+ email +")"); if( $("#CmtTxtArea").val() == "" ){ $("#CmtTxtArea").css({'border': 'red 1px solid'}, 0); return event.preventDefault(event); }else{ $("#CmtTxtArea").css({'border': 'white 1px solid'}, 0); } // then proceed ! }); this code here isn't working! any help please ?: if(filter.test(email) && email != "") { //passme = false; $("#CmtName").css({'background-position': '0 -45px'}, 0); return event.preventDefault(event); }else{ //passme = true; $("#CmtName").css({'background-position': '0 -90px'}, 0); } help is greatly appreciated !
  12. Hello guys I need to get something fun! with setTimeout function! I am n00b! so be patent please. I need when <body onload="Myfunc();"> fires, that function should show "Please wait...!" or "Loading...". for , say 5 seconds!. then it disappear. I used setTimeout with that but it didn't do what I wanted! here is my code: function Myfunc(){ document.getElementById("ss2").innerHTML = "Loading..."; setTimeout("Myfunc();", 5000); } // where id="ss2" is the place to display the string "loading..." ! and a one more question ! can I do something like a while loop! where it holds *i* value as seconds! and for every second it passes it should print out a string I make it up ! let say loop for 3 seconds ! So, when seconds = 0 then display "string of second 0" and stick it in there, then go to the next second when it is exactly = 1 then display "new line with string of second 1" and stick it in there, then do to the last second when it is exactly = 2 then display " new line with string of second 2" and stick it in there, exit the loop! is there anyway to do that? please note that I am a n00bie ! help is much appreciated !
  13. oh! that's a software for PC ! I got mac ! I thought it was a website !
  14. Thank you for the advice and reply! will that thing do so for all pictures at once?
×
×
  • 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.