P2P applications generate a large amount of unnecessary traffic because they know little underlying network information. To optimize P2P traffic, we propose an identifier (called Code-ID, CID for short in the following) construct and distribute method. Each peer has a CID, in our proposal. The distance between two peers can be calculated from their CIDs. Choosing closer peers can not only save network resource, but also improves P2P application's performance in P2P paradigm. Using the CID, network provider (NP) and internet service provider (ISP) can provide ID query service (IDQ) to help P2P application select peers; ISP can perform traffic engineering by influencing the peer selection decisions. To evaluate our method and CID-construct algorithm, we conducted simulations in several different scenarios. The simulations show that our proposal can significantly reduce network consumption and improve P2P download speed; also, it has more scalability compared with other ISP-P2P cooperation traffic optimization method, such as P4P.