我有一个代码将 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 个文件。
消息文本4000.xml
消息文本4001.xml
消息文本8.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());
}
}