夜夜夜的夏洛克 2015-10-08 13:55 采纳率: 50%
浏览 1395
已结题

请大神帮我看下这部分代码怎么写?谢谢!很急

如图所示,我把基本通讯功能已经实现,但是我想实现下图中红色圈中部分的功能,自动发送跟数据格式的功能怎么实现啊,希望发送端跟接收端都可以实现这个数据格式的功能,谢谢图片说明
整体代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//添加的命名空间引用
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace UDP_扩展开发
{
public partial class UDPTOOL : Form
{
delegate void AddListBoxItemCallback(string text);
AddListBoxItemCallback listBoxCallback;
//使用的接收端口号
private int port = 8001;
private UdpClient udpClient;
public UDPTOOL()
{
InitializeComponent();
listBoxCallback = new AddListBoxItemCallback(AddListBoxItem);
}

    private void AddListBoxItem(string text)
    {
        //如果listBoxReceive被不同的线程访问则通过委托处理;
        if (listBoxReceive.InvokeRequired)
        {
            this.Invoke(listBoxCallback, text);
        }
        else
        {
            listBoxReceive.Items.Add(text);
            listBoxReceive.SelectedIndex = listBoxReceive.Items.Count - 1;
        }
    }
    /// <summary>
    /// 在后台运行的接收线程
    /// </summary>
    private void ReceiveData()
    {
        //在本机指定的端口接收
        udpClient = new UdpClient(port);
        IPEndPoint remote = null;
        //接收从远程主机发送过来的信息;
        while (true)
        {
            try
            {
                //关闭udpClient时此句会产生异常
                byte[] bytes = udpClient.Receive(ref remote);
                string str = Encoding.UTF8.GetString(bytes, 0, bytes.Length);
                AddListBoxItem(string.Format("来自{0}:{1}", remote, str));
            }
            catch
            {
                //退出循环,结束线程
                break;
            }
        }
    }
    /// <summary>
    /// 发送数据到远程主机
    /// </summary>
    private void sendData()
    {
        UdpClient myUdpClient = new UdpClient();
        IPAddress remoteIP;
        if (IPAddress.TryParse(textBoxRemoteIP.Text, out remoteIP) == false)
        {
            MessageBox.Show("远程IP格式不正确");
            return;
        }
        IPEndPoint iep = new IPEndPoint(remoteIP, port);
        byte[] bytes = System.Text.Encoding.UTF8.GetBytes(textBoxSend.Text);
        try
        {
            myUdpClient.Send(bytes, bytes.Length, iep);
            myUdpClient.Close();
            textBoxSend.Focus();
        }
        catch (Exception err)
        {
            MessageBox.Show(err.Message, "发送失败");
        }
        finally
        {
            myUdpClient.Close();
        }
    }

    private void UDPTOOL_Load(object sender, System.EventArgs e)
    {
        //设置listBox样式
        listBoxReceive.HorizontalScrollbar = true;
        listBoxReceive.Dock = DockStyle.Fill;
        //获取本机第一个可用IP地址
        IPAddress myIP = (IPAddress)Dns.GetHostAddresses(Dns.GetHostName()).GetValue(0);
        //为了在同一台机器调试,此IP也作为默认远程IP
        textBoxRemoteIP.Text = myIP.ToString();
        //创建一个线程接收远程主机发来的信息
        Thread myThread = new Thread(new ThreadStart(ReceiveData));
        //将线程设为后台运行
        myThread.IsBackground = true;
        myThread.Start();
        textBoxSend.Focus();
    }
     /// <summary>
    /// 单击发送按钮触发的事件
    /// </summary>
    private void buttonSend_Click(object sender, System.EventArgs e)
    {
        {
            sendData();
        }
    }
    /// <summary>
    /// 在textBoxSend中按下并释放Enter键后触发的事件
    /// </summary>
    private void textBoxData_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == (char)Keys.Enter)
            sendData();
    }

    private void UDPTOOL_FormClosing(object sender, FormClosingEventArgs e)
    {
         udpClient.Close();
    }

}

}

如果有大神愿意帮忙可留下自己的qq,我会及时主动加上的,谢谢!
  • 写回答

1条回答 默认 最新

  • threenewbee 2015-10-08 14:11
    关注

    System.Windows.Forms.Timer t = new System.Windows.Forms.Timer(this.components);
    t.Interval = int.Parse(间隔文本框.Text) * 1000;
    t.Enabled = true;
    t.Tick +=new System.EventHandler((a, b) => { sendData(); });

    评论

报告相同问题?

悬赏问题

  • ¥50 comsol稳态求解器 找不到解,奇异矩阵有1个空方程返回的解不收敛。没有返回所有参数步长;pid控制
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功