风铃x 2023-02-23 10:55 采纳率: 0%
浏览 31

c# 上位机网络通信,多线程?

请教:用C#做网络通信的上位机。要实现与多个IP连接(即同时连接多台待测设备)并分别处理数据,得用多线程异步通信吗?

  • 写回答

1条回答 默认 最新

  • muyiliu 2023-02-23 11:02
    关注
    public partial class Form1 : Form
    {
    UdpClient udpClient;
    Thread UdpThread;
    delegate void updateUI(string msg);
    updateUI AddMsg2LogDelegate;
    public Form1()
    {
    InitializeComponent();
    AddMsg2LogDelegate = new updateUI(AddMsg2Log);
    }
    
    private void btnstartservice_Click(object sender, EventArgs e)
    {
    StartServer();
    }
    private void StartServer()
    {
    if (udpClient != null)
    {
    UdpThread.Abort();
    Thread.Sleep(TimeSpan.FromMilliseconds(500d));
    udpClient.Close();
    }
    try
    {
    udpClient = new UdpClient(int.Parse(textBoxPort.Text));
    UdpThread = new Thread(new ThreadStart(UdpReciveThread));
    UdpThread.Start();
    //buttonStartServer.Enabled = false;
    }
    catch (Exception y)
    {
    MessageBox.Show(this, y.Message, "Error", MessageBoxButtons.OK,
    MessageBoxIcon.Error);
    }
    }
    void UdpReciveThread()
    {
    IPEndPoint remoteHost = null;
    this.Invoke(AddMsg2LogDelegate, "启动...");
    //listBox1.Items.Add("启动...");
    while (udpClient != null && Thread.CurrentThread.ThreadState == ThreadState.Running)
    {
    try
    {
    this.Invoke(AddMsg2LogDelegate, "等待连接...");
    
    byte[] buf = udpClient.Receive(ref remoteHost);
    string bufs = Encoding.UTF8.GetString(buf);
    string s = string.Format("主机:{0}; 端口:{1}; 数据报长度:{2}; ",remoteHost.Address, remoteHost.Port, buf.Length);
    this.BeginInvoke(AddMsg2LogDelegate, s);
    this.BeginInvoke(AddMsg2LogDelegate, bufs);
    }
    catch (Exception y)
    {
    this.Invoke(AddMsg2LogDelegate, y.Message);
    }
    }
    this.Invoke(AddMsg2LogDelegate, "结束...");
    }
    
    public void AddMsg2Log(string message)
    {
    listBoxLog.Items.Add(message);
    }
    
    private void Form1_FormClosed(object sender, FormClosedEventArgs e)
    {
    System.Environment.Exit(0);
    }
    
    private void btnSendMessage_Click(object sender, EventArgs e)
    {
    try
    {
    UdpClient uc = new UdpClient(textBoxHost.Text,int.Parse(textBoxPort.Text));
    byte[] sendbuf = Encoding.UTF8.GetBytes(textBoxMessage.Text);
    uc.Send(sendbuf, sendbuf.Length);
    }
    catch (Exception y)
    {
    MessageBox.Show(this, y.Message, "发送失败", MessageBoxButtons.OK,MessageBoxIcon.Hand);
    }
    }
    
    private void button1_Click(object sender, EventArgs e)
    {
    this.listBoxLog.Items.Clear();
    }
    }
    
    

    示例代码,仅供参考!

    img

    评论

报告相同问题?

问题事件

  • 创建了问题 2月23日

悬赏问题

  • ¥15 模电中二极管,三极管和电容的应用
  • ¥15 关于模型导入UNITY的.FBX: Check external application preferences.警告。
  • ¥15 气象网格数据与卫星轨道数据如何匹配
  • ¥100 java ee ssm项目 悬赏,感兴趣直接联系我
  • ¥15 微软账户问题不小心注销了好像
  • ¥15 x264库中预测模式字IPM、运动向量差MVD、量化后的DCT系数的位置
  • ¥15 curl 命令调用正常,程序调用报 java.net.ConnectException: connection refused
  • ¥20 关于web前端如何播放二次加密m3u8视频的问题
  • ¥15 使用百度地图api 位置函数报错?
  • ¥15 metamask如何添加TRON自定义网络