Unix socket connect error connection refused

Socket.Connection refused on linux and osx #923


dv00d00 commented Jul 19, 2018

Not sure if it is a bug but this code works perfectly fine on windows:

but fails on linux

dotnet version: 2.1.302

Related fact

The text was updated successfully, but these errors were encountered:

davidsh commented Jul 19, 2018

wfurt commented Jul 19, 2018

Do you have anything listening on that port @dv00d00 ?
You may run tcpdump -ni lo port 12345 (or strace)

wfurt commented Jul 19, 2018

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.

dv00d00 commented Jul 20, 2018

On the other hand

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.

wfurt commented Sep 19, 2018

I did more testing with C and C# as well as I looked at Linux kernel code.
This seems to be way how Unix works. when sendto() is used, individual chunks of data are submitted independently and the call succeeds as long as there is space in socket buffer.
Since UDP is unreliable, this is has nothing to do with actual delivery.

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.
When the ICMP error get’s back it is remembered on «connection» (internal socket structure)
Subsequent sendmsg() calls fail.

Читайте также:  An error occurred with the sql server mysql

Since this is OS behavior, I don’t think it make sense to hide underlying error.
It seems that raising exception and allowing caller to deal with it is better approach.

I’m proposing to close this unless somebody objects. cc: @karelz
(note that linked PR does not change this behavior)

Krummelz commented Sep 2, 2019 •

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,
at Npgsql.NpgsqlConnector.Connect(NpgsqlTimeout timeout)

I’ve run tcpdump -ni lo port 5432 , and I get the following:

wfurt commented Sep 2, 2019

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.
Also note that this issue is about UDP.

karelz commented Sep 9, 2019

Triage: We need to understand why Send and SendTo behave differently.
We need either doc change, or product change, or test added.


Unix socket connect error connection refused

and my server code is

is there any mistake in my programs? if it is so, pls kindly point out for me.

The server is listening for connections (with listen() ), but it also needs to accept new connections with accept() .

accept() returns a new socket that you use to communicate with the client. You read/write on it then close it when the session is done.

Читайте также:  Borderlands 3 fatal error ue4

accept() is typically called in a loop. By default it will block until a client connects.

You should specify the protocol in: socket(AF_INET, SOCK_STREAM,0)

The BSD socket library attepts to fill in the protocol for you if you specify 0. But WinSock won’t for example. So you may as well just say what it is. Asl it happens, PF_INET has the same value as AF_INET, but socket() really takes PF_INET, so you should use that. It should be: socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)

It’s always best to zero out the address structures before using them. You did that, but comment out the code.

You shouldn’t use htol on INADDR_ANY. So this is wrong. serv_addr.sin_addr.s_addr=htonl(INADDR_ANY);

I can’t really see anything else that’s wrong other than the missing accept() lopp/client processing.

Did you set the protocol?

Did you remove that htonl on INADDR_ANY?

I used

and I removed, htonl(INADDR_ANY);

now I use
int res;
res = inet_pton(AF_INET,»″, &serv_addr,sin_addr);

is it correct? still got same address family not supported by protocol.


Оцените статью