public void Run() { var tq = TaskQueue.GetInstance(); var sock = new Socket(SocketType.Stream, ProtocolType.Tcp); sock.Bind(new IPEndPoint(IPAddress.Any, _port)); sock.Listen(); while (true) { var socket = sock.Accept(); var text = ReceiveAllText(socket); if (text.StartsWith("list")) { socket.Send(GetFileNames()); } else if (text.StartsWith("hash")) { socket.Send(GetHash(text)); } else if (text.StartsWith("size")) { socket.Send(GetFileSize(text)); } else if (text.StartsWith("status")) { socket.Send(_fs.isRunning ? Encoding.UTF8.GetBytes("active") : Encoding.UTF8.GetBytes("stopped")); } else if (text.StartsWith("stop")) { _fs.isRunning = false; socket.Send(Encoding.UTF8.GetBytes("stopped")); } else if (text.StartsWith("start")) { if (!_fs.isRunning) { _fs.isRunning = true; ThreadDispatcher.GetInstance().AddInQueue(_fs); } socket.Send(Encoding.UTF8.GetBytes("started")); } } }
public void Run() { var tq = TaskQueue.GetInstance(); if (_socket is null) { _socket = new Socket(SocketType.Stream, ProtocolType.Tcp); _socket.Bind(new IPEndPoint(IPAddress.Any, _port)); } _socket.Listen(); while (isRunning) { var socket = _socket.Accept(); tq.AddTask(new FileServerConnection(socket, (string)_path.Clone())); } _socket.Close(); _socket = null; }