Growing up in northern China as the son of a factory worker and a housewife, Mr. Ma hoped to go to college and become a teacher. But his parents thought his dreams were too costly, and he was sent to the local police academy instead.

It was there, he said, in a macho culture that revolved around talking about women, that he realized he was gay.

At the time, in the mid-1990s, gay sex was considered a crime in China and homosexuality was classified as a psychological disorder. At the police academy, Mr. Ma took courses on criminal psychology where cadets were told that gay people should be viewed suspiciously because they were more likely to commit crimes.

“When I realized I was different from other people,” he recalled, “I thought I was ill.”

Mr. Ma turned to the internet for advice. But instead of finding a supportive community, he found rants describing gay people as lunatics and perverts. On health websites, he was bombarded with recommendations to seek medication and electroshock treatment.

After becoming a police officer, Mr. Ma was inspired in 2000 to start his own website, Danlan.org, Chinese for “light blue,” evoking the clear coastal skies of his childhood. The site offered chat forums and advice on reducing the risk of H.I.V. and other sexually transmitted diseases. Danlan soon became a popular way for gay men in China to connect in an age when many had been resorting to scrawling meeting dates and places on bathroom stalls, worried about the stigma of coming out.

At work, Mr. Ma chased burglars, filed incident reports and recorded public service announcements. In his spare time, he raced to the keyboard, writing essays for Danlan and chatting with friends under the pseudonym Geng Le.

Mr. Ma kept up the routine for more than a decade. He married, under pressure from friends and family. But when his supervisors confronted him about his website in 2012, he offered his resignation. His family was devastated.