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.It is completely unnecessary to create a new
Thread
to callStartListening
. The thread will terminate onceudpClient.BeginReceive()
has been called as this is async. You might as well callStartListening()
directly inPrepareClient()
.UdpClient
isIDisposable
and should therefor be disposed when not needed anymore. You do not clean up the existingUdpClient
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.
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.It is completely unnecessary to create a new
Thread
to callStartListening
. The thread will terminate onceudpClient.BeginReceive()
has been called as this is async. You might as well callStartListening()
directly inPrepareClient()
.UdpClient
isIDisposable
and should therefor be disposed when not needed anymore. You do not clean up the existingUdpClient
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.
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.It is completely unnecessary to create a new
Thread
to callStartListening
. The thread will terminate onceudpClient.BeginReceive()
has been called as this is async. You might as well callStartListening()
directly inPrepareClient()
.UdpClient
isIDisposable
and should therefor be disposed when not needed anymore. You do not clean up the existingUdpClient
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.
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.It is completely unnecessary to create a new
Thread
to callStartListening
. YourThe thread will terminate onceudpClient.BeginReceive()
has been called as this is async. You might as well callStartListening()
directly inPrepareClient()
.UdpClient
isIDispoosableIDisposable
and should therefor be disposed when not needed anymore. You do not clean up the existingUdpClient
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.
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.It is completely unnecessary to create a new
Thread
to callStartListening
. Your thread will terminate onceudpClient.BeginReceive()
has been called as this is async. You might as well callStartListening()
directly inPrepareClient()
.UdpClient
isIDispoosable
and should therefor be disposed when not needed anymore. You do not clean up the existingUdpClient
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.
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.It is completely unnecessary to create a new
Thread
to callStartListening
. The thread will terminate onceudpClient.BeginReceive()
has been called as this is async. You might as well callStartListening()
directly inPrepareClient()
.UdpClient
isIDisposable
and should therefor be disposed when not needed anymore. You do not clean up the existingUdpClient
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.
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.It is completely unnecessary to create a new
Thread
to callStartListening
. Your thread will terminate onceudpClient.BeginReceive()
has been called as this is async. You might as well callStartListening()
directly inPrepareClient()
.UdpClient
isIDispoosable
and should therefor be disposed when not needed anymore. You do not clean up the existingUdpClient
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.