假設有兩臺分別處于各自的私有網絡中的主機:A和B;N1和N2是兩個NAT設備;S是一個使用了一個眾所周知的、從全球任何地方都能訪問得到的IP地址的公共服務器
讓客戶滿意是我們工作的目標,不斷超越客戶的期望值來自于我們對這個行業的熱愛。我們立志把好的技術通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領域值得信任、有價值的長期合作伙伴,公司提供的服務項目有:申請域名、虛擬空間、營銷軟件、網站建設、沙雅網站維護、網站推廣。
步驟一:A和B分別和S建立UDP連接;NAT設備N1和N2創建UDP轉換狀態并分配臨時的外部端口號
步驟二:S將這些端口號傳回A和B
步驟三:A和B通過轉換好的端口直接聯系到對方的NAT設備;NAT設備則利用先前創建的轉換狀態將分組發往A和B
源碼已發送請查收
ip nat inside source list 訪問列表標號 pool 內部合法地址池名字
ip nat inside destination static10.106.1.16 172.1.1.15
將外部局部地址轉換為外部全局地址
nat inside destination static tcp 10.106.1.16 21 172.1.1.11 21
將外部局部端口21轉換為外部全局端口21
在示例中好理解,建議結合實際操作好掌握。以下網上找的。。。
NAT 具體命令理解
1,由內向外的轉換,在路由器的inside口處發生了NAT轉換行為
r1-2514(config)#ip nat inside ?
destination Destination address translation
source Source address translation
從上面可以看出,在inside邊可以對數據包中的源地址或者目標地址進行轉換.
r1-2514(config)#ip nat inside source ?
list Specify access list describing local addresses
route-map Specify route-map
static Specify static local-global mapping
從上面可以看出,針對源地址進行轉換可以使用acl 或者 route-map來表述一個本地地址,意思是數據包中源地址符合這些的都要被轉換. 也可以使用static進行靜態映射,指定一個靜態的從本地到全局的映射.
r1-2514(config)#ip nat inside source list 1 ?
interface Specify interface for global address
pool Name pool of global addresses
從上面輸出可以看出接下來要給一個全局地址,數據包中的源地址將被這個全局地址替代.
對于靜態映射,還可以指定協議 端口號:
When translating addresses to an interface's address, outside-initiated connections to services on the inside network (like mail) will require additional configuration to send the connection to the correct inside host. This command allows the user to map certain services to certain inside hosts.
ip nat inside source static { tcp | udp } localaddr localport globaladdr globalport
Example:
ip nat inside source static tcp 192.168.10.1 25 171.69.232.209 25
In this example, outside-initiated connections to the SMTP port (25) will be sent to the inside host 192.168.10.1.
在inside邊對目標進行轉換:
r1-2514(config)#ip nat inside destination ?
list Specify access list describing global addresses
從上面輸出可以看出,路由器要求輸入一個表示全局地址的ACL
r1-2514(config)#ip nat inside destination list 1 ?
pool Name pool of local addresses
pool Name pool of local addresses
接著要求輸入一個本地地址池
所以這是一個針對從outside向inside方向數據的NAT,凡是在這個方向數據包中目標地址符合ACL描述的全部被轉換成POOL中的本地地址.這可以被用來進行TCP的負載均衡,即外部都請求同一個全局地址,而在路由器的inside邊,這些請求的目標地址全部被轉換成地址池中的地址,而且是循環使用地址池中的地址,從而達到負載均衡,但是這種方法只適合TCP流,同時不適宜用在WEB服務的負載均擔上.詳細解釋看這里:
Destination Address Rotary Translation
A dynamic form of destination translation can be configured for some outside-to-inside traffic. Once a mapping is set up, a destination address matching one of those on an access list will be replaced with an address from a rotary pool. Allocation is done in a round-robin basis, performed only when a new connection is opened from the outside to the inside. All non-TCP traffic is passed untranslated (unless other translations are in effect).
This feature was designed to provide protocol translation load distribution. It is not designed nor intended to be used as a substitute technology for Cisco's LocalDirector product. Destination address rotary translation should not be used to provide web service load balancing because, like vanilla DNS, it knows nothing about service availability. As a result, if a web server were to become offline, the destination address rotary translation feature would continue to send requests to the downed server.
2.由外向內,在OUTSIDE邊發生的行為:
r1-2514(config)#ip nat outside ?
source Source address translation
從上面可以看出在OUTSIDE邊,只能對數據包中的源地址轉換
r1-2514(config)#ip nat outside source ?
list Specify access list describing global addresses
route-map Specify route-map
static Specify static global-local mapping
從上面可以看出接下來路由器要求給定一個全局地址的描述,可以是ACL route-map 或者 靜態的.
r1-2514(config)#ip nat outside source list 1 ?
pool Name pool of local addresses
從上面可以看出,路由器接著又要求給定一個本地地址,這說明 這個命令是對從外到內的數據包,進行源地址字段的替換,它將外部全局地址轉換成內部地址(內部本地或者內部全局,內部本地和內部全局可以相同也可以不同)
ip nat outside source { list acl pool name | static global-ip local-ip }
The first form (list..pool..) enables dynamic translation. Packets from addresses that match those on the simple access list are translated using local addresses allocated from the named pool.
The second form (static) of the command sets up a single static translation.
一個例子:
CONFIGURATION EXAMPLES
The following sample configuration translates between inside hosts addressed from either the 192.168.1.0 or 192.168.2.0 nets to the globally-unique 171.69.233.208/28 network.
ip nat pool net-20 171.69.233.208 171.69.233.223 netmask netmask 255.255.255.240
ip nat inside source list 1 pool net-20
!
interface Ethernet0
ip address 171.69.232.182 255.255.255.240
ip nat outside
!
interface Ethernet1
ip address 192.168.1.94 255.255.255.0
ip nat inside
!
access-list 1 permit 192.168.1.0 0.0.0.255
access-list 1 permit 192.168.2.0 0.0.0.255
The next sample configuration translates between inside hosts addressed from the 9.114.11.0 net to the globally unique 171.69.233.208/28 network. Packets from outside hosts addressed from 9.114.11.0 net (the "true" 9.114.11.0 net) are translated to appear to be from net 10.0.1.0/24.
ip nat pool net-20 171.69.233.208 171.69.233.223 netmask netmask 255.255.255.240
定義一個名稱為 net-20的內部全局地址池
ip nat pool net-10 10.0.1.0 10.0.1.255 netmask netmask 255.255.255.0
定義一個名稱為net-10的外部本地地址池
ip nat inside source list 1 pool net-20
ip nat outside source list 1 pool net-10
注意inside /outside全部調用了list 1 這說明 內外兩邊的源地址是重疊地址,通過將內部的源地址轉換成net-20中地址和外部的9.114.11.0網絡通信。將外部的源地址轉換成net-10中的地址來與內部這邊的9.114.11.0網絡通信
!
interface Ethernet0
ip address 171.69.232.182 255.255.255.240
ip nat outside
!
interface Ethernet1
ip address 9.114.11.39 255.255.255.0
ip nat inside
!
access-list 1 permit 9.114.11.0 0.0.0.255
Private?Sub?Command1_Click()
Dim?b?As?Variant,?a?As?Variant
b?=?Array(1,?3,?5,?7,?9)
For?i?=?0?To?(5?-?1)?\?2
a?=?b(i)
b(i)?=?b(4?-?i)
b(4?-?i)?=?a
Next?i
For?i?=?0?To?4
Print?b(i)
Next?i
End?Sub
代碼你沒給全,我寫了個
這個分幾種情況的
先說說前提
聊天軟件需要一個服務器存放客戶端列表吧
只要客戶端是在線的,那客戶端與服務端必須是處于連接狀態(這樣服務器就可以作為客戶端的中間人)
在這里Server(為服務端),Client A(客戶端1)Client B(客戶端2)
1,Client A是外網,Client B是外網 這種情況很簡單,Client A只要從服務器端得到Client B的信息(IP與登記的端口)就能夠建立連接
2,Client A是外網,Client B是內網 這種情況稍復雜,Client A連接Client B會失敗,這時,Client A向Server發出請求,這時,Server通知Client B,讓Client B連接Client A.. 因為Client A是外網機器,所以這樣也就建立連接了
3,Client A與Client B都在內網,p2p關鍵是在這里,
當Client A連接Client B失敗,Client A向Server發出請求,Server通知Client B對Client A進行連接,這時Client B他能連接上Client A!但是,如果剛才Client A沒有連接Client B,那現在的Client B就連接不上Client A!反之:這里重新再來啊,Client B連接Client A失敗,Client B向Server發出請求,Server通知Client A連接Client B,這時Client A也能連接上Client A!
在這里需要說一下NAT(網絡地址轉換),就是當內網用戶連接外網時,NAT可以可以將內網地址轉換成公網的IP地址與外網進行通信,但外網有數據返進入內網時,有兩種可能(如果前面內網有向這個外網地址發送過數據包,那么NAT會把這個外網的連接轉換為剛剛發送過數據包的內網地址)
再回來說前面的,Client A為什么會連接Client B失敗?,因為Client A的數據包到達Client B的NAT時被丟棄了,而這時,Client B為什么又如何能連接Client A成功呢,因為當Client B的數據包到達Client A的NAT時,NAT檢測到剛剛Client A有發送過數據包到Client B,這個時候NAT會將這個連接轉換到Client A上去... 這樣就連接成功了
說著我都有點暈了.不知道你看著會不會暈..- -!看著暈慢慢看吧..CSDN中有VB版的UDP打洞例子,你找下...
我們知道內網ip和外網ip可能不同,這涉及到“網絡地址轉換”(NAT)。
事實上,*只有*和公網聯絡才有可能獲取外網ip地址,直接用winsock的localip屬性,或者ipconfig之類的方法(在內外網ip不同的情況下)獲取的都是內網ip!
獲取外網ip可以用你說的ip138網頁的方式,可以通過winsock、inet、xmlhttp、webbrowser等組件來獲取遠程服務器返回的數據,即外網ip——從服務器的角度看的你的ip地址。此外也可以使用STUN協議??傊?,這些方法都需要處在外網的服務器,因為NAT設備是不會讓內網主機獲取其ip映射表的。
我可以實現上述提到的每一種獲取外網ip的方法。如有需要請追問