Exemple #1
0
 public static void Start()
 {
     outStream.ReadLine();
     outStream.ReadLine();
     outStream.ReadLine();
     inputStream.Write("\n");
     curDirectory        = outStream.ReadLine();
     curDirectory        = "PC-Telnet>";
     Brodcaster.outstr   = outStream;
     Brodcaster.errorstr = errorStream;
     Brodcaster.StartBroadcast();
 }
Exemple #2
0
    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);
    }
Exemple #3
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();
        }
    }