沐浴重生 2013-10-08 02:30 采纳率: 0%
浏览 2468

C # UDPClient 通讯连续发送消息

如何实现UDP通讯时的连续发送消息,目前我只实现发送一条消息就不能再发送,在网上搜找后说要使用多线程,但是我不会啊,请求高手指导》
Server:
private void btRec_Click(object sender, EventArgs e)
{
//在本機指定的端口接收
IPEndPoint remoteIpEndIPoint = new IPEndPoint(IPAddress.Any, 8002);
udpReceive = new UdpClient(remoteIpEndIPoint);
IPEndPoint iep = new IPEndPoint(IPAddress.Any, 0);
//接收從遠程主機發送過來的消息

//while (true)
//{
//ref表示引用類型 IPPoint實例接收消息

byte[] receiveBytes = udpReceive.Receive(ref iep);
string returnData = Encoding.UTF8.GetString(receiveBytes);
txtSerRev.Text = returnData;
string strBack = "I am fine. Your IP address is " +iep.ToString();
byte[] data = Encoding.ASCII.GetBytes(strBack);
udpReceive.Send(data,data.Length,iep);

       //}
    }

CLient :
private void btSendMsg_Click(object sender, EventArgs e)
{
//初始化UdpClient

udpSend = new UdpClient();
//實際使用時應將192.168.80.128該為服務器的遠程IP

IPAddress remoteIPAddress = IPAddress.Parse(txtServerIP.Text);

IPEndPoint remoteIPEndPoint = new IPEndPoint(remoteIPAddress, 8002);

//將發送內容轉換為字節數組

byte[] bytes = System.Text.Encoding.UTF8.GetBytes(txtSend.Text);

          udpSend.Send(bytes, bytes.Length, remoteIPEndPoint);


          bytes = udpSend.Receive(ref remoteIPEndPoint);


          string str = Encoding.UTF8.GetString(bytes, 0, bytes.Length);
          txtRec.Text = str;

    }
  • 写回答

1条回答

  • Ganker 2013-10-09 00:54
    关注

    http://download.csdn.net/detail/aasswwe/3887169

    你看看吧,或许对你有帮助

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog