コード例 #1
0
        /// <summary>
        /// 서버로 부터 메시지를 받음
        /// </summary>
        /// <param name="socketObject"></param>
        private void onSocketReceiveCallback(IAsyncResult socketObject)
        {
            try
            {
                // 추가 정보 불러오기
                AsyncSocketObject socketResult = (AsyncSocketObject)socketObject.AsyncState;
                int    receivedBytes           = _sck.Client.EndReceive(socketObject);
                string message = Encoding.UTF8.GetString(socketResult.buffer, 0, receivedBytes);

                if (receivedBytes > 0)
                {
                    // Debug: 테스트를 위해 shutdown 이라는 메시지가 들어오면 임의로 소켓 연결 재설정
                    if (message != "shutdown")
                    {
                        _sck.lastSendtime = DateTime.Now;
                        messageEvent?.Invoke(this, new messageArgs(message));

                        AsyncSocketObject newsocketObject = new AsyncSocketObject(_sck.ReceiveBufferSize);
                        _sck.Client.BeginReceive(newsocketObject.buffer, 0, newsocketObject.buffer.Length, SocketFlags.None, new AsyncCallback(onSocketReceiveCallback), newsocketObject);

                        return;
                    }
                }

                // 소캣 연결 종료 이벤트 발생
                _sck.finalTime = DateTime.Now;
                lostEvent?.Invoke(this, new lostConnectionArgs("서버 연결 종료됨", _sck.remote, _sck.port));
            }
            catch (Exception)
            {
                // 소캣 연결 종료 이벤트 발생
                _sck.finalTime = DateTime.Now;
                lostEvent?.Invoke(this, new lostConnectionArgs("서버 연결 종료됨", _sck.remote, _sck.port));
            }
        }
コード例 #2
0
        /// <summary>
        /// 소켓 메시지를 받음
        /// </summary>
        private void ListenforMessage()
        {
            if (_sck.Connected == false)
            {
                // 소켓이 연결되어 있지 않음
                throw new Exceptions.socketConnect("연결이 수립되지 않았습니다.");
            }

            AsyncSocketObject socketObject = new AsyncSocketObject(_sck.ReceiveBufferSize);

            socketObject.WorkingSocket = _sck.Client;
            _sck.Client.BeginReceive(socketObject.buffer, 0, socketObject.buffer.Length, SocketFlags.None, new AsyncCallback(onSocketReceiveCallback), socketObject);
        }
コード例 #3
0
        public void sendMessage(string message)
        {
            byte[]            buffer       = Encoding.UTF8.GetBytes(message);
            AsyncSocketObject socketObject = new AsyncSocketObject(_sck.ReceiveBufferSize);

            socketObject.WorkingSocket = _sck.Client;
            socketObject.buffer        = buffer;

            try
            {
                _sck.Client.BeginSend(socketObject.buffer, 0, socketObject.buffer.Length, SocketFlags.None, new AsyncCallback(onSocketSendCallback), socketObject);
                _sck.lastSendtime = DateTime.Now;
            }
            catch (Exception)
            {
                _sck.finalTime = DateTime.Now;
                lostEvent?.Invoke(this, new lostConnectionArgs("서버 연결 종료"));
            }
        }