nrobi Posted October 4, 2009 Share Posted October 4, 2009 I'm a noob when it comes to socket programming. I have two programs one in PHP (a string parser/MySQL app that continually runs) and a Win32 C++ program that does everything else. The problem I'm having is when I attempt to read the socket in PHP.... but only in certain circumstances. Let me post the cost and then I'll explain what's happening. C++ 1st then PHP if(OpenClipboard(NULL)) { const HANDLE hglb = GetClipboardData(CF_TEXT); if (hglb != NULL) { //const char * lptstr = (const char *) GlobalLock(hglb); data = (const char *) GlobalLock(hglb); err = ::send(s,data.c_str(),data.size(),0); err = ::recv(s,buffer,64,0); if (data.size()) GlobalUnlock(hglb); } CloseClipboard(); } $input = socket_read($client[$i]['sock'] , 65535); // while(false !== ($str = socket_read($client[$i]['sock'] , 1024))) // if(empty($str)) break; // else $input .= $str; When I send small strings over everything is fine. But the data I'll be sending over is > 32kB. On the C side I've dumped the data variable to a file. Everything is there. The first file is a little over 24kB. I've tried a few different things on the PHP side so here they are with their results. One other thing I must say is that a wrote a short PHP script just to send the termination string to the PHP socket server. socket_read(....,65535): C++ debugger indicates send() sent 24kB Dumping $input to a file gives me a 14kB file Termination string from C++ app does nothing...unless I add Sleep(1000) in between the send/recv in my C++ code above... it's milliseconds in Win32 while-loop: C++ debugger indicates send() sent 24kB Dumping $input to screen from loop shows full file The while loop is not infinite. socket_read() hangs Termination string from C++ app or PHP script does nothing. I need to Ctrl-Brk to end it The PHP code is essentially the socket server from http://www.functionblog.com/?p=67=1 with my own input handling... minus the logging. I took it out once I could send over multiple [short] strings w/o problems. Any ideas? Any help is appreciated. I'm also curious why the Sleep() call fixes the termination string issue. In the debugger with or without that call recv() reads in the same string. Thanks Link to comment https://forums.phpfreaks.com/topic/176429-socket_read-problem/ Share on other sites More sharing options...
nrobi Posted October 4, 2009 Author Share Posted October 4, 2009 I tried the following: while(false !== ($str = socket_read($client[$i]['sock'] , 1024))) { $input .= $str; if(empty($str) || strlen($str) < 1024) break; } This reads in the file and outputs the correct response but then I can't terminate the server..... i.e. read in a second string. If it's more useful here's the server loop: while ($run) { socket_set_block($sock); $read[0] = $sock; for ($i = 0; $i < $max_clients; $i++) { if ($client[$i]['sock'] != null) $read[$i + 1] = $client[$i]['sock'] ; } $ready = socket_select($read,$write,$except,null); if (in_array($sock, $read)) { for ($i = 0; $i < $max_clients; $i++) { if ($client[$i]['sock'] == null) { $client[$i]['sock'] = socket_accept($sock); break; } elseif ($i == $max_clients - 1) print ("too many clients"); } if (--$ready <= 0) continue; } // end if in_array // If a client is trying to write - handle it now for ($i = 0; $i < $max_clients; $i++) { // for each client if (in_array($client[$i]['sock'] , $read)) { // $input = socket_read($client[$i]['sock'] , 65535); while(false !== ($str = socket_read($client[$i]['sock'] , 1024))) { $input .= $str; if(empty($str) || strlen($str) < 1024) break; } if ($input == null) unset($client[$i]); if($input) { if ($input == 'exit') { socket_close($client[$i]['sock']); unset($client[$i]['sock']); $run = false; } /*else handle other cases for $input*/ } else { // Close the socket socket_close($client[$i]['sock']); unset($client[$i]); } } } } // end while Link to comment https://forums.phpfreaks.com/topic/176429-socket_read-problem/#findComment-929980 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.