The state of Shanshan 鄯善 (Türkic name Kargan) was the successor state of Loulan 樓蘭 after the Han dynasty 漢 (206 BCE-220 CE) had forced the latter into submission. The territory of Shanshan was almost completely located in the desert. The new capital was Yuni 扜泥. The population lived from cattle breeding and the production of jade stones, the export of reeds and other agronomically used plants. The blacksmiths were famous for their weapons.
The Han dynasty had forced upon the king of Shanshan a bureaucratic administration, with a Commander Supporting the State (fuguohou 輔國侯), a Commander Warding off the Barbarians (quehuhou 卻胡侯), a Commander-in-chief (duwei 都尉) and a Commander for the Attack on Cheshi 車師 (ji Cheshi duwei 擊車師都尉).
The dependency from the Han empire relaxed somewhat during the Later Han period, and Shanshan was able to control the access to the southern route of the Silk Road. In 45 CE, eighteen states of the Western Territories (xiyu 西域) sent an embassy to the Han court asking for the establishing of a protectorate (duhufu 都護府), but the emperor declined. A year later the king of Shache 莎車 suggested blocking the way to the east towards the Han empire, but King An 安 of Shanshan declined and killed the envoy of Shache.
King Xian 賢 of Shache thereupon attacked Shanshan and forced King An to flee. A second request to the Han court to establish a protectorate in order to ensure the peace of the region was again declined. Shanshan thereupon turned its back to the Han court and again became an ally of the mighty steppe federation of the Xiongnu 匈奴.
In 73 CE it happened that Ban Chao 班超, envoy of the Han court, and a Xiongnu envoy arrived in Shanshan at the same time. Ban Chao killed the Xiongnu envoy and forced King Guang 廣 to surrender to the Han. A permanent envoy of the Han was appointed in 119 who was residing in Yiwu 伊吾, an outpost of the commandery of Dunhuang 敦煌. Shortly after an army of Rear Cheshi 後車師 killed the Chinese envoy Suo Ban 索班. The king of Shanshan asked the governor of Dunhuang for support.
In 124 Ban Yong 班勇 bestowed the king of Shanshan with an imperial seal. A year later, Rear Cheshi was defeated, and the Protectorate of the Western Territories (Xiyu duhufu 西域都護府) came into full being. The kings of Shanshan stayed loyal to the next Chinese dynasties and were willing to become vassals to the Wei 曹魏 (220-265) and Jin 晉 (265-420) dynasties.
In 445, the Northern Wei 北魏 (386-534) general Wan Dugui 萬度歸 conquered Shanshan, killed King Zhen-da 真達 and established a defense command (zhen 鎮) in the place of Shanshan.