Reputation: 81
I have a code there send a XML file to a UdpSocket and received an answer. I send and received the answer Asynchronous. My problem is, when I received the answer from UDP socket, I cannot save it to the right file. I have tried a lot of things, but nothing works.
To explain my code short. I start to make 3 AsynchronousConnection in the Main() method. Which call static void AsynchronousConnection(object objectFilename) from there I call the UdpStartClient method.
From UdpStartClient method I send a file with UdpSendXmlFile(fileToSend, udpClient, bytes, CallDuration, out threadId);
After that, I received the answer in a while loop with method UdpReceivedXmlFile("c:\Received" + filename, udpClient, remoteEPReceived, CallDuration, out threadId);
The answer I received in the UdpReceivedXmlFile method, will be save to a file. It is here my problem is, I think. I send 3 file by AsynchronousConnection and received 3 answer from the UDP socket, but the answers does not match the file I send.
For example I send these 3 files.
MessagingText4000.xml
MessagingText4001.xml
MessagingText8.xml
I received the answer randomly, for example:
File MessagingText4000.xml can get the answer from MessagingText8.xml
File MessagingText4001.xml can get the answer from MessagingText4000.xml
File MessagingText8.xml can get the answer from MessagingText4001.xml
Can you help me, so I received the right answer to the right file?
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());
}
}
Upvotes: 1
Views: 632
Reputation: 3167
That's how UDP works.
UDP is a protocol that provides an unreliable, unordered data delivery between devices connected to an IP network. It is generally considered a "layer 4" protocol in the OSI stack. One popular use of UDP is for transport of time-sensitive information, such as Voice over IP. UDP is specified in RFC 768.
http://www.techabulary.com/u/udp/
You either need to use TCP, or be prepared to handle the out-of-order (and possibly completely missing) responses.
SCTP is another option for your transmission protocol.
Upvotes: 2