源码贴出来了,高人看看哪里出问题了,同样的代码如果服务端是放在控制台或是webform或winform程序中都是可以的,但是在windows service中却不行 求指教!测试代码 问题很多 多多谅解
客户端源码:控制台程序
public void Main()
{
try
{
System.Net.Sockets.UdpClient udpClient = new System.Net.Sockets.UdpClient(11000);
//向服务器发送数据
udpClient.Connect("192.168.1.24", 12000);
string sendStr = "我来自客户端:" + DateTime.Now.ToString();
Byte[] sendBytes = Encoding.UTF8.GetBytes(sendStr);
udpClient.Send(sendBytes, sendBytes.Length);
Console.WriteLine("This is the message client send: " + sendStr);
//等待服务器的答复,收到后显示答复,并结束对话
IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
string str = RemoteIpEndPoint.Address.ToString();
string str1 = RemoteIpEndPoint.Port.ToString();
// 此处通过引用传值,获得客户端的IP地址及端口号
Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint);
//此处获得服务器端的数据
string returnData = Encoding.UTF8.GetString(receiveBytes);
//Encoding.ASCII.GetString(receiveBytes); 此处若用ASCII,不能正确处理中文
Console.WriteLine("This is the message come from server: " + returnData.ToString());
udpClient.Close();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}服务端:windows service中
public void Main()
{
UdpClient udpClient = new UdpClient(12000);
string returnData = "client_end";
do
{
IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
// 此处通过引用传值,获得客户端的IP地址及端口号
Byte[] receiveBytes;
try
{
receiveBytes = udpClient.Receive(ref RemoteIpEndPoint);
//此处获得客户端的数据
returnData = Encoding.UTF8.GetString(receiveBytes);
}
catch (Exception ep)
{
EventLogError(ep);
}
Thread.Sleep(3000);
udpClient.Connect("192.168.1.33", 11000);
string sendStr = "服务器时间:" + DateTime.Now.ToString();
Byte[] sendBytes = Encoding.UTF8.GetBytes(sendStr);
udpClient.Send(sendBytes, sendBytes.Length);
} while (returnData != "client_end"); }
public void EventLogError(Exception ex)
{
FileStream fs = new FileStream("D:\\log.txt", FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter m_streamWriter = new StreamWriter(fs);
m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
m_streamWriter.WriteLine("--------出现异常--------" + DateTime.Now.ToString() + "\n");
m_streamWriter.WriteLine(ex.ToString() + "\n");
m_streamWriter.Flush();
m_streamWriter.Close();
fs.Close();
}
客户端源码:控制台程序
public void Main()
{
try
{
System.Net.Sockets.UdpClient udpClient = new System.Net.Sockets.UdpClient(11000);
//向服务器发送数据
udpClient.Connect("192.168.1.24", 12000);
string sendStr = "我来自客户端:" + DateTime.Now.ToString();
Byte[] sendBytes = Encoding.UTF8.GetBytes(sendStr);
udpClient.Send(sendBytes, sendBytes.Length);
Console.WriteLine("This is the message client send: " + sendStr);
//等待服务器的答复,收到后显示答复,并结束对话
IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
string str = RemoteIpEndPoint.Address.ToString();
string str1 = RemoteIpEndPoint.Port.ToString();
// 此处通过引用传值,获得客户端的IP地址及端口号
Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint);
//此处获得服务器端的数据
string returnData = Encoding.UTF8.GetString(receiveBytes);
//Encoding.ASCII.GetString(receiveBytes); 此处若用ASCII,不能正确处理中文
Console.WriteLine("This is the message come from server: " + returnData.ToString());
udpClient.Close();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}服务端:windows service中
public void Main()
{
UdpClient udpClient = new UdpClient(12000);
string returnData = "client_end";
do
{
IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
// 此处通过引用传值,获得客户端的IP地址及端口号
Byte[] receiveBytes;
try
{
receiveBytes = udpClient.Receive(ref RemoteIpEndPoint);
//此处获得客户端的数据
returnData = Encoding.UTF8.GetString(receiveBytes);
}
catch (Exception ep)
{
EventLogError(ep);
}
Thread.Sleep(3000);
udpClient.Connect("192.168.1.33", 11000);
string sendStr = "服务器时间:" + DateTime.Now.ToString();
Byte[] sendBytes = Encoding.UTF8.GetBytes(sendStr);
udpClient.Send(sendBytes, sendBytes.Length);
} while (returnData != "client_end"); }
public void EventLogError(Exception ex)
{
FileStream fs = new FileStream("D:\\log.txt", FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter m_streamWriter = new StreamWriter(fs);
m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
m_streamWriter.WriteLine("--------出现异常--------" + DateTime.Now.ToString() + "\n");
m_streamWriter.WriteLine(ex.ToString() + "\n");
m_streamWriter.Flush();
m_streamWriter.Close();
fs.Close();
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货