PHP stream_get_contents from TCP client hangs if called more than once -


i writing program connects tcp server stream_socket_client in php. problem if echo contents of $client object before perform additional fwrites, page hangs. work if send of requests before calling stream_get_contents, once call stream_get_contents, client no longer responds? thankful given.

**edit api using: https://www.onlinenic.com/cp_english/template_api/download/onlinenic_api2.0.pdf

**edit

//see full code below //------client creation code, precho function function gettestclient($address, $port) {     $client = stream_socket_client("$address:$port", $errno, $errormessage);     if ($client === false) {         throw new unexpectedvalueexception("failed connect: $errormessage");     }     return $client; } function precho($s) {     echo "<pre>";     echo $s;     echo "</pre>"; } //-------problem code //$loginrequest, $domainavailablerequest, , $logoutrequest - see full code below //----------------------------------------- $address = "www.somesite.com";//this fill in address $port = "12345";//this fill in port $client = gettestclient($address, $port); //----------------------------------------- fwrite($client, $loginrequest); precho(htmlspecialchars(stream_get_contents($client))); fwrite($client, $domainavailablerequest); precho(htmlspecialchars(stream_get_contents($client))); fwrite($client, $logoutrequest); precho(htmlspecialchars(stream_get_contents($client))); fclose($client);   //-------alternative working code, if read responses @ once... //*but want able read each response individually...  fwrite($client, $loginrequest); fwrite($client, $domainavailablerequest); fwrite($client, $logoutrequest); precho(htmlspecialchars(stream_get_contents($client))); fclose($client);  

full code:

<?php             function getdomaintype($ext)     {         $domaintypes = ['com'=>0, 'net'=>0, 'org'=>807, 'biz'=>800, 'info'=>805, 'us'=>806, 'in'=>808,                          'mobi'=>903, 'eu'=>902, 'asia'=>905, 'me'=>906, 'name'=>804, 'tel'=>907, 'cc'=>[600,610], 'tv'=>400,                          'tw'=>302, 'uk'=>901, 'co'=>908, 'xxx'=>930, 'pw'=>940, 'club'=>740, 'bike'=>2001, 'clothing'=>2002,                          'guru'=>2003, 'holdings'=>2004, 'plumbing'=>2005, 'singles'=>2006, 'ventures'=>2007, 'camera'=>2008,                          'equipment'=>2009, 'estate'=>2010, 'gallery'=>2011, 'graphics'=>2012, 'lighting'=>2013, 'photography'=>2014,                          'construction'=>2015, 'contractors'=>2016, 'site'=>950, 'online'=>951, 'sex'=>936,                         'directory'=>2017, 'kitchen'=>2018, 'land'=>2019, 'technology'=>2020, 'today'=>2021, 'diamonds'=>2022,                          'enterprises'=>2023, 'tips'=>2024, 'voyage'=>2025, 'careers'=>2026, 'photos'=>2027,                          'recipes'=>2028, 'shoes'=>2029, 'cab'=>2030, 'company'=>2031, 'domains'=>2032, 'limo'=>2033,                          'academy'=>2034, 'center'=>2035, 'computer'=>2036, 'management'=>2037, 'systems'=>2038,                          'builders'=>2039, 'email'=>2040, 'solutions'=>2041, 'support'=>2042, 'training'=>2043, 'camp'=>2044,                          'education'=>2045, 'glass'=>2046, 'institute'=>2047, 'repair'=>2048, 'coffee'=>2049, 'florist'=>2050,                          'house'=>2051, 'international'=>2052, 'solar'=>2053, 'marketing'=>2054, 'viajes'=>2055, 'farm'=>2056,                          'codes'=>2057, 'cheap'=>2058, 'zone'=>2059, 'agency'=>2060, 'bargains'=>2061, 'boutique'=>2062,                          'cool'=>2063, 'watch'=>2064, 'works'=>2065, 'expert'=>2066, 'foundation'=>2067, 'exposed'=>2068,                          'villas'=>2069, 'flights'=>2070, 'rentals'=>2071, 'cruises'=>2072, 'vacations'=>2073, 'condos'=>2074,                          'properties'=>2075, 'maison'=>2076, 'tienda'=>2077, 'dating'=>2078, 'events'=>2079, 'partners'=>2080,                          'productions'=>2081, 'community'=>2082, 'catering'=>2083, 'cards'=>2084, 'cleaning'=>2085, 'tools'=>2086,                          'industries'=>2087, 'parts'=>2088, 'supplies'=>2089, 'supply'=>2090, 'report'=>2091, 'vision'=>2092,                          'fish'=>2093, 'services'=>2094, 'capital'=>2095, 'engineering'=>2096, 'exchange'=>2097, 'gripe'=>2098,                          'associates'=>2099, 'lease'=>2100, 'media'=>2101, 'pictures'=>2102, 'reisen'=>2103, 'toys'=>2104,                          'university'=>2105, 'town'=>2106, 'wtf'=>2107, 'fail'=>2108, 'financial'=>2109, 'limited'=>2110,                          'care'=>2111, 'clinic'=>2112, 'surgery'=>2113, 'dental'=>2114, 'tax'=>2115, 'cash'=>2116,                          'fund'=>2117, 'investments'=>2118, 'furniture'=>2119, 'discount'=>2120, 'fitness'=>2121, 'schule'=>2122,                          'sexy'=>2500, 'tattoo'=>2501, 'link'=>2502, 'guitars'=>2503, 'gift'=>2504, 'pics'=>2505, 'photo'=>2506,                          'christmas'=>2507, 'blackfriday'=>2508, 'hiphop'=>2509, 'juegos'=>2510, 'audio'=>2511, 'click'=>2512,                          'hosting'=>2513, 'property'=>2514, 'top'=>770, 'porn'=>932, 'adult'=>934, 'city'=>2129, 'ceo'=>742];                             if(isset($domaintypes[strtolower($ext)]))         {             return $domaintypes[strtolower($ext)];         }         return "unknown";     }     function gettcpclient()     {      }     function gettestclient($address, $port)     {         $client = stream_socket_client("$address:$port", $errno, $errormessage);         if ($client === false) {             throw new unexpectedvalueexception("failed connect: $errormessage");         }         return $client;     }     function uniqueinvoiceid()     {         $legend = "0123456789";         $len = 22;         $result = "";         for($i=0;$i<$len;$i++)         {             $result .= $legend[rand(0,9)];         }         return "client".$result;     }     function precho($s)     {         echo "<pre>";         echo $s;         echo "</pre>";     }     $clid = "135610";//onlinenic's test account username     $clpass = "654123";//onlinenic's test account password     $cltrid = uniqueinvoiceid();//client record id/invoice     echo $cltrid."<br><br>";     //md5(clid + md5(clpass) + cltrid + “login”)     $checksumlogin = md5($clid.md5($clpass).$cltrid."login");//checksum     $checksumlogout = md5($clid.md5($clpass).$cltrid."logout");//checksum     $loginrequest = "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>                         <request>                             <category>client</category>                             <action>login</action>                             <params>                                 <param name=\"clid\">$clid</param>                             </params>                             <cltrid>$cltrid</cltrid>                             <chksum>$checksumlogin</chksum>                         </request>";     $logoutrequest = "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>                         <request>                             <category>client</category>                             <action>logout</action>                             <params>                                 <param name=\"clid\">$clid</param>                             </params>                             <cltrid>$cltrid</cltrid>                             <chksum>$checksumlogout</chksum>                         </request>";     $checkdomain = "somedomain.com";     $ext = "com";     $dtype = getdomaintype($ext);     $requestid = uniqueinvoiceid();     $domainavailchecksum = md5($clid.md5($clpass).$requestid."checkdomain".$dtype.$checkdomain);     $domainavailablerequest = "<?xml version=\"1.0\"?>                                 <request>                                     <category>domain</category>                                     <action>checkdomain</action>                                     <params>                                         <param name=\"domaintype\">$dtype</param>                                         <param name=\"domain\">$checkdomain</param>                                     </params>                                     <cltrid>$requestid</cltrid>                                     <chksum>$domainavailchecksum</chksum>                                 </request>";     $client = gettestclient("tcp://ote.onlinenic.com", "30009");     fwrite($client, $loginrequest);     precho(htmlspecialchars(stream_get_contents($client)));     fwrite($client, $domainavailablerequest);     precho(htmlspecialchars(stream_get_contents($client)));     fwrite($client, $logoutrequest);     precho(htmlspecialchars(stream_get_contents($client)));     fclose($client);           ?> 

the second parameter stream_get_contents $maxlength, maximum number of bytes read. when not set (or default of -1 passed), entire stream read, up end of stream.

this means stream_get_contents continue read data stream until there no more, when stream closed other end. (stream_get_contents not know details of protocol, can not know "supposed" stop reading after "one response" server; doesn't have concept of means.)

this means, if have query-response protocol on tcp connection, have either stream_get_contents() exactly length of data want receive (if know response size be), or have read data server , process it yourself.

how going depend on protocol. if line-oriented, can use fgets($client) retrieve 1 line @ time. if binary protocol, can use stream_get_contents($client, $sizeofyourpacket).


since have xml-based protocol, want create new function handles reading responses , understands xml, knows how data read , when can send response (possibly processed) application.

without having credentials, it's little hard see how server behaves, beyond this, you're going have experiment.


Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

Cross-Compiling Linux Kernel for Raspberry Pi - ${CCPREFIX}gcc -v does not work -

java.lang.NoClassDefFoundError When Creating New Android Project -