1

XMLファイルをUdpSocketに送信するコードがあり、回答を受け取りました。回答を非同期で送受信します。私の問題は、UDP ソケットから回答を受け取ったときに、それを正しいファイルに保存できないことです。私は多くのことを試しましたが、何もうまくいきません。

私のコードを簡単に説明します。Main() メソッドで 3 つの AsynchronousConnection を作成し始めます。そこから static 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

たとえば、ランダムに回答を受け取りました。

ファイル MessagingText4000.xml は MessagingText8.xml から回答を得ることができます

ファイル MessagingText4001.xml は MessagingText4000.xml から回答を得ることができます

ファイル 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());
  }
}

4

1 に答える 1

2

それがUDPの仕組みです。

UDP は、IP ネットワークに接続されたデバイス間で、信頼性が低く、順序付けされていないデータ配信を提供するプロトコルです。これは一般に、OSI スタックの「レイヤ 4」プロトコルと見なされます。UDP の一般的な用途の 1 つは、Voice over IP など、時間に依存する情報の転送です。UDP は RFC 768 で指定されています。

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

TCP を使用するか、順不同の (そしておそらく完全に欠落している) 応答を処理する準備をする必要があります。

SCTPは、伝送プロトコルのもう 1 つのオプションです。

于 2013-08-25T19:12:51.243 に答える