Defense Secretary Caspar W. Weinberger capped a visit to China today by announcing a new program of military cooperation between China and the United States.

Weinberger said U.S. and Chinese military officers will begin exchanging visits in 1984 and they will study each other's training, logistics, battle tactics and other military skills. U.S. Navy ships may also visit Chinese ports for the first time since the Communist revolution of 1949.

Announcement of the exchange program, one of the primary goals of Weinberger's five-day visit, followed yesterday's agreement for President Reagan to visit China next April. Chinese Premier Zhao Ziyang is to visit the United States in January.

The two announcements mark a pronounced improvement in U.S.-Chinese relations after two chilly years, although China's skittishness about being seen as a U.S. ally, which threatened to cause problems at the beginning of Weinberger's visit, had not entirely abated today.

The difficulties were greatest during discussions of the U.S. arms sales to China, another principal goal of Weinberger's trip. Although U.S. officials said both sides want to do business, the questions raised by establishing such a relationship between two former enemies could not be answered in five days.

In general, however, U.S. officials sounded optimistic about the results of the visit.

"The military people are not only warm and friendly, but they're interested in increasing their contacts," said a senior defense official traveling aboard Weinberger's Air Force jet from Shanghai to Hong Kong who spoke with reporters on the condition that he not be named. "That all helps get a much closer ability to work together in any kind of contingency in the future."

The official declined to say what contingencies he had in mind. During his talks in China, Weinberger was careful not to suggest direct strategic cooperation, but he repeatedly stressed his view that the United States and China are united in facing a major threat from the Soviet Union.

The Chinese did not publicly endorse that view, insisting they would never attach themselves to "any great power or bloc of powers." But the senior official on Weinberger's plane said today he is confident after talks with party chairman Deng Xiaoping, Premier Zhao and other officials that the Chinese by and large share his view of Soviet intentions.

"There's a common understanding of the threat that faces everybody, the Soviet threat," the official said. "They have no doubt about that." Despite the generally positive atmosphere of the talks, however, Chinese officials remain suspicious of American motives concerning Taiwan, and the availability of sophisticated U.S. technology.

U.S. officials who met with the Chinese in working groups and high level sessions said the negotiations were cautious, halting and at times painfully indirect.

The officials, uncertain of where they stood even on their last day in China, looked frantically this morning for newspapers with the official Chinese press agency's account of this week's meetings.

"It's not your standard San Francisco labor negotiations," the senior official said. Asked what his goals had been, he joked, "First of all, not to be thrown out on the first day."

The United States is willing to sell China defensive weapons, including the improved TOW antitank missile and the improved Hawk antiaircraft missile, components of early warning radar systems and other weapons. Officials said today they believe the Chinese are interested in buying the TOW, Hawk and others, although in what quantities is uncertain.

But the Chinese also want some more advanced weapons that the United States will not sell and in some cases they apparently did not know exactly what weapons to ask for. The two countries played a delicate dance during the arms sales talks, with the Chinese urging U.S. officials to explain what is available before they made any requests and U.S. officials probing into what China needs before making any firm offers.

U.S. officials also continued to seek assurances that China will not pass technological secrets to third countries like North Korea. Chinese officials objected to aspects of such commitments, but U.S. officials said the matter is likely to be resolved in future talks.

"There will be agreement on the sales, and they'll want to deal with manufacturers, and we have no problem with that," the senior official said.

Agreement on military exchanges was reached in Peking Tuesday and yesterday, but Chinese officials did not agree until early this morning to make the understanding public. One U.S. official said the 1984 starting date will give China an opportunity to measure progress on technology transfer, both in arms sales and the equally contentious area of technology with dual civil-military uses, before beginning the exchanges.

Disclosing both the military exchange programs and scheduled visits of top leaders while Weinberger was still in China was seen as a gesture of good will by the Chinese.

Defense Secretary Harold Brown began a low-level exchange program after he visited China in 1980. But it was not renewed when it lapsed after two years, and U.S. officials said they expect future exchanges to involve more people, more military specialties and more depth of knowledge sharing.

Weinberger arrived in Hong Kong today and was to leave Friday for Sri Lanka and Pakistan.