udpclient类的应用实例
using System;using
using
using System.Text;
namespace UdpClientServer
{
class class1
{
static void Main()
{
StarListener();
Console.ReadLine();
}
private static void StarListener()
{
UdpClient udpServer=new UdpClient(8000);
IPEndPoint myHost=null;
{
while(true)
{
try
Console.WriteLine("等待接受...");
byte[]getBytes=udpServer.Receive(ref myHost);
string getString=Encoding.Unicode.GetString(getBytes,0,getBytes.Length);
Console.WriteLine("接受信息:{0}",getString);
if(getString=="quite")break;
string sendString="你好,多保重!";
Console,WriteLine("发送消息:{0}",sendString);
byte[]sendBytes=Encoding.Unicode.GetBytes(sendString);
udpServer.Send(sendBytes,sendBytes.Length,"127.0.0.1",8001);
}
udpServer.Close();
Console.WriteLine("对方已经退出,请按回车键退出。");
}
catch(Exception err)
{
Console.WriteLine(err.ToString());
}
}
}
}