Sockets working in openSUSE do not work in Debian?
Asked Answered
P

1

1

I have a C/C++ TCP client working in OpenSUSE but not in Debian. I'm using nc -l 4242 for the server. Then I connect with ./my_client 127.0.0.1 4242 on my Debian system (Sid) and it will fail when using the connect function.

Can you confirm if you have the same error too, using Debian or maybe another OS? Where does the problem come from?

Here's 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;
}
Pinstripe answered 10/3, 2013 at 2:12 Comment(8)
It works on my system running sid.Grapher
@SamuelEdwinWard It will print "Error: connect" because the connect function will fail.Pinstripe
Try adding strerror(errno) to your error output to determine which error connect is getting.Bivalent
@Grapher thanks for the reply. Do I have to install additional packages in Debian in order to work with sockets? Or maybe add permissions? serverfault.com/questions/390153/…Pinstripe
are you sure netcat has bound to 127.0.0.1? Use netstat to checkInterglacial
Problems caused by a missing package would show up during the compile or when starting the prograom, it wouldn't get that far. The permissions issue in that linked question would show up from netcat not being able to bind to the requested port. It could be an issue with firewall rules. Have you tried using netcat as the client as well to narrow down where the problem is?Grapher
@SamuelEdwinWard strerror(errno) gives me "Connection refused"Pinstripe
@SamMiller netstat is printing addresses like debian-2.local:44640 and 10.14.59.46:39989. Is that what you where asking?Pinstripe
B
4

This seems to have to do with the netcat flavor you have selected.

With the 'traditional' netcat (/etc/alternatives/nc links to /bin/nc.traditional) you have to use this syntax to specify the listening port:

nc -l -p 4242

The 'openbsd' netcat also supports this syntax (as well as the one you used) even though it's man page says you can't use -l and -p together.

Bivalent answered 10/3, 2013 at 3:15 Comment(5)
Thanks, that was the solution! Do you know what I have to change in order to use nc with -l option only?Pinstripe
@Pinstripe ask another question for that topicInterglacial
@SamMiller is it really necessary? It's still related to my original question using nc -l port_number.Pinstripe
@Pinstripe yes it is necessary, you have already accepted this answer as it solves your question. Please ask a new question, read the FAQ for more information.Interglacial
@SamMiller Here it is: #15319280Pinstripe

© 2022 - 2024 — McMap. All rights reserved.