hey
I'm writing a multithreaded server , and I want to print a line in a file whenever a client is connected , I need to lock the filestream to prevent the lines of multiple thread from overlapping I used an integer lock but it won't work right , so any help
if (server.Pending())
{
TcpClient client = server.AcceptTcpClient();
string time;
time = DateTime.Now.ToString(@"MM\/dd\/yyyy h\:mm tt");
if (Lock == 0)
{
Lock = 1;
if (first == 0)
{
first++;
}
else
{
statistics = new FileStream("statistic.txt", FileMode.Append, FileAccess.Write, FileShare.None);
wr = new BinaryWriter(statistics);
}
string clientIPAddress =time+ ", A connection is made from the client whose IP Address is: " + IPAddress.Parse(((
IPEndPoint)client.Client.RemoteEndPoint).Address.ToString())+"\r\n";
wr.Write(clientIPAddress);
closeFile();
Console.Write(clientIPAddress);
Lock = 0;
}}