I am writing a socket server which receives many client request to write to a file. I have an arraylist to store the socketlisteners. After I ran the client to send up to 25 requests, which results in the socket server storing 25 socketlisteners and starting 25 threads to receive the data and write to the file. After running I get errors from the server that its trying to access the same file which is locked by another process. I also get null reference errors. So, what is the best way to synchronize the threads so all the requests get processed and all the data get written to the file.
Any help is appreciated. Thanks. -CR
string data = Encoding.ASCII.GetString(byteBuffer,0, size);
if (!File.Exists(filepath))
{
using (StreamWriter sw = File.CreateText(filepath))
{
sw.WriteLine(data);
}
}
else
{
using (StreamWriter sw = File.AppendText(filepath))
{
sw.WriteLine(data);
}
}