我在用 aardio 的UDP广播功能时测试好像 UDP Bind 网卡无效,不知道是不是我的方法有问题还是这个wsock.udp 库有问题。
电脑里有2个网卡,一个以太网,一个是WLAN,我需要分别给这2个网卡广播数据,但是使用 udpClient.bind()指定的网卡IP ,服务端收到显示始终是一个IP,通过Wireshark抓包信息来看,确实客户端的网卡IP没有变化。我怀疑wsock.udp 有个bug,用其他第三方工具测试,绑定网卡时收到会显示绑定网卡IP。
以下是测试代码和截图,麻烦大佬们测试下指教一二,感谢!
import win.ui;
import wsock.udp.asynClient;
/*DSG{{*/
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}
)
/*}}*/
//创建UDP服务器
var udpServer = wsock.udp.asynClient();
//启用UDP服务器广播
udpServer.setBroadcast(true);
//udpServer.reuseAddress(true);
udpServer.bind("0.0.0.0",1901)
import inet.adapterInfo;
//adapterInfo方式遍历网卡
var loadNetworks = function(){
winform.cmbAdapter.clear();
for adapterInfo in inet.adapterInfo.each(2/*_AF_INET*/) {
//IP地址
for addr,strAddr in adapterInfo.eachUnicastAddress(){ strIP=strAddr; }
//判断网卡是否可用
if(adapterInfo.operStatusUp){ //如果可以,则添加到列表
//Software Loopback Interface
if string.left(adapterInfo.description,8) != "Software" {
winform.cmbAdapter.add(strIP,1)
}
}
}
if (winform.cmbAdapter.count>0) { winform.cmbAdapter.selIndex=1 }
}
//加载网卡
loadNetworks();
//显示Log信息
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(); //远端IP
logview("客户端IP:" ++ strIP,0x0000FF);
logview("数据--->:" ++ string.hex(strReceive," "),0xFF0000);
}
//创建UDP客户端
var udpClient = wsock.udp.client();
//启用广播模式
udpClient.setBroadcast(true);
//网卡绑定测试
winform.button.oncommand = function(id,event){
//绑定当前所选IP
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();