OpenSUSE sockets not working on Debian?

I have a C / C ++ TCP client running on OpenSUSE but not on Debian. I am using nc -l 4242 for the server. Then I connect to. / My _client 127.0.0.1 4242 on my Debian (Sid) system, and it does not work when using the connect function.

Can you check if you have the same error using Debian or possibly another OS? Where does the problem come from?

Here is the code:

#include <sys/types.h> #include <sys/socket.h> #include <arpa/inet.h> #include <netdb.h> #include <unistd.h> #include <stdlib.h> #include <string.h> #include <iostream> void do_server(int s) { write(s, "client connected\n", strlen("client connected\n")); close(s); } int main(int ac, char **av) { struct protoent *pe; struct sockaddr_in sin; int s; if (ac != 3) { std::cerr << "Usage: ./client ip port" << std::endl; return EXIT_FAILURE; } pe = getprotobyname("TCP"); if ((s = socket(AF_INET, SOCK_STREAM, pe->p_proto)) == -1) { std::cerr << "Error: socket" << std::endl; return EXIT_FAILURE; } sin.sin_family = AF_INET; sin.sin_port = htons(atoi(av[2])); sin.sin_addr.s_addr = inet_addr(av[1]); if (connect(s, (const struct sockaddr *)&sin, sizeof(sin)) == -1) { std::cerr << "Error: connect" << std::endl; close(s); return EXIT_FAILURE; } std::cout << "client started" << std::endl; do_server(s); return EXIT_SUCCESS; } 
+1
source share
1 answer

This seems to be related to your chosen netcat flavor.

Using the "traditional" netcat ( /etc/alternatives/nc links to /bin/nc.traditional ) you should use this syntax to specify the listening port:

 nc -l -p 4242 

netcat 'openbsd' also supports this syntax (as well as the one you used), although the manual page says that you cannot use -l and -p together.

+4
source

All Articles