Ciao,
ho creato due piccoli script un client un server e un oggetto.
I client inviano delle stringhe che il server prende e inserisce in un file (data.txt).
Da come credo di aver capito peroì in questo modo l'accesso al file ogni volta che un client lo richiede non è controllato e quindi potrebbe verificarsi una corruzione dei dati che vengono scritti nel file.
Ho letto quindi che si adotta l'utilizzo dei thread e della classe monitor per creare una coda (queue?) di processi che vengono svolti ogni qualvolta un thread ha finito di scrivere i dati sul file.
Mi potete fare aiutare a capire come realizzare cio'?
Qui ce l'esempio che sto svolgendo:
http://www.megafileupload.com/en/file/57862/remoting-zip.html
Codice:
Classe Oggetto:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace MyRemotableType
{
public class MyRemotableType : MarshalByRefObject
{
private string path = "dati.txt";
public MyRemotableType()
{
Console.WriteLine("A New MarshalByRefObject created");
}
public void write(string message)
{
if(!File.Exists(this.path))
{
using (StreamWriter sw = new StreamWriter(this.path))
{
// Add some text to the file.
sw.Write("This is the ");
sw.WriteLine("header for the file.");
// Arbitrary objects can also be written to the file.
sw.Write("The date is: ");
sw.WriteLine(DateTime.Now);
sw.WriteLine("The message is " + message);
sw.WriteLine("-------------------");
sw.Close();
}
}else
{
StreamWriter sw =File.AppendText(this.path);
// Add some text to the file.
sw.Write("This is the ");
sw.WriteLine("header for the file.");
// Arbitrary objects can also be written to the file.
sw.Write("The date is: ");
sw.WriteLine(DateTime.Now);
sw.WriteLine(message);
sw.WriteLine("-------------------");
sw.Close();
}
}
public string CaldSum(int a, int b)
{
return string.Format("Sum is: {0}", a + b);
}
}
}
Client:
Server:
Grazie.