Linux socket connection failed

socket connection failure

I am beginner in socket programming and reading Linux Network Programming book. I decided to implement client-server connection as shown in the book. Server program is run on Ubuntu 14.04 machine and client code is run from Mac machine. The server code is the following

#include #include #include #include #include const char message[] = "hello, world\n"; int main() < int sock = 0; int port = 0; sock = socket(AF_INET, SOCK_STREAM, 0); if (sock == -1) fprintf(stderr, "failed\n"); else printf("connection is establisshed\n"); struct sockaddr_in server; server.sin_family = AF_INET; server.sin_addr.s_addr = htonl(INADDR_ANY ); server.sin_port = 3500; int status = bind(sock, (struct sockaddr*) &server, sizeof(server)); if (status == 0) printf("connection completed\n"); else printf("problem is encountered\n"); status = listen(sock, 5); if (status == 0) printf("app is ready to work\n"); else < printf("connection is failed\n"); return 0; >while (1) < struct sockaddr_in client = < 0 >; int sclient = 0; int len = sizeof(client); int childSocket = accept(sock, (struct sockaddr*) &client, &len); if (childSocket == -1) < printf("cannot accept connection\n"); close(sock); break; >write(childSocket, message, strlen(message)); close(childSocket); > return 0; > 
#include #include #include #include #include #include #include int main(int argc, char* argv[]) < int sock = 0; int port = 0; struct sockaddr_in servaddr; sock = socket(AF_INET, SOCK_STREAM, 0); int status = 0; char buffer[256] = ""; if (sock == -1) < printf("could not establish connection\n"); exit(1); >port = 3500; servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = inet_addr(argv[1]); servaddr.sin_port = htons(port); status = connect(sock, (struct sockaddr*) &servaddr, sizeof(servaddr)); if (status == 0) printf("connection is established successfully\n"); else < printf("could not run the app\n"); exit(1); >status = read(sock, buffer, sizeof(buffer)); if (status > 0) printf("%d: %s", status, buffer); close(sock); return 0; > 

To get IP address of client machine I run ifconfig from terminal an get inet_addr 192.168.1.165 value. Now when I pass that address string as command line argument I get message that app is not running message. There is problem with address that I got, as I understand. So what is the problem? Thanks in advance

Источник

Can not connect to Linux «abstract» unix socket

I’m trying to use UNIX sockets for inter-thread communication. The program is only intended to run on Linux. To avoid creating the socket files, I wanted to use «abstract» sockets, as documented in unix(7). However, I don’t seem to be able to connect to these sockets. Everything works if I’m using «pathname» sockets, though. Here is the code (I haven’t quoted any error handling, but it’s done): thread#1:

int log_socket = socket(AF_LOCAL, SOCK_STREAM, 0); struct sockaddr_un logaddr; socklen_t sun_len = sizeof(struct sockaddr_un); logaddr.sun_family = AF_UNIX; logaddr.sun_path[0] = 0; strcpy(logaddr.sun_path+1, "futurama"); bind(log_socket, &logaddr, sun_len); listen(log_socket, 5); accept(log_socket, &logaddr, &sun_len); . // send - receive 
struct sockaddr_un tolog; int sock = socket(AF_LOCAL, SOCK_STREAM, 0); tolog.sun_family = AF_UNIX; tolog.sun_path[0] = 0; strcpy(tolog.sun_path+1, "futurama"); connect(sock, (struct sockaddr*)&tolog, sizeof(struct sockaddr_un)); 

If all I do in the above code, is change the sun_path to not have leading \0, things work perfect. strace output:

t1: socket(PF_FILE, SOCK_STREAM, 0) = 0 t1: bind(0, , 110) t1: listen(0, 5) t2: socket(PF_FILE, SOCK_STREAM, 0) = 1 t2: connect(1, , 110 t2: ) = -1 ECONNREFUSED (Connection refused) t1: accept(0,

I know that the connect comes before accept, that’s not an issue (I tried making sure that accept() is called before connect(), same result. Also, things are fine if the socket is «pathname» anyway).

Читайте также:  Kali linux twin evil

For communication between threads of the same process, an ordinary pipe(2) should be enough! And you could also use pipes if all the communicating processes and/or threads have the same parent process!

@BasileStarynkevitch pipe will not work in my case. I need multiple threads to send info, and receive a synchronous response before moving on.

@BasileStarynkevitch for this, I will have to know in advance how many maximum pipes to open, or limit access to one using locks. The socket approach has less overhead for such case.

4 Answers 4

While I was posting this question, and re-reading unix(7) man page, this wording caught my attention:

an abstract socket address is distinguished by the fact that sun_path[0] is a null byte (’\0’). All of the remaining bytes in sun_path define the «name» of the socket

So, if I bzero’ed the sun_path before filling in my name into it, things started to work. I figured that’s not necessarily straight-forward. Additionally, as rightfully pointed out by @davmac and @StoneThrow, the number of those «remaining bytes» can be reduced by specifying only enough length of the socket address structure to cover the bytes you want to consider as your address. One way to do that is to use SUN_LEN macro, however, the first byte of the sun_path will have to be set to !0, as SUN_LEN uses strlen .

If sun_path[0] is \0, The kernel uses the entirety of the remainder of sun_path as the name of the socket, whether it’s \0-terminated or not, so all of that remainder counts. In my original code I would zero the first byte, and then strcpy() the socket name into the sun_path at position 1. Whatever gibberish that was in sun_path when the structure was allocated (especially likely to contain gibberish since it’s allocated on the stack), and was included in the length of the socket structure (as passed to the syscalls), counted as the name of the socket, and was different in bind() and connect().

Читайте также:  Realtek 8169 linux driver

IMHO, strace should fix the way it displays abstract socket names, and display all the sun_path bytes from 1 to whatever the structure length that was supplied, if sun_path[0] is 0

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

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

Socket.Connection refused on linux and osx #923

area-System.Net.Sockets bug os-linux Linux OS (any supported distro) tenet-compatibility Incompatibility with previous versions or .NET Framework

Comments

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

[Fact] public static void UDP_MultipleSends()  var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); socket.Connect(new IPEndPoint(IPAddress.Loopback, 12345)); for (int i = 0; i  100; i++)  socket.Send(Encoding.ASCII.GetBytes("hello world")); > >
Operating System Details Distributor ID: Ubuntu Description: Ubuntu 14.04.5 LTS Release: 14.04 Codename: trusty 
instance: 6963e4a4-daa1-404d-b285-a2db097e200f travis-ci-macos10.12-xcode8.3-1507738863 (via amqp) 
Error Message: System.Net.Sockets.SocketException : Connection refused 

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

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

It seems like the error is coming from OS:

strace -f -e network ~/dotnet-2.1.302/dotnet run [pid 28081] connect(26, , 16) = 0 Connect OK [pid 28081] sendmsg(26, ], msg_controllen=0, msg_flags=0>, 0) = 11 [pid 28081] sendmsg(26, ], msg_controllen=0, msg_flags=MSG_DONTWAIT|MSG_WAITALL|MSG_CONFIRM|MSG_ERRQUEUE|MSG_NOSIGNAL|MSG_WAITFORONE|0x85500000>, 0) = -1 ECONNREFUSED (Connection refused) 

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.

[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.
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:

furt@Ubuntu:~/git/wfurt-corefx-serial/src/System.Diagnostics.Process/src$ sudo tcpdump -eni lo [sudo] password for furt: tcpdump: verbose output suppressed, use -v or -vv for full protocol decode listening on lo, link-type EN10MB (Ethernet), capture size 262144 bytes 14:48:23.512034 00:00:00:00:00:00 > 00:00:00:00:00:00, ethertype IPv4 (0x0800), length 53: 127.0.0.1.46992 > 127.0.0.1.12345: UDP, length 11 14:48:23.512043 00:00:00:00:00:00 > 00:00:00:00:00:00, ethertype IPv4 (0x0800), length 81: 127.0.0.1 > 127.0.0.1: ICMP 127.0.0.1 udp port 12345 unreachable, length 47 

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.

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)

Источник

UNIX socket connection refused

Under OS-X, I’ve got process named ‘listener’ that is waiting on ‘accept’ to read data from local unix socket named listener_socket. unfortunately, any attempt to connect that socket terminate in ‘connection refused’ error. Using lsof, to make sure that the ‘listener’ actually listen to this socket :

sudo lsof -p 570 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME . listener 570 root 3u unix 0x48a2751a1bad61ef 0t0 /private/var/run/my_sockets/listener_socket 
file /private/var/run/my_sockets/listener_socket /private/var/run/my_sockets/listener_socket: socket 

However, it still fail to connect, even when i’m using an alternative way from command like (using socat command)

sudo socat LOCAL:/private/var/run/my_sockets/listener_socket,interval=1 EXEC:'aaaaaaaaaaaaaaaaa',nofork 2015/11/23 00:57:33 socat[928] E connect(3, LEN=49 AF=1 "/private/var/run/my_sockets/listener_socket", 49): Connection refused 

perhaps there are more i can do to figure out why i cannot send data to the socket, even-though it’s obvious that ‘listener’ waiting for this data on the other side ? here’s the relevant part of my code : sender:

sockfd = socket(PF_UNIX, SOCK_STREAM, 0); address.sun_family = AF_UNIX; snprintf(address.sun_path, UNIX_PATH_MAX, "%s", LISTENER_SOCKET_PATH); connect(sockfd, (struct sockaddr *) &address, sizeof(struct sockaddr_un) write . 
fd = socket(PF_UNIX, SOCK_STREAM, 0); unlink(sock_name); // in case the socket is used before listen(server->fd, 5); // we don't reach 5 listeners for sure . chmod(sock_name, mode); // giving root privilages accept(server->fd, (struct sockaddr *) &server->address, &server->address_length); read . 

Источник

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