为什么我使用异步UDP客户端套接字随机接收UDP套接字应答?

时间:2013-08-25 19:09:45

标签: c# asynchronous udpclient

我有一个代码将XML文件发送到UdpSocket并收到答案。我发送并收到了异步的答案。我的问题是,当我从UDP套接字收到答案时,我无法将其保存到正确的文件中。我尝试了很多东西,但没有任何作用。

简短解释我的代码。我开始在Main()方法中创建3个AsynchronousConnection。从那里调用静态void AsynchronousConnection(object objectFilename)我调用了UdpStartClient方法。

从UdpStartClient方法我发送一个带有UdpSendXmlFile的文件(fileToSend,udpClient,bytes,CallDuration,out threadId);

之后,我在方法UdpReceivedXmlFile(“c:\ Received”+ filename,udpClient,remoteEPReceived,CallDuration,out threadId)的while循环中收到了答案;

我在UdpReceivedXmlFile方法中收到的答案将保存到文件中。我想问题就在这里。我通过AsynchronousConnection发送3个文件,并从UDP套接字收到3个答案,但答案与我发送的文件不匹配。

例如,我发送这3个文件。

MessagingText4000.xml

MessagingText4001.xml

MessagingText8.xml

我随机收到了答案,例如:

File MessagingText4000.xml可以从MessagingText8.xml

获得答案

File MessagingText4001.xml可以从MessagingText4000.xml获得答案

File MessagingText8.xml可以从MessagingText4001.xml获得答案

你能帮助我吗,所以我收到了正确文件的正确答案?

public delegate void AsyncMethodCall(object objectFilename, int callDuration, out int threadId, out string receivedXmlDataFromTNX);

// Program
public static void Main(String[] args)
{
  Thread newThread;
  newThread = new Thread(AsynchronousConnection);
  newThread.Name = "4001";
  newThread.Start("MessagingText4001.xml");

  newThread = new Thread(AsynchronousConnection);
  newThread.Name = "4000";
  newThread.Start("MessagingText4000.xml");

  newThread = new Thread(AsynchronousConnection);
  newThread.Name = "8";
  newThread.Start("MessagingText8.xml");
}


// Asynchronous Connection 
static void AsynchronousConnection(object objectFilename)
{
  int threadId; string receivedXmlData;
  UdpClass udpClass = new UdpClass();
  AsyncMethodCall caller = new AsyncMethodCall(udpClass.UdpStartClient);
  IAsyncResult result = caller.BeginInvoke(objectFilename, 500, out threadId, out receivedXmlData, null, null);
  result.AsyncWaitHandle.WaitOne();
  caller.EndInvoke(out threadId, out receivedXmlData, result);
  result.AsyncWaitHandle.Close();
}


// UdpClient received 
void UdpReceivedXmlFile(object objectFilename, UdpClient udpClient, IPEndPoint remoteEPReceived, int CallDuration, out int threadId)
{
  Thread.Sleep(CallDuration);
  threadId = Thread.CurrentThread.ManagedThreadId;
  try
  {
    // Blocks until a message returns on this socket from a remote host. 
    Byte[] receiveBytes = udpClient.Receive(ref remoteEPReceived);
    File.WriteAllText((string)objectFilename, Encoding.UTF8.GetString(receiveBytes));
  }
  catch (Exception ex)
  {
    Console.WriteLine("Contact webmaster with this error in UdpReceivedXmlFile:\n " + ex.ToString());
  }
}


// UdpClient send 
void UdpSendXmlFile(string fileToSend, UdpClient udpClient, byte[] bytes, int CallDuration, out int threadId)
{
  Thread.Sleep(CallDuration);
  threadId = Thread.CurrentThread.ManagedThreadId;
  try
  {
    // Encode the data string into a byte array 
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load(fileToSend);
    // Load XML fil
    string xmlContent = xmlDoc.OuterXml;
    byte[] msg = Encoding.UTF8.GetBytes(xmlDoc.OuterXml);
    // Send the data through the socket.
    udpClient.Send(msg, msg.Length);
  }
  catch (Exception ex)
  { Console.WriteLine("Contact webmaster with this error in UdpSendXmlFile:\n " + ex.ToString()); 
  }
}


// UdpStart Client
public void UdpStartClient(object objectFilename, int CallDuration, out int threadId, out string receivedXmlData)
{
  string filename = (string)objectFilename;
  receivedXmlData = null; Thread.Sleep(CallDuration);
  threadId = Thread.CurrentThread.ManagedThreadId;
  try
  {
    Console.WriteLine("1: UdpStartClient Async - id: " + threadId + " objectFilename: " + (string)objectFilename);
    fileToSend = fileLocation + filename;
    // Send a file to the UdpSocket 
    UdpSendXmlFile(fileToSend, udpClient, bytes, CallDuration, out threadId);

    TimeSpan maxTime = TimeSpan.FromSeconds(10);
    Stopwatch stopwatch = Stopwatch.StartNew();
    bool stopwatchStop = false;

    while (stopwatch.Elapsed < maxTime && !stopwatchStop)
    {
      // listed on UdpSocket and save to file 
      UdpReceivedXmlFileDirectToFile("c:\\Received" + filename, udpClient, remoteEPReceived, CallDuration, out threadId);
      attributXMLReceived = ReadXmlAttribut("c:\\Received" + filename, CallDuration, out threadId);

      if ((attributXMLReceived == "Status=Pending") || (attributXMLReceived == "Status=Sent"))
      {
        Console.WriteLine("Answer from XMl file:" + attributXMLReceived + " id: " + Thread.CurrentThread.ManagedThreadId + "\n");
      }
      else if (attributXMLReceived == "Status=Delivered")
      {
        Console.WriteLine("Answer from XMl file:" + attributXMLReceived + " id: " + Thread.CurrentThread.ManagedThreadId + "\n");
        stopwatchStop = true;
      }
      if (stopwatch.Elapsed == maxTime)
        Console.WriteLine("Timeout!");
    }
  }
  catch (Exception e)
  {
    Console.WriteLine(e.ToString());
  }
}

// Program public static void Main(String[] args) { Thread newThread; newThread = new Thread(AsynchronousConnection); newThread.Name = "4001"; newThread.Start("MessagingText4001.xml"); newThread = new Thread(AsynchronousConnection); newThread.Name = "4000"; newThread.Start("MessagingText4000.xml"); newThread = new Thread(AsynchronousConnection); newThread.Name = "8"; newThread.Start("MessagingText8.xml"); } // Asynchronous Connection static void AsynchronousConnection(object objectFilename) { int threadId; string receivedXmlData; UdpClass udpClass = new UdpClass(); AsyncMethodCall caller = new AsyncMethodCall(udpClass.UdpStartClient); IAsyncResult result = caller.BeginInvoke(objectFilename, 500, out threadId, out receivedXmlData, null, null); result.AsyncWaitHandle.WaitOne(); caller.EndInvoke(out threadId, out receivedXmlData, result); result.AsyncWaitHandle.Close(); } // UdpClient received void UdpReceivedXmlFile(object objectFilename, UdpClient udpClient, IPEndPoint remoteEPReceived, int CallDuration, out int threadId) { Thread.Sleep(CallDuration); threadId = Thread.CurrentThread.ManagedThreadId; try { // Blocks until a message returns on this socket from a remote host. Byte[] receiveBytes = udpClient.Receive(ref remoteEPReceived); File.WriteAllText((string)objectFilename, Encoding.UTF8.GetString(receiveBytes)); } catch (Exception ex) { Console.WriteLine("Contact webmaster with this error in UdpReceivedXmlFile:\n " + ex.ToString()); } } // UdpClient send void UdpSendXmlFile(string fileToSend, UdpClient udpClient, byte[] bytes, int CallDuration, out int threadId) { Thread.Sleep(CallDuration); threadId = Thread.CurrentThread.ManagedThreadId; try { // Encode the data string into a byte array XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(fileToSend); // Load XML fil string xmlContent = xmlDoc.OuterXml; byte[] msg = Encoding.UTF8.GetBytes(xmlDoc.OuterXml); // Send the data through the socket. udpClient.Send(msg, msg.Length); } catch (Exception ex) { Console.WriteLine("Contact webmaster with this error in UdpSendXmlFile:\n " + ex.ToString()); } } // UdpStart Client public void UdpStartClient(object objectFilename, int CallDuration, out int threadId, out string receivedXmlData) { string filename = (string)objectFilename; receivedXmlData = null; Thread.Sleep(CallDuration); threadId = Thread.CurrentThread.ManagedThreadId; try { Console.WriteLine("1: UdpStartClient Async - id: " + threadId + " objectFilename: " + (string)objectFilename); fileToSend = fileLocation + filename; // Send a file to the UdpSocket UdpSendXmlFile(fileToSend, udpClient, bytes, CallDuration, out threadId); TimeSpan maxTime = TimeSpan.FromSeconds(10); Stopwatch stopwatch = Stopwatch.StartNew(); bool stopwatchStop = false; while (stopwatch.Elapsed < maxTime && !stopwatchStop) { // listed on UdpSocket and save to file UdpReceivedXmlFileDirectToFile("c:\\Received" + filename, udpClient, remoteEPReceived, CallDuration, out threadId); attributXMLReceived = ReadXmlAttribut("c:\\Received" + filename, CallDuration, out threadId); if ((attributXMLReceived == "Status=Pending") || (attributXMLReceived == "Status=Sent")) { Console.WriteLine("Answer from XMl file:" + attributXMLReceived + " id: " + Thread.CurrentThread.ManagedThreadId + "\n"); } else if (attributXMLReceived == "Status=Delivered") { Console.WriteLine("Answer from XMl file:" + attributXMLReceived + " id: " + Thread.CurrentThread.ManagedThreadId + "\n"); stopwatchStop = true; } if (stopwatch.Elapsed == maxTime) Console.WriteLine("Timeout!"); } } catch (Exception e) { Console.WriteLine(e.ToString()); } }

1 个答案:

答案 0 :(得分:2)

这就是UDP的工作方式。

UDP是一种在连接到IP网络的设备之间提供不可靠,无序数据传输的协议。它通常被认为是OSI堆栈中的“第4层”协议。 UDP的一种流行用途是用于传输时间敏感信息,例如IP语音。 UDP在RFC 768中指定。

http://www.techabulary.com/u/udp/

您需要使用TCP,或者准备好处理无序(可能完全丢失)的响应。

SCTP是传输协议的另一种选择。