UDPListener: System.Net.Sockets.SocketException

J

J C

Hi,

I'm using UDPClient to make a simple DNS server. I notice that
intermittently and unpredictibly I get:

Unhandled Exception: System.Net.Sockets.SocketException: An existing
connection
was forcibly closed by the remote host
at System.Net.Sockets.Socket.ReceiveFrom(Byte[] buffer, Int32 offset,
Int32 s
ize, SocketFlags socketFlags, EndPoint& remoteEP)
at System.Net.Sockets.UdpClient.Receive(IPEndPoint& remoteEP)
at dnsone_c.CoolUDP.Receive()
at dnsone_c.Program.Main(String[] args)

If I try to restart the server right away, I get:

Unhandled Exception: System.Net.Sockets.SocketException: Only one usage of
each
socket address (protocol/network address/port) is normally permitted
at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot,
SocketAddress
socketAddress)
at System.Net.Sockets.Socket.Bind(EndPoint localEP)
at System.Net.Sockets.UdpClient..ctor(IPEndPoint localEP)
at dnsone_c.CoolUDP.Receive()
at dnsone_c.Program.Main(String[] args)

After about a minute, this goes away and I can restart the program as usual.

Is the problem maybe that I'm both sending and receiving out of the same
UDPClient object? It's like something's getting in the way and blocking the
port and the program throws a fit.

Thanks,

--Jon
(e-mail address removed)
 
J

J C

It appears I can keep the program from erroring out by putting the Receive
and Send calls in a try/catch block, but if anyone has advice on how to
eliminate the problem rather than just mask it, that would be great!

--Jon
 
V

Vadym Stetsyak

Hello, J!

Can you post the code sample where you perform network I/O?

J> Unhandled Exception: System.Net.Sockets.SocketException: An existing
J> connection
J> was forcibly closed by the remote host
J> at System.Net.Sockets.Socket.ReceiveFrom(Byte[] buffer, Int32 offset,
J> Int32 s
J> ize, SocketFlags socketFlags, EndPoint& remoteEP)
J> at System.Net.Sockets.UdpClient.Receive(IPEndPoint& remoteEP)
J> at dnsone_c.CoolUDP.Receive()
J> at dnsone_c.Program.Main(String[] args)

Do you use UDPClient.Connect(...) method?
--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
 
J

J C

Sure... the general idea is:

namespace dnsone_c {
class Program {
static void Main(string[] args) {
CoolUDP myCoolUDP = new CoolUDP();
myCoolUDP.Receive();
}
}

class CoolUDP {
private IPEndPoint myEndPoint = new IPEndPoint(IPAddress.Any, 53);
private IPEndPoint herEndPoint = new IPEndPoint(IPAddress.Any, 0);

public void Receive() {
UdpClient myListener = new UdpClient(myEndPoint);
byte[] herDNSQuery;
string herDNSQueryText;

while (true) {
try {
herDNSQuery = myListener.Receive(ref herEndPoint);
} catch {
Console.WriteLine("Big error (1).");
continue;
}

if( //i like the packet )
//generate a response in byte[] myDNSResponse

try {
myListener.Send(myDNSResponse, myDNSResponse.Length,
herEndPoint);
}
catch {
Console.WriteLine("Big error (2).");
continue;
}
}
}
}
}
}

--Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top