2016-09-06 3 views
0

UWP를 사용하여 UDP 멀티 캐스트 주소와 송수신하려고합니다. 처음 몇 번은 완벽하게 작동하지만,이 샌드 - 수신 프로세스가 끝나면받는 부분을 잠글 것입니다. 비동기 방식에서 동기식 방식으로 변경되었지만 여전히 동일합니다. 새 UDP 클라이언트를 인스턴스화하더라도 응용 프로그램을 다시 시작할 때까지 포트가 차단됩니다. 내가 잘못한 건 뭐니?여러 개의 송수신 후 UDP 클라이언트가 수신을 중지하고 포트를 차단합니다.

private UdpClient udp; 
//inside main function: 
if (udp == null) 
     { 
      udp = new UdpClient(new IPEndPoint(IPAddress.Any, portNumber)); 
      //^the second time this is called, it will complain about port reuse 
      udp.Client.ReceiveTimeout = udp.Client.SendTimeout = 3000; 
      //udp.Client.SetSocketOption(SocketOptionLevel.Udp, SocketOptionName.ReuseAddress, true); 
      //^invalid 
     } 

     //await udp.SendAsync(data, data.Length, , portNumber); 
     //I changed from async to synchronous in case it was the issue, but no. 
     udp.Client.SendTo(data, new IPEndPoint(IPAddress.Parse(ipString), portNumber)); 
     //the receive used to be async, too 
     byte[] receivedByte = new byte[udp.Client.ReceiveBufferSize]; 
     try 
     { 
      udp.Client.Receive(receivedByte); 
     } 
     catch (Exception ex) 
     { 
      udp.Client.Shutdown(SocketShutdown.Both); 
      udp = null; // added these, but port still blocked until restart 
     } 

저는 UWP를 사용하고 있으며 클래스 라이브러리에는 여기에없는 메소드가 있습니다.

+1

백 로그 또는 열 수있는 소켓 수가 제한되어 있다고 생각하십니까? –

+0

@Am_I_Helpful 모르겠지만 그렇게 생각하지 마십시오 ... 아마도 외부 문제 일 수 있습니다 ... – Tyress

+0

문제를 재현하는 데 도움이 될 수있는 최소한의 샘플을 제공해 주시겠습니까? –

답변

1

UdpClient를 private 필드로 선언하는 대신 using() 문에 넣은 다음 짧은 비동기 메서드에 넣어 범위를 제한하면 더 이상 이러한 문제가 발생하지 않습니다.

+1

OK, 좋습니다.;) –

관련 문제