Microsoft MVP성태의 닷넷 이야기
비동기 소켓 close시 ObjectDisposedException 문제점 질문 있습니다.. [링크 복사], [링크+제목 복사],
조회: 21975
글쓴 사람
C#초보
홈페이지
첨부 파일
 

        Socket listenSocket;

        private void tcpip_conBtn_Click(object sender, EventArgs e)
        {
            if (tcpip_conBtn.Text == "CONNECT")
            {
                tcpServer = new Server(10, 1024); // the maximum number of connectios , buffer size
                tcpServer.Initialize();
                tcpServer.Start(new IPEndPoint(IPAddress.Any, 5000));
                tcpip_textBox.Text += "TCP/IP SocketAsyncServer Start().... \r\n";
                tcpip_conBtn.Text = "DISCONNECT";
            }
            else
            {
                tcpip_conBtn.Text = "CONNECT";

                listenSocket .close();
              // tcpServer = null;
            }
        }


        public void Start(IPEndPoint localEndPoint)
        {
            // create the socket which listens for incoming connections
            listenSocket = new Socket(localEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
            listenSocket.Bind(localEndPoint);
            listenSocket.Listen(100);

            StartAccept(null);
        }


        public void StartAccept(SocketAsyncEventArgs acceptEventArg)
        {
            if (acceptEventArg == null)
            {
                acceptEventArg = new SocketAsyncEventArgs();
                acceptEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(AcceptEventArg_Completed);
            }
            else
            {
                // socket must be cleared since the context object is being reused
                acceptEventArg.AcceptSocket = null;
            }

            m_maxNumberAcceptedClients.WaitOne();


            bool willRaiseEvent = listenSocket.AcceptAsync(acceptEventArg);

            if (!willRaiseEvent)
            {
                ProcessAccept(acceptEventArg);
            }

        }

        void AcceptEventArg_Completed(object sender, SocketAsyncEventArgs e)
        {
            ProcessAccept(e);
        }

  
버튼을 이용 연결 및 해제를 하려고 하는데요...
해제를 위해 버튼을 눌렀을때 소켓을 close() ( listen.close(); )하면..

StartAccept () 계속 호출 되는데요... 원래 이런건지요?? 아니면 소스가 잘못된건지요....
ObjectDisposedException 에러가 호출 되네요..
StartAccept () {
...
...
 bool willRaiseEvent = listenSocket.AcceptAsync(acceptEventArg); // 에러 부분...
...
...
...
}

조언좀 해주시면 감사하겠습니다...
참고로..
https://msdn.microsoft.com/ko-kr/library/system.net.sockets.socketasynceventargs(v=vs.110).aspx
예제를 참고하면서 구현하고 있습니다..








[최초 등록일: ]
[최종 수정일: 12/28/2016]


비밀번호

댓글 작성자
 



2016-12-30 06시31분
재현이 되는 최소한의 예제 프로젝트를 올려주세요.
정성태

... 16  17  18  [19]  20  21  22  23  24  25  26  27  28  29  30  ...
NoWriterDateCnt.TitleFile(s)
5492한예지 donator5/5/202113322FromCurrentSynchronizationContext 관련 코드 질문있습니다! [2]
5491조우성5/4/202118919WinForm과 WPF의 성능차이 비교한 자료가 있을까요? [4]
5490한예지 donator5/3/202114802UI 스레드의 Invoke 질문있습니다. [4]
5489저누4/28/202115185시작하세요 C# 9.0, 225페이지 구조체 관련 질문드립니다. [2]
5488종범4/27/202115203C# 비동기 함수 async, await 와 Task의 관계에 대해 질문 드립니다. [6]
5487진우4/27/202114384C# 엑셀 자동화 성능 향상 문의 [2]
5486지나가던...4/26/202114701닷넷 구현 코드 관련 질문 [2]
5485이재원4/17/202114996교재 315페이지 내용 질문 [3]
5484Syong4/16/202118888윈폼 기반의 응용프로그램 dll 참조와 32,64bit 빌드 관련 문의 [4]
5483한예지 donator4/15/202113937익명 형식과 var 관계 질문 있습니다. [2]
5482질문4/13/202115623WPF를 위한 MVVM toolkit 선택과 관련한 문의드립니다. [4]
5480한예지 donator4/5/202116889GetHashCode 질문있습니다! [2]
5479한예지 donator4/4/202115584Equals를 닷넷에서 어떻게 구현했는지 보고 싶을 떄는 어떻게 해야 될까요? [2]
5478갑자기C#3/23/202118720C# Winform에서 TextBox없이 입력 받을 수 있나요? [7]
5477달팽이3/18/202114728c# 도형을 그린후 편집하는 방법이 궁금합니다. [2]파일 다운로드1
5475dimo...3/2/202115268POH가 .NET 5에 추가된 것으로 알고 있습니다. POH가 유용한 경우는 어떤 경우가 있을까요? [1]
5474Syong2/26/202114989사용자 지정 컨트롤 생성시 Invalidate, Update, Refresh의 차이점 [2]
5473한예지 donator2/25/202114099디자인 타임이 뭔지 궁금합니다!! [5]
5472dimo...2/24/202116105등록하지 않고 ocx를 사용하는 `키움`관련 포스팅을 따라가다 질문드립니다. [9]
5471남산2/21/202113838MarshalDirectiveException 에 대한 질문입니다. [1]
5470한예지 donator2/7/202113846yield 질문있습니다!! [2]
5469유호성2/6/202113668Parallel + Task.Run 동시 실행 환경에서 간헐적으로 Task.Run()에서 null이 리턴됩니다. [3]파일 다운로드1
5467hero...2/4/202114196실행 환경에 따른 Thread.Sleep 딜레이 차이 질문 [8]
5466pr1/29/202114041c# winform load시 작업표시줄에 뜨지 않는 현상을 겪으신적이 있으신가요? [4]파일 다운로드1
5465영귤1/28/202114046두 번째 await 부터는 스레드 개수만 늘어나는 것이 아닌가요 [1]
5464민우1/26/202115461C# 빌드시 코드 치환되는걸 확인하는 방법 문의 [2]
... 16  17  18  [19]  20  21  22  23  24  25  26  27  28  29  30  ...