By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,152 Members | 2,159 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,152 IT Pros & Developers. It's quick & easy.

UDPListener: System.Net.Sockets.SocketException

P: n/a
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
j@fourtentech.com
Apr 3 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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
"J C" <j@fourtentech.com> wrote in message
news:OG**************@TK2MSFTNGP09.phx.gbl...
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
j@fourtentech.com

Apr 3 '06 #2

P: n/a
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
Apr 3 '06 #3

P: n/a
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

"Vadym Stetsyak" <va*****@ukr.net> wrote in message
news:ed**************@tk2msftngp13.phx.gbl...
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

Apr 3 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.