Hi bad boy,
Sorry for the delay I had been in training.
For the server com, I have the following code :
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace UDPServeur
{
public class Program
{
private static Thread _thEcoute;
private static void Main(string[] args)
{
//Préparation et démarrage du thread en charge d'écouter.
_thEcoute = new Thread(new ThreadStart(Ecouter));
_thEcoute.Start();
}
private static void Ecouter()
{
Console.WriteLine("Préparation à l'écoute...");
//On crée le serveur en lui spécifiant le port sur lequel il devra écouter.
UdpClient serveur = new UdpClient(6008);
//On crée la trame XML de réponse
System.Xml.XmlDocument SendXML = new System.Xml.XmlDocument(); // XmlDocument pattern
// On charge le modèle de réponse
SendXML.PreserveWhitespace = true;
SendXML.Load("C:\\Documents and Settings\\alexandre\\Mes documents\\Visual Studio 2010\\Projects\\serveurUDPsdz\\ExternalData.xml");
//Création d'une boucle infinie qui aura pour tâche d'écouter.
while (true)
{
//Création d'un objet IPEndPoint qui recevra les données du Socket distant.
IPEndPoint client = null;
Console.WriteLine("ÉCOUTE...");
//On écoute jusqu'à recevoir un message.
byte[] data = serveur.Receive(ref client);
//Console.WriteLine("Données reçues en provenance de {0}:{1}.", client.Address, client.Port);
//Décryptage et affichage du message.
string message = Encoding.ASCII.GetString(data);
//Console.WriteLine("CONTENU DU MESSAGE : {0}\n", message);
// wait for data and receive bytes
// convert bytes to string
string strReceive = message;
// take a look to the end of data
if ((strReceive.LastIndexOf("</Rob>")) == -1)
{
continue;
}
else
{
// mirror the IPO counter you received yet
string strSend = SendXML.InnerXml;
strSend = mirrorIPOC(strReceive, strSend);
// données envoyé au client
//Console.WriteLine("Données envoyées au client {0}:{1}.", client.Address, client.Port);
//Console.WriteLine("CONTENU DU MESSAGE : {0}\n", strSend);
// send data as requested
byte[] msg = System.Text.Encoding.ASCII.GetBytes(strSend);
serveur.Send(msg, msg.Length, client);
}
strReceive = null;
}
}
// send immediately incoming IPO counter to have a timestamp
private static string mirrorIPOC(string receive, string send)
{
// separate IPO counter as string
int startdummy = receive.IndexOf("<IPOC>") + 6;
int stopdummy = receive.IndexOf("</IPOC>");
string Ipocount = receive.Substring(startdummy, stopdummy - startdummy);
//IPOC arrivé
Console.WriteLine("IPOCclienttoserv {0}", Ipocount);
//Console.WriteLine("IPOCclienttoserv {0}, {1}", startdummy, stopdummy);
// find the insert position
startdummy = send.IndexOf("<IPOC>") + 6;
stopdummy = send.IndexOf("</IPOC>");
//IPOC arrivé
//Console.WriteLine("IPOCservtoclient {0}, {1}", startdummy, stopdummy);
// remove the old value and insert the actual value
send = send.Remove(startdummy, stopdummy - startdummy);
send = send.Insert(startdummy, Ipocount);
// send back the string
return send;
}
}
}
This is only a server that receive data according to an example XML frame. Then send it back. It is very basic, however it might help you start.
Only one point. In my case, as I work with RSI for real time data transmission, C# seems to be a bit too slow with my computer. It is not really able to send data every 4 ms.
I know that C++ server can have better time response.
Some comments are in french, sorry for that.
If it helps, could you post back the improvements ? I do not have much time to work on this as it is not my main work so it would help me also if you can share your work.
If not possible because confidential I understand
Thanks
Alex