/** * will read data from the socket. Since we are using non blocking * mode, if we attempt to read the data even a millisecond before * it becomes available, the method just returns null. * * So what's the cure? we will loop for a while and try to read it * in multiple times. Note that if we used blocking mode there may * be instances when this function NEVER returns. */ function sock_read($i=0) { /* * global variables are lame, we will create a class in the * next step */ global $sock; $s = fgets($sock); if($s=='') { if($i==30) { break; } else { sleep(1); return sock_read($i+1); } } return $s; } function is_ok() { $s = sock_read(); if($s == "" || preg_match('/^5/',$s) ) { return 0; } else { return 1; } } function sock_write($s) { global $sock; echo "< $s\n"; fputs($sock,"$s\n"); } $sock = fsockopen("localhost",21,&$errno,&$errstr,30); set_socket_blocking($sock,false); if($sock) { if(is_ok()) { sock_write("user imapuser"); if(is_ok()) { sock_write("pass 123"); is_ok(); } } }