UDP怎么实现外网与内网的通信?
RT,UDP通信,局域网内通信好办,广域网通信也好办,但是如果一台外网主机和一台局域网内的能上网的主机通信就不好办了,内网主机发给外网主机的数据包能收到,但是反过来就收不到数据包了,期待高手解答!代码如下:
#Include <ACN_NET.au3>
#include <GUIConstantsEx.au3>
$my_ip=_NetworkGetInternetIP()
UDPStartup()
$GOOEY = GUICreate("aaa", 300, 355)
$edit1 = GUICtrlCreateEdit("", 10, 10, 280, 180)
$ipinput=GUICtrlCreateInput ( @IPAddress1, 75, 200, 150 , 20 )
$edit2 = GUICtrlCreateEdit("", 10, 230, 280, 80)
$send=GUICtrlCreateButton ( "发送", 110, 320 , 80 , 30 )
GUISetState()
$socket_recv=UDPBind($my_ip, 65432)
If @error Then
$socket_recv=UDPBind(@IPAddress1, 65432)
EndIf
Local $msg=""
Local $ip=""
While 1
$msg = GUIGetMsg()
If $msg = $GUI_EVENT_CLOSE Then Exit
$data = UDPRecv($socket_recv,1024,1)
If $data <> "" Then
$data=BinaryToString ($data,4)
If GUICtrlRead($edit1)<>"" Then
GUICtrlSetData($edit1,GUICtrlRead($edit1)& @CRLF &GUICtrlRead ($ipinput) &" To Me::"& $data)
Else
GUICtrlSetData($edit1,GUICtrlRead ($ipinput) &" To Me::"& $data)
EndIf
EndIf
If $msg=$send Then
$ip=GUICtrlRead ($ipinput)
$socket_send=UDPOpen($ip, 65432)
UDPSend($socket_send,StringToBinary($my_ip&"-->"&GUICtrlRead($edit2),4))
UDPCloseSocket ( $socket_send )
If GUICtrlRead($edit1)<>"" Then
GUICtrlSetData($edit1,GUICtrlRead($edit1) & @CRLF &"Me To "&GUICtrlRead ($ipinput)&"::"& GUICtrlRead ($edit2))
Else
GUICtrlSetData($edit1,"Me To "&GUICtrlRead ($ipinput)&"::"& GUICtrlRead ($edit2))
EndIf
GUICtrlSetData ($edit2, "" )
EndIf
WEnd 本帖最后由 l4ever 于 2010-5-25 02:08 编辑
反过来当然不行,因为没端口映射
LAN -> WAN 当然可以
WAN -> LAN 只是发到lan的外网IP上了,需在路由上映射用到的端口到LAN机器上 #Include <ACN_NET.au3>
#include <GUIConstantsEx.au3>
$my_ip=_NetworkGetInternetIP()
UDPStartup()
$GOOEY = GUICreate("aaa", 300, 355)
$edit1 = GUICtrlCreateEdit("", 10, 10, 280, 180)
$ipinput=GUICtrlCreateInput ( "118.251.140.194", 75, 200, 100 , 20 )
$portinput=GUICtrlCreateInput ( "65432", 185, 200, 50 , 20 )
$edit2 = GUICtrlCreateEdit("", 10, 230, 280, 80)
$send=GUICtrlCreateButton ( "发送", 110, 320 , 80 , 30 )
GUISetState()
$socket_recv=UDPBind($my_ip, 65432)
If @error Then
$socket_recv=UDPBind(@IPAddress1, 65432)
EndIf
Local $msg=""
Local $ip=""
While 1
$msg = GUIGetMsg()
If $msg = $GUI_EVENT_CLOSE Then Exit
$data = UDPRecv($socket_recv,1024,3)
If $data <> "" Then
$data=BinaryToString ($data,4)
If GUICtrlRead($edit1)<>"" Then
GUICtrlSetData($edit1,GUICtrlRead($edit1)& @CRLF &$data&"("&$data&") To Me::"& $data)
Else
GUICtrlSetData($edit1,$data&"("&$data&") To Me::"& $data)
EndIf
EndIf
If $msg=$send Then
$ip=GUICtrlRead ($ipinput)
$port=GUICtrlRead ($portinput)
$socket_send=UDPOpen($ip, $port)
UDPSend($socket_send,StringToBinary(GUICtrlRead($edit2),4))
UDPCloseSocket ( $socket_send )
If GUICtrlRead($edit1)<>"" Then
GUICtrlSetData($edit1,GUICtrlRead($edit1) & @CRLF &"Me To "&GUICtrlRead ($ipinput)&"::"& GUICtrlRead ($edit2))
Else
GUICtrlSetData($edit1,"Me To "&GUICtrlRead ($ipinput)&"::"& GUICtrlRead ($edit2))
EndIf
GUICtrlSetData ($edit2, "" )
EndIf
WEnd
改了下,还是不行,为什么呢?
测试自己电脑时,这个获取到的端口怎么都不是我打开的那个端口,而是每次都不同,是一个1K多的端口,这是什么端口? 所以网络通信的还是要从基本的TCP和UDP原理学起。
内网通信外网UDP没戏。 还想学习呢,先顶下把。 做端口映射要在接外网的路由器里设置,这个需要你有设置路由器的权限 先顶一下,{:face (427):} 还想学习呢,先顶下把。
页:
[1]