服务器端
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);
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);
解决方案 »
- 一次执行多个线程,如何知道每个线程什么时候结束?
- 请教怎么把科学计数法转换成decimal型,我的科学计数法在库中是varchar类型保存
- 为什么在线程中无法获取复制板上的数据
- (c#)xp下屏蔽ctrl+alt+del
- 为什么我点击这个BUTTON,程序就没响应了
- 用WebRequest创建连接怎么通过代理身份验证?
- 连接远程服务器上的SQL SERVER 可以直接这样连吗?
- 如题如何把DataTable强制转换成SqlDataAdapter
- 使用JMAIL接受邮件的问题
- Excel操作问题????
- WebMIS.net操作大全之一:WebTree功能分析
- 如何使用WinForm实现上载整个文件夹
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;
}
your means client machine set timeserver is this server that installed SimpleService?
谢谢!!谢谢!! wangsaokui(无间道II(前传)) 兄的一看就清楚明白。 redbb兄的的确看不清楚看不明白呀!
能否解释一下?
我明天去试,今天我在休息!
呵---呵----