2 u010867034 u010867034 于 2013.10.08 10:30 提问

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个回答

aasswwe
aasswwe   2013.10.09 08:54

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

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

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!