我在用 aardio 的UDP广播功能时测试好像 UDP Bind 网卡无效,不知道是不是我的方法有问题还是这个wsock.udp 库有问题。
电脑里有2个网卡,一个以太网,一个是WLAN,我需要分别给这2个网卡广播数据,但是使用 udpClient.bind()指定的网卡IP ,服务端收到显示始终是一个IP,通过Wireshark抓包信息来看,确实客户端的网卡IP没有变化。我怀疑wsock.udp 有个bug,用其他第三方工具测试,绑定网卡时收到会显示绑定网卡IP。
以下是测试代码和截图,麻烦大佬们测试下指教一二,感谢!

Code AardioLine:94复制
1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.29.30.31.32.33.34.35.36.37.38.39.40.41.42.43.44.45.46.47.48.49.50.51.52.53.54.55.56.57.58.59.60.61.62.63.64.65.66.67.68.69.70.71.72.73.74.75.76.77.78.79.80.81.82.83.84.85.86.87.88.89.90.91.92.93.94.import
win
.ui;import
wsock.udp.asynClient;var
winform = win
.form(text="UDP Bind测试"
;right=997
;bottom=469
)- winform.add(
- button={cls=
"button"
;text="Bind IP 测试"
;left=714
;top=217
;right=908
;bottom=260
;z=5
}; - cmbAdapter={cls=
"listbox"
;left=714
;top=19
;right=983
;bottom=110
;edge=1
;items={};z=4
}; - static={cls=
"static"
;text="选择网卡:"
;left=645
;top=20
;right=711
;bottom=42
;dl=1
;dt=1
;transparent=1
;z=2
}; - txtIP={cls=
"static"
;text="网卡IP:"
;left=656
;top=143
;right=971
;bottom=165
;dl=1
;dt=1
;transparent=1
;z=3
}; - txtLog={cls=
"richedit"
;left=5
;top=7
;right=625
;bottom=460
;bgcolor=1
;db=1
;dl=1
;dr=1
;edge=1
;font=LOGFONT(h=-13
;name='微软雅黑'
);multiline=1
;readonly=1
;vscroll=1
;wrap=1
;z=1
} - )
var
udpServer = wsock.udp.asynClient();- udpServer.setBroadcast(
true
); - udpServer.bind(
"0.0.0.0"
,1901
) import
inet.adapterInfo;var
loadNetworks = function
(){- winform.cmbAdapter.clear();
-
for
adapterInfo in
inet.adapterInfo.each(2) { -
-
for
addr,strAddr in
adapterInfo.eachUnicastAddress(){ strIP=strAddr; } -
-
-
if
(adapterInfo.operStatusUp){ -
-
if
string
.left(adapterInfo.description,8
) != "Software"
{ - winform.cmbAdapter.add(strIP,
1
) - }
- }
- }
-
-
if
(winform.cmbAdapter.count>0
) { winform.cmbAdapter.selIndex=1
} -
- }
- loadNetworks();
- logview =
function
(data,color,bkcolor){ - winform.txtLog.appendText(
tostring
(time
(," [%H:%M:%S] "
) ),data,'\r\n'
); -
-
var
line = winform.txtLog.lineCount-1
; -
var
start = winform.txtLog.lineToChar(line) -
- winform.txtLog.setsel(start,-
1
); - winform.txtLog.setSelCharformat( textColor = color;backColor = bkcolor)
-
- winform.txtLog.deselect();
- winform.txtLog.lineScroll();
-
if
(line>100
) { - winform.txtLog.text=
""
; - }
- }
- winform.cmbAdapter.onSelChange =
function
(){ - winform.txtIP.text =
"所选网卡IP:"
++ winform.cmbAdapter.selText - }
- udpServer.onReceive =
function
(err){ -
var
strReceive= udpServer.recvfrom(1024
); -
var
strIP = udpServer.getRemoteIp(); - logview(
"客户端IP:"
++ strIP,0x0000FF); - logview(
"数据--->:"
++ string
.hex(strReceive," "
),0xFF0000); - }
var
udpClient = wsock.udp.client();- udpClient.setBroadcast(
true
); - winform.button.oncommand =
function
(id,event){ -
- udpClient.bind(winform.cmbAdapter.selText,
1901
) - logview(
"Bind 网卡:"
++ winform.cmbAdapter.selText,0x00FFFF) -
-
var
strM4 ="01 02 03 04"
- udpClient.sendto(
string
.unhex(strM4," "
),'255.255.255.255'
,1901
); -
- }
- winform.show();
win
.loopMessage();