帮忙啊!
实际描述:
有一群设备通过udp和server通讯。服务器只能使用一个端口。设备的Ip,port都是动态的。
设备同server之间通讯无特殊要求,但是server向设备通讯有一些要求:
1. 命名的发送顺序有要求
2. 命令的发送不能够跳跃。
3. 设备可能不能接收到消息
我的想法:1.在一个线程中异步接收消息
2.发送消息时,对每一个(IP,Port),启动一个线程发送消息。在这个线程里面来顺序发送消息问题:
如果没有发送消息,那么接受线程正常运行
但是在发送消息的线程启动后,接受消息的端口便被强行关闭
{"An existing connection was forcibly closed by the remote host"}
如何才能够解决这个问题呢,请指教!要是能够用其他的办法达到需求也可以哈。代码如下:
/////////////////////////////////
Start()
{
mUdpClient = new UdpClient(mPort);
UdpState s = new UdpState();
s.e = LocalIpEndPoint;
s.u = mUdpClient;
// 异步接收消息
mUdpClient.BeginReceive(new AsyncCallback(ReceiveCallback), s);
//启动一个线程发送消息
Thread SendMessageThread = new Thread(new ParameterizedThreadStart(Send));
SendMessageThread.Start(s); while (!messageReceived)
{
Thread.Sleep(100);
}
}
public void ReceiveCallback(IAsyncResult ar)
{
UdpClient u = (UdpClient)((UdpState)(ar.AsyncState)).u;
IPEndPoint e = (IPEndPoint)((UdpState)(ar.AsyncState)).e; // Get current connection's information
Byte[] receiveBytes = u.EndReceive(ar, ref e); // Process receiveBytes
ProcessReceiveBytes(receiveBytes);UdpState s = new UdpState();
s.e = e;
s.u = u
mUdpClient.BeginReceive(new AsyncCallback(ReceiveCallback), s);
}private void Send(object e)
{
while (true)
{
// Get msgs
List<SendMsg> msg = mServer.GetOrderedSendMsg();
// Group the messages of (IP,Port) pair List<SendMsg> GroupMsg = mServer.GetGroupMsgByAddress(msg);
while (GroupMsg != null)
{
helper = new Helper((UdpClient)((UdpState)(e)).u, GroupMsg);
Thread send = new Thread(new ParameterizedThreadStart(SendMessages));
send.Start(helper);
mServer.GetGroupMsgByAddress(ref msg);
}
}
}
private void SendMessages(object Args)
{
// Get the helper
Helper helper = (Helper)Args;
List<SendMsg> sendmsg = helper.msg;
");
for (int i = 0; i < sendmsg.Count; i++)
{
// Send a message
timeOut = SendMessage(sendmsg[i], helper.Client);
// If timeout then cancel all message
if (timeOut == true)
{
break;
}
}
}private bool SendMessage(SendMsg sendmsg, UdpClient client)
{
bool timeOut = true;
UdpClient c = new UdpClient();
IPEndPoint RcvEP = sendmsg.Address; byte[] toSend = GetSendBytes(sendmsg);
mUdpClient.Send(toSend, toSend.Length, RcvEP);
// Wait the response from (ip,port) or it will time out
timeOut = WaitResponse(sendmsg);
if (timeOut)
{
ProcessTimeOut(sendmsg.IMEI);
}
return timeOut;
}
/////////////////////////////////
实际描述:
有一群设备通过udp和server通讯。服务器只能使用一个端口。设备的Ip,port都是动态的。
设备同server之间通讯无特殊要求,但是server向设备通讯有一些要求:
1. 命名的发送顺序有要求
2. 命令的发送不能够跳跃。
3. 设备可能不能接收到消息
我的想法:1.在一个线程中异步接收消息
2.发送消息时,对每一个(IP,Port),启动一个线程发送消息。在这个线程里面来顺序发送消息问题:
如果没有发送消息,那么接受线程正常运行
但是在发送消息的线程启动后,接受消息的端口便被强行关闭
{"An existing connection was forcibly closed by the remote host"}
如何才能够解决这个问题呢,请指教!要是能够用其他的办法达到需求也可以哈。代码如下:
/////////////////////////////////
Start()
{
mUdpClient = new UdpClient(mPort);
UdpState s = new UdpState();
s.e = LocalIpEndPoint;
s.u = mUdpClient;
// 异步接收消息
mUdpClient.BeginReceive(new AsyncCallback(ReceiveCallback), s);
//启动一个线程发送消息
Thread SendMessageThread = new Thread(new ParameterizedThreadStart(Send));
SendMessageThread.Start(s); while (!messageReceived)
{
Thread.Sleep(100);
}
}
public void ReceiveCallback(IAsyncResult ar)
{
UdpClient u = (UdpClient)((UdpState)(ar.AsyncState)).u;
IPEndPoint e = (IPEndPoint)((UdpState)(ar.AsyncState)).e; // Get current connection's information
Byte[] receiveBytes = u.EndReceive(ar, ref e); // Process receiveBytes
ProcessReceiveBytes(receiveBytes);UdpState s = new UdpState();
s.e = e;
s.u = u
mUdpClient.BeginReceive(new AsyncCallback(ReceiveCallback), s);
}private void Send(object e)
{
while (true)
{
// Get msgs
List<SendMsg> msg = mServer.GetOrderedSendMsg();
// Group the messages of (IP,Port) pair List<SendMsg> GroupMsg = mServer.GetGroupMsgByAddress(msg);
while (GroupMsg != null)
{
helper = new Helper((UdpClient)((UdpState)(e)).u, GroupMsg);
Thread send = new Thread(new ParameterizedThreadStart(SendMessages));
send.Start(helper);
mServer.GetGroupMsgByAddress(ref msg);
}
}
}
private void SendMessages(object Args)
{
// Get the helper
Helper helper = (Helper)Args;
List<SendMsg> sendmsg = helper.msg;
");
for (int i = 0; i < sendmsg.Count; i++)
{
// Send a message
timeOut = SendMessage(sendmsg[i], helper.Client);
// If timeout then cancel all message
if (timeOut == true)
{
break;
}
}
}private bool SendMessage(SendMsg sendmsg, UdpClient client)
{
bool timeOut = true;
UdpClient c = new UdpClient();
IPEndPoint RcvEP = sendmsg.Address; byte[] toSend = GetSendBytes(sendmsg);
mUdpClient.Send(toSend, toSend.Length, RcvEP);
// Wait the response from (ip,port) or it will time out
timeOut = WaitResponse(sendmsg);
if (timeOut)
{
ProcessTimeOut(sendmsg.IMEI);
}
return timeOut;
}
/////////////////////////////////
http://www.codeproject.com/vb/net/UDP_Send_Receive.asp