alin19 Posted October 10, 2008 Share Posted October 10, 2008 i have a server to what i connect, and that server sends me data almost all the time, and i have to send some data to it also, so i have developed this code. but my connect program waits for some date to come and after that send's it so if there for some time i don't recive any dates it won't send any, just waiting to read .... can you help me? <?php $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); socket_connect($sock,"127.0.0.1", 9191); socket_set_block($sock); if (false === ($line = socket_read($sock, 2048))) echo "socket_read() failed: reason: " . socket_strerror(socket_last_error($msgsock)) . "\n"; else echo $line."\r\n"; if (false === ($line = socket_read($sock, 2048))) echo "socket_read() failed: reason: " . socket_strerror(socket_last_error($msgsock)) . "\n"; else echo $line."\r\n"; $user=toUTF("user"); socket_write($sock, $user); if (false === ($line = socket_read($sock, 2048))) echo "socket_read() failed: reason: " . socket_strerror(socket_last_error($msgsock)) . "\n"; else echo $line."\r\n"; $parola=toUTF("password"); socket_write($sock, $parola); while (true) { if (false === ($line = socket_read($sock,10000))) echo "socket_read() failed: reason: " . socket_strerror(socket_last_error($msgsock)) . "\n"; else { echo $line; } $order="test"; $order=toUTF($trim); if (socket_write($sock, $order)) } function toUTF( $content ) { $contentLen = strlen( $content ); $newContent = pack( "n", $contentLen ); $newContent .= $content; for ( $i = 0; $i < strlen( $newContent ); $i++ ) { print ord( substr( $newContent, $i, 1 ) )." "; } print "\n"; return $newContent; } ?> Link to comment https://forums.phpfreaks.com/topic/127839-socket-read-and-write-in-the-same-time/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.