服务器端
Socket sServer= new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
//设置服务器端的端口号
IPEndPoint ServerEP = new IPEndPoint(MyIP, 2001);
//设置客户端的端口号
IPEndPoint ClientEP = new IPEndPoint(MyIP, 2002);
//将Socket与端口绑定
sServer.Bind(ServerEP);
While (True)
{
Dim bytes(100) As Byte
//等待客户端的请求
sServer.ReceiveFrom(bytes, ClientEP);string str = System.Now().Tostring("yyyy-MM-dd HH:mm:ss:fff");
bytes = System.Text.Encoding.ASCII.GetBytes(str);
//返回结果
sServer.SendTo(bytes, ClientEP);
}客户端
Socket cClient = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
//设置服务器端的端口号
IPEndPoint ServerEP = new IPEndPoint(MyIP, 2001);
//设置客户端的端口号
IPEndPoint ClientEP = new IPEndPoint(MyIP, 2002);
//将Socket与端口绑定
cClient.Bind(ClientEP);
string str;
bytes[] Byte = System.Text.Encoding.ASCII.GetBytes(str);
//向服务器发出请求
cClient.SendTo(bytes, ServerEP);
Byte [] bytes2= new Byte[100];
//接收服务器的响应结果
cClient.ReceiveFrom(bytes2, ServerEP)
str = System.Text.Encoding.ASCII.GetString(bytes2);
//此str就是返回的时间
//用这个类修改时间
public class Cls_ChangeDT
{
//this function is used for get local machine time
[DllImport("Kernel32.dll")]
public static extern void GetLocalTime(SystemTime st);
public static string abc;
//this function is used for set local machine time
[DllImport("Kernel32.dll")]
public static extern void SetLocalTime(SystemTime st); //this structure define a systemtime
[StructLayout(LayoutKind.Sequential)]
public class SystemTime
{
public ushort wYear;
public ushort wMonth;
public ushort wDayOfWeek;
public ushort wDay;
public ushort wHour;
public ushort wMinute;
public ushort wSecond;
public ushort wMilliseconds;
}
}
调用的时候
Cls_ChangeDT.SystemTime st= new Cls_ChangeDT.SystemTime();
st.wYear=(ushort)此值由上面字符串分割获取;
st.wMonth=(ushort)此值由上面字符串分割获取;
st.wDay=(ushort)此值由上面字符串分割获取;
st.wHour=(ushort)此值由上面字符串分割获取;
st.wMinute =(ushort)此值由上面字符串分割获取;
st.wSecond =(ushort)此值由上面字符串分割获取;
st.wMilliseconds=(ushort)此值由上面字符串分割获取;Cls_ChangeDT.SetLocalTime(st);

解决方案 »

  1.   

    Dim bytes(100) As Byte=================>Byte [] bytes = new Byte[100];
      

  2.   

    public static void Main() {
            ServiceBase.Run(new SimpleService());
        }    public SimpleService()
        {
            CanPauseAndContinue = true;
            ServiceName = "服务";        timer = new Timer();
            timer.Interval = 1000;
            timer.Elapsed += new ElapsedEventHandler(OnTimer);
        }    protected override void OnStart(string[] args)
        {
            EventLog.WriteEntry("服务已启动");
            timer.Enabled = true;
        }
      

  3.   

    To: redbb
    your means client machine set timeserver is this server that installed SimpleService?
      

  4.   


     谢谢!!谢谢!!     wangsaokui(无间道II(前传)) 兄的一看就清楚明白。      redbb兄的的确看不清楚看不明白呀!
          
           能否解释一下?
          
         我明天去试,今天我在休息!
                      呵---呵----