-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Socket.Connection refused on linux and osx #923
Comments
cc: @wfurt |
Do you have anything listening on that port @dv00d00 ? |
It seems like the error is coming from OS:
When sending data locally, kernel can probably detect that port is closed and the sendmsg() call fails. That does not looks like problem with the runtime, more difference in how OS handle I/O. |
On the other hand [Fact]
public static void UDP_MultipleSends()
{
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
var ep = new IPEndPoint(IPAddress.Loopback, 12345);
for (int i = 0; i < 100; i++)
{
socket.SendTo(Encoding.ASCII.GetBytes("hello world"), ep);
}
} works without throwing exceptions. I don't believe anything is on that specific port, this was happening on a travis CI machine, but the same happened on my mac. |
I did more testing with C and C# as well as I looked at Linux kernel code. I did also packet capture for both calls. In both cases I see:
When sendmsg() is trying to send data following happens: first message goes out without error. Since this is OS behavior, I don't think it make sense to hide underlying error. I'm proposing to close this unless somebody objects. cc: @karelz |
Hi all, please take a look at this same issue over on the NpgSql repo - npgsql/npgsql#2198. I'm experiencing the same thing. And their code is using the same Socket class to make the connections. You can look at the relevant Connect method code here: https://github.com/npgsql/npgsql/blob/91d23f90ef00eadc7c07966833959a5b3f877127/src/Npgsql/NpgsqlConnector.cs#L642. "System.Net.Sockets.SocketException (111): Connection refused" error, I've run
|
From the dump it is clear that the port you trying to connect to is not listening and sends back RST. It is correct to throw. |
Triage: We need to understand why |
I'm only just now getting into this as well and it looks like |
This issue is unrelated to On Linux, Windows
Maybe this error is only generated for non-localhost communication on Windows? You can add an extension method if you always want to ignore the error, and avoid the cost of throwing (and catching) Exceptions for it. static class SocketExtensions
{
public static int SendUdp(this Socket socket, ReadOnlySpan<byte> buffer)
{
int rv = socket.Send(buffer, SocketFlags.None, out SocketError socketError);
return socketError switch
{
SocketError.Success => rv,
SocketError.ConnectionRefused => 0,
_ => throw new SocketException((int)socketError),
};
}
} |
Due to lack of recent activity, this issue has been marked as a candidate for backlog cleanup. It will be closed if no further activity occurs within 14 more days. Any new comment (by anyone, not necessarily the author) will undo this process. This process is part of our issue cleanup automation. |
This issue will now be closed since it had been marked |
Not sure if it is a bug but this code works perfectly fine on windows:
but fails on linux
and OSX
dotnet version: 2.1.302
Related fact
SendTo
behaves differently - see https://github.com/dotnet/corefx/issues/31206#issuecomment-406515367The text was updated successfully, but these errors were encountered: