C#利用VUDP.cs开发网络通讯应用例程

VClassLib-CS项目Github地址:https://github.com/velscode/VClassLib-CS
VUDP文档地址:https://github.com/velscode/VClassLib-CS/blob/master/VUDP/VUDP_Docs.md

概述

VUDP类可以帮助您快速搭建需要使用UDP协议进行网络通讯的应用

UI设计

实例化

VUDP v = new VUDP();

编写监听线程方法

private void RecvThread()
{ 
    while(true)
    {
        try 
        {             
            byte[] data = v.ReceiveData();
            for(int i =0; i < data.Length;i++)
            {
                //这里将接受到的数据写入textbox,可以替换其它业务代码
                VDelegate.AddTextBoxText(textBox_Receive,data[i].ToString("X2")+" ");
            }
        }
        catch 
        { 
            ;
        }
    }
}

开启监听按钮

private void button_StartRecv_Click(object sender, EventArgs e)
{
    //配置参数
    v.localIP = textBox_LocalIP.Text;  
    v.localport = Convert.ToInt32(textBox_LocalPort.Text);

    v.OpenRecv();

    //启动监听线程
    Thread recvThread = new Thread(RecvThread);
    recvThread.IsBackground = true;
    recvThread.Start();
}

发送按钮

private void button_Send_Click(object sender, EventArgs e)
{
    v.remoteIP = textBox_RemoteIP.Text;
    v.remoteport = Convert.ToInt32(textBox_RemotePort.Text);

    v.SendData(textBox_Send.Text);
}

运行演示

posted @ 2019-02-07 19:07  Velscode  阅读(492)  评论(0编辑  收藏  举报