The two communist neighbors have swung between conflict and cooperation over centuries, with Hanoi now leaning more openly toward Beijing.