public static void AcceptCallback(IAsyncResult ar) { // Signal the main thread to continue. allDone.Set(); Console.WriteLine("Соединение установлено"); // Get the socket that handles the client request. Socket listener = (Socket)ar.AsyncState; Socket handler = listener.EndAccept(ar); Brodcaster.AddSocket(handler); handler.Send(Encoding.ASCII.GetBytes('\r' + ProcessCMD.GetCurrentDirectory() + ProcessCMD.GetCommand())); // Create the state object. StateObject state = new StateObject(); state.workSocket = handler; handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state); }
public static void StartBroadcast() { CancellationToken token = cancelTokenSource.Token; Task task = new Task(() => { Task <string> task1 = outstr.ReadLineAsync(); Task <string> task2 = errorstr.ReadLineAsync(); while (!token.IsCancellationRequested) { if (task1.IsCompleted) { string temp = task1.Result; if (ProcessCMD.dirsync) { ProcessCMD.SetCurrentDirectory(temp); ProcessCMD.dirsync = false; } else if (ProcessCMD.exec) { outstr.ReadLine(); ProcessCMD.exec = false; } else { SendToAll(Encoding.ASCII.GetBytes(task1.Result + "\r\n")); } task1 = outstr.ReadLineAsync(); } else if (task2.IsCompleted) { SendToAll(Encoding.ASCII.GetBytes(task2.Result + "\r\n")); task2 = errorstr.ReadLineAsync(); } } }); task.Start(); }
public static int Main(String[] args) { ProcessCMD.Start(); StartListening(); return(0); }
public static void ReadCallback(IAsyncResult ar) { String content = String.Empty; // Retrieve the state object and the handler socket // from the asynchronous state object. StateObject state = (StateObject)ar.AsyncState; Socket handler = state.workSocket; // Read data from the client socket. int bytesRead; try { bytesRead = handler.EndReceive(ar); } catch { Brodcaster.RemoveSocket(handler); return; } if (bytesRead > 0) { for (int i = bytesRead; i < state.buffer.Length; i++) { state.buffer[i] = 0; } // There might be more data, so store the data received so far. //state.sb.Append(Encoding.ASCII.GetString( // state.buffer, 0, bytesRead)); // Check for end-of-file tag. If it is not there, read // more data. // content = state.sb.ToString(); //Brodcaster.NotifyAllExept(handler, state.buffer); string income = Encoding.ASCII.GetString(state.buffer, 0, bytesRead); if (income.Contains('\n')) { Brodcaster.NotifyAllExept(handler, Encoding.ASCII.GetBytes("\r\n")); } ProcessCMD.Write(income); if (!income.Contains('\n')) { Brodcaster.SendToAll(Encoding.ASCII.GetBytes('\r' + ProcessCMD.GetCurrentDirectory() + ProcessCMD.GetCommand())); } if (income.IndexOf('\n') > -1) { //Brodcaster.NotifyAllExept(null, Encoding.ASCII.GetBytes(ProcessCMD.GetCurrentDirectory())); } // Not all data received. Get more. try { handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state); } catch { Brodcaster.RemoveSocket(handler); } } else { Brodcaster.RemoveSocket(handler); handler.Shutdown(SocketShutdown.Both); handler.Close(); } }