How do animals integrate internal drives and external environmental cues to coordinate behaviors? We address this question by studying mate-searching behavior in C. elegans. C. elegans males explore their environment in search of mates (hermaphrodites) and will leave food if mating partners are absent. However, when mates and food coincide, male exploratory behavior is suppressed and males are retained on the food source. We show that the drive to explore is stimulated by male-specific neurons in the tail, the ray neurons. Periodic contact with the hermaphrodite detected through ray neurons changes the male's behavior during periods of no contact and prevents the male from leaving the food source. The hermaphrodite signal is conveyed by male-specific interneurons that are postsynaptic to the rays and that send processes to the major integrative center in the head. This study identifies key parts of the neural circuit that regulates a sexual appetitive behavior in C. elegans.