tcp客户端和服务端

axuanup 5月前 742

import win.ui;
/*DSG{{*/
var winform = win.form(text="aardio form";right=759;bottom=469)
winform.add(
button={cls="button";text="Button";left=128;top=104;right=272;bottom=144;z=1}
)
/*}}*/

import hpsocket.tcpServer;

var server = hpsocket.tcpServer();
server.start("192.168.1.143",6003);


server.onThreadCreated = function(){
	/*监听线程首次回调前触发*/
}
server.onThreadEnd = function(){
	/*监听线程退出前触发*/
}
server.onPrepareListen = function(hpTcpServer,soListen){
	/*绑定监听地址前触发*/
}
server.onAccept = function(hpTcpServer,connId,pClient){
	/*接受请求触发*/
}
server.onHandShake = function(hpTcpServer,connId,pClient){
	/*握手完成触发*/
}
server.onSend = function(hpTcpServer,connId,pData,length){
	var data = ..raw.tostring(pData,1,length)
	/*数据发送成功后触发*/
}
server.onReceive = function(hpTcpServer,connId,pData,length){
	var data = ..raw.tostring(pData,1,length)
	/*接收到数据时触发*/
	import console;
	console.dump(connId,data)
}
server.onClose = function(hpTcpServer,connId,enOperation,errCode){
	/*连接正常或异常关闭时触发*/
}
server.onShutdown = function(hpTcpServer){
	/*关闭通信组件触发*/
}

winform.button.oncommand = function(id,event){
	
}

winform.show();
win.loopMessage();


import win.ui;
/*DSG{{*/
var winform = win.form(text="aardio form";right=759;bottom=469)
winform.add(
button={cls="button";text="Button";left=112;top=72;right=224;bottom=112;z=1}
)
/*}}*/

import hpsocket.tcpClient;
var client = hpsocket.tcpClient()

client.start("192.168.1.143",6003,true);



client.onThreadCreated = function(){
	/*监听线程首次回调前触发*/
	import console; 
	..console.log("连接成功")
}

client.onThreadEnd = function(){
	/*监听线程退出前触发*/
	import console; 
	..console.log("连接成功")
}

client.onPrepareConnect = function(hpTcpClient,connId,soListen){
	/*准备建立连接前触发*/
	import console; 
	..console.log("连接成功")
}

client.onHandShake = function(hpTcpClient,connId){
	/*握手完成触发*/
	import console; 
	..console.log("连接成功")
}

client.onSend = function(hpTcpClient,connId,pData,length){
	var data = ..raw.tostring(pData,1,length)
	/*数据发送成功后触发*/
	
}

client.onConnect = function(hpTcpClient,connId){
	/*建立连接后触发*/
}

client.onReceive = function(hpTcpClient,connId,pData,length){
	var data = ..raw.tostring(pData,1,length)
	import console;
	..console.dump(data)
	/*接收到数据时触发*/
}

client.onClose = function(hpTcpClient,connId,enOperation,errCode){
	/*连接正常或异常关闭时触发*/
}

client.onShutdown = function(hpTcpClient){
	/*关闭通信组件触发*/
}


winform.button.oncommand = function(id,event){
	client.send(raw.buffer("123"))
}

winform.show();
win.loopMessage();


上传的附件:
最新回复 (6)
  • mndsoft 3月前
    0 2

    您好,老师,上线能否指导下:

    hpsocket 是个不错的网络库,虽然官方已经废弃,但是这个库功能还是强大的,只用到TCP/UDP通讯的程序还是很简洁适用。

    我看您封装的这些库函数(应该叫工作线程吧?我理解为触发事件),如何把在工作线程中信息显示到界面中? 最近学习线程,有点难哈

    请写个示范,谢谢

    比如:

    client.onConnect = function(hpTcpClient,connId){
    /*建立连接后触发*/
    winform.edit.text ="建立连接后触发";
    }
    
    client.onReceive = function(hpTcpClient,connId,pData,length){
    var data = ..raw.tostring(pData,1,length)
    //import console;
    //..console.dump(data)
    /*接收到数据时触发*/
    winform.edit.text ="接收:" ++ string.hex(data," ")     //在工作线程里把收到的数据如何显示到文本框里?
    }


  • mndsoft 3月前
    0 3

    问了AI 回调函数中如何把数据显示到界面中,测试好像没效果,继续努力。

    import thread.command;
    
    var listener = thread.command();
    listener.updateUI = function(data){
        winform.edit.print(data)
    }
    
    client.onReceive = function(hpTcpClient,connId,pData,length){
        var data = ..raw.tostring(pData,1,length)
        thread.command.updateUI(data)
    }


  • mndsoft 3月前
    0 4

    搞定,贴出来共享参考学习。

    主要学习了回调函数与界面主线程交互的方法。 有更好的方法,欢迎大佬贴一下学习哈


    以下是代码:

    import win.ui;
    import hpsocket.tcpClient;
    import enet.richeditEx;
    /*DSG{{*/
    var winform = win.form(text="HpSocket TCP Client";right=759;bottom=474)
    winform.add(
    button={cls="button";text="发送";left=627;top=410;right=739;bottom=450;db=1;dr=1;z=1};
    txtLog={cls="richedit";left=9;top=13;right=748;bottom=387;bgcolor=15793151;color=12632256;db=1;dl=1;dr=1;dt=1;edge=1;font=LOGFONT(h=-13;name='微软雅黑');multiline=1;readonly=1;vscroll=1;wrap=1;z=2};
    txtSend={cls="edit";text="55 AA AA AA AA AA 00 01 2F D7 16";left=19;top=415;right=593;bottom=446;db=1;dl=1;dr=1;edge=1;multiline=1;z=3}
    )
    /*}}*/
    
    //log信息
    var  r = enet.richeditEx(winform.txtLog)
    //global.lastConnId = null;
    
    import thread.command;
    
    //刷新界面
    thread.command.instance().updateStatic = function(id,str){
        r.log(0,id ++ str ,0xFFDD254C);    //前景色蓝色,背景色
    }
    
    
    var client = hpsocket.tcpClient()
    
    client.start("192.168.1.11",8891,true);
    
    client.onThreadCreated = function(){
        /*监听线程首次回调前触发*/
        import thread.command;
        thread.command.post("updateStatic","onThreadCreated","监听线程首次回调前触发");
    }
    
    client.onThreadEnd = function(){
        /*监听线程退出前触发*/
        import thread.command;
        thread.command.post("updateStatic","onThreadEnd","监听线程退出前触发");
    }
    
    client.onPrepareConnect = function(hpTcpClient,connId,soListen){
        /*准备建立连接前触发*/
        import thread.command;
        thread.command.post("updateStatic","onPrepareConnect","准备建立连接前触发");
    }
    
    client.onHandShake = function(hpTcpClient,connId){
        /*握手完成触发*/
        import thread.command;
        thread.command.post("updateStatic","onHandShake","握手完成触发");
    }
    
    client.onSend = function(hpTcpClient,connId,pData,length){
        var data = ..raw.tostring(pData,1,length)
        /*数据发送成功后触发*/
        import thread.command;
        thread.command.post("updateStatic","onSend","数据发送成功后触发,发送数据:" ++ string.hex(data," "));
       
    }
    
    
    client.onConnect = function(hpTcpClient,connId){
        /*建立连接后触发*/
        import thread.command;
        thread.command.post("updateStatic","onConnect","连接ID:" ++ tostring(connId) ++ " " ++ table.tostring(hpTcpClient));   
    }    
    
    
    client.onReceive = function(hpTcpClient,connId,pData,length){
        /*接收到数据时触发*/
        var data = ..raw.tostring(pData,1,length)
        import thread.command;
        thread.command.post("updateStatic","onReceive","接收:" ++ string.hex(data," ")); 
    
    }
    
    client.onClose = function(hpTcpClient,connId,enOperation,errCode){
        /*连接正常或异常关闭时触发*/
        import thread.command;
        thread.command.post("updateStatic","onClose","关闭ID:" ++ tostring(connId) ++ " " ++ table.tostring(hpTcpClient)); 
    
    }
    
    winform.button.oncommand = function(id,event){
          //client.send(raw.buffer("123"))
          client.send(string.unhex("55 AA AA AA AA AA 00 01 2F D7 16", " "))
    }
    
    
    
    winform.show();
    win.loopMessage();


  • 光庆 3月前
    0 5

  • tanzhwen 3月前
    0 6
    hpsocdet 库在哪里下?
  • tanzhwen 3月前
    0 7

    上传的附件:
返回