コード例 #1
0
 public void Send(byte[] data)
 {
     try
     {
         TcpState state = new TcpState(this.tcpClient);
         state.Stream.BeginWrite(data, 0, data.Length, new AsyncCallback(sendCallback), state);
     }
     catch (Exception ex)
     {
         this.Error?.Invoke(this, new DyEventArgs()
         {
             Message = ex.Message + "send"
         });
     }
 }
コード例 #2
0
        private void sendCallback(IAsyncResult ar)
        {
            TcpState state = (TcpState)ar.AsyncState;

            try
            {
                state.Stream.EndWrite(ar);
            }
            catch (Exception ex)
            {
                this.Error?.Invoke(this, new DyEventArgs()
                {
                    Message = ex.Message + "   sendCallback"
                });
            }
        }
コード例 #3
0
        private void receiveCallback(IAsyncResult ar)
        {
            TcpState state     = (TcpState)ar.AsyncState;
            int      readbytes = 0;

            try
            {
                readbytes = state.Stream.EndRead(ar);
            }
            catch
            {
                readbytes = 0;
            }
            if (readbytes == 0)
            {
                this.Error?.Invoke($"Client {state.ID} is already disconnect!");
                this.clientDict.Remove(state.ID);
                return;
            }
            byte[] buf = new byte[readbytes];
            Array.Copy(state.Buffer, 0, buf, 0, readbytes);
            state.Stream.BeginRead(state.Buffer, 0, state.Buffer.Length, new AsyncCallback(receiveCallback), state);
            this.Received?.Invoke(buf);
        }
コード例 #4
0
        private void sendCallback(IAsyncResult ar)
        {
            TcpState state = (TcpState)ar.AsyncState;

            state.Stream.EndWrite(ar);
        }
コード例 #5
0
        public void Send(byte[] data, string remote)
        {
            TcpState state = this.clientDict[remote];

            state.Stream.BeginWrite(data, 0, data.Length, new AsyncCallback(sendCallback), state);
        }