472,138 Members | 1,646 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,138 software developers and data experts.

UDPListener: System.Net.Sockets.SocketException

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
3 11424
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
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
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.

Similar topics

reply views Thread by Tim Wagaman | last post: by
2 posts views Thread by Steve Lowe | last post: by
1 post views Thread by larspeter | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.