我也在关注这个问题:我写了一段代码,但不是很好,仅供参考:
[Server][Client]
using System.Collections ;
using System.ComponentModel ;
using System.Windows.Forms ;
using System.Data ;
using System.Net ;
using System;
using System.Xml;
using System.IO;
using System.Text;
using System.Windows;
using System.Net.Sockets ;
using System.Runtime.InteropServices ;
//程序引入WinAPI函数要使用到
namespace UDPClient
{
public class Form1 : System.Windows.Forms.Form
{ private UdpClient client ;
//创建UDP网络服务
private IPEndPoint receivePoint ;
private int port = 9000 ; 
//定义接收服务器端程序发送对时信息对应的端口号
private string timeString = DateTime.Now.ToString  ( ) ;
//存放时间日期信息字符串
private System.DateTime  temp ;
private System.Windows.Forms.RichTextBox RbMessage;
//定义一个时间类型,用以修改当前时间和日期
private System.ComponentModel.Container components = null; public Form1()
{
InitializeComponent();
} protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null) 
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows Form Designer generated code
[STAThread] static void Main() 
{
Application.Run(new Form1());
} private void button1_Click ( object sender , System.EventArgs e )
{
StartClient() ;
} private void StartClient()
{
client = new UdpClient ( port ) ;
IPAddress a = IPAddress.Parse  ( "127001" ) ;
receivePoint = new IPEndPoint ( a  , port ) ;
IPAddress HostIP ;
bool continueLoop = true ;
while ( continueLoop )
{
string hostName = Dns.GetHostName ( ) ;
System.Text.ASCIIEncoding encode = new System.Text.ASCIIEncoding ( ) ;
//定义发送到服务器端的请求信息
//请求信息是一个字符串,为客户端名称和接收服务器反馈信息的端口号组成的字符串
string TargetStr = "";
StringToSend(out TargetStr);
string sendString = hostName + "/" + port.ToString ( ) ; byte[] sendData = encode.GetBytes ( sendString ) ;
//判断使用者输入的是IP地址还是计算机名称
try
{
HostIP = IPAddress.Parse  ( textBox3.Text ) ;
}
catch
{
//如果输入的是计算机名称,则按照执行下列代码。
//发送请求信息
//client.Send ( sendData , sendData.Length , textBox3.Text , port ) ;
client.Send ( encode.GetBytes(TargetStr), TargetStr.Length , textBox3.Text , port ) ;
//接收来自服务器端的信息
byte[] recData = client.Receive ( ref receivePoint ) ;
timeString = encode.GetString ( recData ) ;
client.Close ( ) ;
continueLoop=false ;
return ;
}
//输入的是IP地址,则执行下列代码
IPEndPoint host = new IPEndPoint ( HostIP ,port ) ;
//发送请求信息
//client.Send ( sendData , sendData.Length , host ) ;
client.Send ( encode.GetBytes(TargetStr), TargetStr.Length , textBox3.Text , port ) ; //接收来自服务器端的信息
byte[] recData1 = client.Receive ( ref receivePoint ) ;
//获取服务器端的时间和日期
timeString = encode.GetString ( recData1 ) ;
client.Close ( ) ;
//退出循环
continueLoop=false ;
}
} private void button2_Click ( object sender , System.EventArgs e )
{
StartClient ( ) ;
//把接收来的数据转换时间日期格式
try
{
temp = DateTime.Parse  ( timeString ) ;
}
catch
{
MessageBox.Show  ( "错误时间" ) ;
return ;
}
//根据得到的时间日期,来定义时间、日期
SystemTime st= new SystemTime ( ) ;
st.year=  ( short )temp.Year ;
st.Month= ( short )temp.Month ;
st.DayOfWeek= ( short )temp.DayOfWeek ;
st.Day= ( short )temp.Day ;
st.Hour=Convert.ToInt16 ( temp.Hour ) ;
if ( st.Hour>=12 )
{
st.Hour-= ( short )8 ;
}
else if ( st.Hour >= 8 )
{
st.Hour-= ( short )8 ;
}
else
{
st.Hour+= ( short )16 ;
}
st.Minute=Convert.ToInt16 ( temp.Minute ) ;
st.Second=Convert.ToInt16 ( temp.Second ) ;
st.Milliseconds=Convert.ToInt16 ( temp.Millisecond ) ;
//修改本地端的时间和日期
if ( SetSystemTime ( st ) )
{
MessageBox.Show ( DateTime.Now.ToString ( ) ,"修改成功" ) ;

else
MessageBox.Show ( "不成功!" ,"不成功" ) ;
} private void StringToSend(out string TargetStr)
{
TargetStr ="";
OpenFileDialog MyDlg = new OpenFileDialog();
try
{
if ( MyDlg.ShowDialog ( ) == DialogResult.OK )
{
FileStream fs = new FileStream ( MyDlg.FileName  , FileMode.Open , FileAccess.Read ) ;
TargetStr = fs.ToString();
StreamReader m_streamReader = new StreamReader ( fs,System.Text.Encoding.GetEncoding("GB2312")) ; //使用StreamReader类来读取文件
m_streamReader.BaseStream.Seek ( 0 , SeekOrigin.Begin ) ;// 从数据流中读取每一行,直到文件的最后一行,并在richTextBox1中显示出内容
RbMessage.Text = "" ;
string strLine = m_streamReader.ReadLine ( ) ;
while ( strLine != null )
{
RbMessage.Text += strLine + "\r\n" ;
strLine = m_streamReader.ReadLine ( ) ;
}
//关闭此StreamReader对象
m_streamReader.Close ( ) ;
TargetStr = RbMessage.Text;

}
catch ( Exception em )
{
MessageBox.Show ( em.Message.ToString ( ) ) ;
}
} private void StringToSave()
{
SaveFileDialog MyDlg = new SaveFileDialog();
try
{
//获得另存为的文件名称
if ( MyDlg.ShowDialog ( ) == DialogResult.OK )

//创建一个文件流,用以写入或者创建一个StreamWriter
FileStream fs = new FileStream ( @MyDlg.FileName  , FileMode.OpenOrCreate , FileAccess.Write ) ;
StreamWriter m_streamWriter = new StreamWriter ( fs ) ;
m_streamWriter.Flush ( ) ; // 使用StreamWriter来往文件中写入内容
m_streamWriter.BaseStream.Seek ( 0 , SeekOrigin.Begin ) ; // 把richTextBox1中的内容写入文件
m_streamWriter.Write ( RbMessage.Text ) ; //关闭此文件
m_streamWriter.Flush ( ) ;
m_streamWriter.Close ( ) ;
}
}
catch ( Exception em )
{
Console.WriteLine ( em.Message.ToString ( ) ) ;
} }