示例#1
0
文件: IpcServer.cs 项目: hulian/bot
        private void receiveReadData(IAsyncResult ar)
        {
            TcpClientState state = (TcpClientState)ar.AsyncState;

            int bytesRead = state.stream.EndRead(ar);

            if (bytesRead > 0)
            {
                File.AppendAllText("net.log", "received data" + bytesRead + Environment.NewLine);

                MemoryStream ms = new MemoryStream(tcpClientState.buffer);
                ms.Seek(0, 0);
                StreamReader sr = new StreamReader(ms);

                action.Invoke(sr.ReadLine());
            }

            Thread.Sleep(10);

            if (state.tcpClient != null && !state.tcpClient.Connected)
            {
                File.AppendAllText("net.log", "client closed" + Environment.NewLine);
                state.stream.Close();
                state.tcpClient.Close();
                return;
            }

            if (!isUp)
            {
                File.AppendAllText("net.log", "stop receive data" + Environment.NewLine);
                return;
            }

            state.stream.BeginRead(state.buffer, 0, state.buffer.Length, new AsyncCallback(receiveReadData), state);
        }
示例#2
0
文件: IpcServer.cs 项目: hulian/bot
        private void acceptClient(IAsyncResult iar)
        {
            if (!isUp)
            {
                File.AppendAllText("net.log", "stop accept client" + Environment.NewLine);
                return;
            }

            TcpClient client = this.tcpListener.EndAcceptTcpClient(iar);

            File.AppendAllText("net.log", "client conneted :" + client + Environment.NewLine);

            tcpClientState = new TcpClientState(client);

            tcpClientState.stream.BeginRead(tcpClientState.buffer, 0, tcpClientState.buffer.Length, new AsyncCallback(receiveReadData), tcpClientState);

            tcpListener.BeginAcceptTcpClient(new AsyncCallback(acceptClient), this.tcpListener);
        }