Can HIV transfer from naked anus to penis (cotton trouser, no underwear) through rubbing and ejaculating. What penetration is in anus I mean how far?

Keep protection. It sounds as if you are describing rubbing through your own clothing against someone who was unclothed. You're not going to catch HIV this way. If you're describing skin-to-skin contact with any penetration, there is some risk to you (though much less than to the passive partner if you were HIV positive.) if you want to continue this dangerous exploring, then please use barrier protection.