Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
  1. I find a better naming convention for private fields on a class is _PascalCase or _camelCase. This makes it easy to see whether you are using a field or a local variable.

  2. It is completely unnecessary to create a new Thread to call StartListening. The thread will terminate once udpClient.BeginReceive() has been called as this is async. You might as well call StartListening() directly in PrepareClient().

  3. UdpClient is IDisposable and should therefor be disposed when not needed anymore. You do not clean up the existing UdpClient at all.

The answer to this question The answer to this question indicates that when you call Dispose or Close on a socket then the callback is invoked but will throw an ObjectDisposedException when calling EndReceive . Because you are storing the currentAsyncResult you can probably get around this by changing StartListening() to this:

 private readonly object _clientLock = new object();
 private void StartListening()
 {
 lock (_clientLock)
 {
 if (udpClient != null)
 {
 currentAsyncResult = null;
 updClient.Dispose();
 }
 udpClient = new UdpClient();
 udpClient.ExclusiveAddressUse = false;
 udpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
 udpClient.Client.Bind(localEP);
 var s = new UdpState(localEP, udpClient);
 currentAsyncResult = udpClient.BeginReceive(new AsyncCallback(ReceiveCallback), s);
 }
 }

Plus you should lock the inner block of your receive callback. This should ensure that you don't kill the client in the middle of receiving.

  1. I find a better naming convention for private fields on a class is _PascalCase or _camelCase. This makes it easy to see whether you are using a field or a local variable.

  2. It is completely unnecessary to create a new Thread to call StartListening. The thread will terminate once udpClient.BeginReceive() has been called as this is async. You might as well call StartListening() directly in PrepareClient().

  3. UdpClient is IDisposable and should therefor be disposed when not needed anymore. You do not clean up the existing UdpClient at all.

The answer to this question indicates that when you call Dispose or Close on a socket then the callback is invoked but will throw an ObjectDisposedException when calling EndReceive . Because you are storing the currentAsyncResult you can probably get around this by changing StartListening() to this:

 private readonly object _clientLock = new object();
 private void StartListening()
 {
 lock (_clientLock)
 {
 if (udpClient != null)
 {
 currentAsyncResult = null;
 updClient.Dispose();
 }
 udpClient = new UdpClient();
 udpClient.ExclusiveAddressUse = false;
 udpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
 udpClient.Client.Bind(localEP);
 var s = new UdpState(localEP, udpClient);
 currentAsyncResult = udpClient.BeginReceive(new AsyncCallback(ReceiveCallback), s);
 }
 }

Plus you should lock the inner block of your receive callback. This should ensure that you don't kill the client in the middle of receiving.

  1. I find a better naming convention for private fields on a class is _PascalCase or _camelCase. This makes it easy to see whether you are using a field or a local variable.

  2. It is completely unnecessary to create a new Thread to call StartListening. The thread will terminate once udpClient.BeginReceive() has been called as this is async. You might as well call StartListening() directly in PrepareClient().

  3. UdpClient is IDisposable and should therefor be disposed when not needed anymore. You do not clean up the existing UdpClient at all.

The answer to this question indicates that when you call Dispose or Close on a socket then the callback is invoked but will throw an ObjectDisposedException when calling EndReceive . Because you are storing the currentAsyncResult you can probably get around this by changing StartListening() to this:

 private readonly object _clientLock = new object();
 private void StartListening()
 {
 lock (_clientLock)
 {
 if (udpClient != null)
 {
 currentAsyncResult = null;
 updClient.Dispose();
 }
 udpClient = new UdpClient();
 udpClient.ExclusiveAddressUse = false;
 udpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
 udpClient.Client.Bind(localEP);
 var s = new UdpState(localEP, udpClient);
 currentAsyncResult = udpClient.BeginReceive(new AsyncCallback(ReceiveCallback), s);
 }
 }

Plus you should lock the inner block of your receive callback. This should ensure that you don't kill the client in the middle of receiving.

fixed typo
Source Link
ChrisWue
  • 20.6k
  • 4
  • 42
  • 107
  1. I find a better naming convention for private fields on a class is _PascalCase or _camelCase. This makes it easy to see whether you are using a field or a local variable.

  2. It is completely unnecessary to create a new Thread to call StartListening. YourThe thread will terminate once udpClient.BeginReceive() has been called as this is async. You might as well call StartListening() directly in PrepareClient().

  3. UdpClient is IDispoosableIDisposable and should therefor be disposed when not needed anymore. You do not clean up the existing UdpClient at all.

The answer to this question indicates that when you call Dispose or Close on a socket then the callback is invoked but will throw an ObjectDisposedException when calling EndReceive . Because you are storing the currentAsyncResult you can probably get around this by changing StartListening() to this:

 private readonly object _clientLock = new object();
 private void StartListening()
 {
 lock (_clientLock)
 {
 if (udpClient != null)
 {
 currentAsyncResult = null;
 updClient.Dispose();
 }
 udpClient = new UdpClient();
 udpClient.ExclusiveAddressUse = false;
 udpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
 udpClient.Client.Bind(localEP);
 var s = new UdpState(localEP, udpClient);
 currentAsyncResult = udpClient.BeginReceive(new AsyncCallback(ReceiveCallback), s);
 }
 }

Plus you should lock the inner block of your receive callback. This should ensure that you don't kill the client in the middle of receiving.

  1. I find a better naming convention for private fields on a class is _PascalCase or _camelCase. This makes it easy to see whether you are using a field or a local variable.

  2. It is completely unnecessary to create a new Thread to call StartListening. Your thread will terminate once udpClient.BeginReceive() has been called as this is async. You might as well call StartListening() directly in PrepareClient().

  3. UdpClient is IDispoosable and should therefor be disposed when not needed anymore. You do not clean up the existing UdpClient at all.

The answer to this question indicates that when you call Dispose or Close on a socket then the callback is invoked but will throw an ObjectDisposedException when calling EndReceive . Because you are storing the currentAsyncResult you can probably get around this by changing StartListening() to this:

 private readonly object _clientLock = new object();
 private void StartListening()
 {
 lock (_clientLock)
 {
 if (udpClient != null)
 {
 currentAsyncResult = null;
 updClient.Dispose();
 }
 udpClient = new UdpClient();
 udpClient.ExclusiveAddressUse = false;
 udpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
 udpClient.Client.Bind(localEP);
 var s = new UdpState(localEP, udpClient);
 currentAsyncResult = udpClient.BeginReceive(new AsyncCallback(ReceiveCallback), s);
 }
 }

Plus you should lock the inner block of your receive callback. This should ensure that you don't kill the client in the middle of receiving.

  1. I find a better naming convention for private fields on a class is _PascalCase or _camelCase. This makes it easy to see whether you are using a field or a local variable.

  2. It is completely unnecessary to create a new Thread to call StartListening. The thread will terminate once udpClient.BeginReceive() has been called as this is async. You might as well call StartListening() directly in PrepareClient().

  3. UdpClient is IDisposable and should therefor be disposed when not needed anymore. You do not clean up the existing UdpClient at all.

The answer to this question indicates that when you call Dispose or Close on a socket then the callback is invoked but will throw an ObjectDisposedException when calling EndReceive . Because you are storing the currentAsyncResult you can probably get around this by changing StartListening() to this:

 private readonly object _clientLock = new object();
 private void StartListening()
 {
 lock (_clientLock)
 {
 if (udpClient != null)
 {
 currentAsyncResult = null;
 updClient.Dispose();
 }
 udpClient = new UdpClient();
 udpClient.ExclusiveAddressUse = false;
 udpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
 udpClient.Client.Bind(localEP);
 var s = new UdpState(localEP, udpClient);
 currentAsyncResult = udpClient.BeginReceive(new AsyncCallback(ReceiveCallback), s);
 }
 }

Plus you should lock the inner block of your receive callback. This should ensure that you don't kill the client in the middle of receiving.

Source Link
ChrisWue
  • 20.6k
  • 4
  • 42
  • 107
  1. I find a better naming convention for private fields on a class is _PascalCase or _camelCase. This makes it easy to see whether you are using a field or a local variable.

  2. It is completely unnecessary to create a new Thread to call StartListening. Your thread will terminate once udpClient.BeginReceive() has been called as this is async. You might as well call StartListening() directly in PrepareClient().

  3. UdpClient is IDispoosable and should therefor be disposed when not needed anymore. You do not clean up the existing UdpClient at all.

The answer to this question indicates that when you call Dispose or Close on a socket then the callback is invoked but will throw an ObjectDisposedException when calling EndReceive . Because you are storing the currentAsyncResult you can probably get around this by changing StartListening() to this:

 private readonly object _clientLock = new object();
 private void StartListening()
 {
 lock (_clientLock)
 {
 if (udpClient != null)
 {
 currentAsyncResult = null;
 updClient.Dispose();
 }
 udpClient = new UdpClient();
 udpClient.ExclusiveAddressUse = false;
 udpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
 udpClient.Client.Bind(localEP);
 var s = new UdpState(localEP, udpClient);
 currentAsyncResult = udpClient.BeginReceive(new AsyncCallback(ReceiveCallback), s);
 }
 }

Plus you should lock the inner block of your receive callback. This should ensure that you don't kill the client in the middle of receiving.

lang-cs

AltStyle によって変換されたページ (->オリジナル) /