1

I use buildroot cross toolchain to compile Raspberry application from my computer (Ubuntu X86). I'm developping a TCP serveur that allows a connection on 5003 (0x138B) TCP port number. When I'm start the server, that's right but my server wait a connection on 35603 (0x8B13) TCP port number (check with netstat -a).

It seems to be an endianness problem but I don't know how to resolve.

Can you help me please?

Thanks.

Totoscill
  • 19
  • 1

1 Answers1

0

thanks for your answer.

I agree it's very strange. I don't think the code is the problem. It's working well on other platform.

Please find the code below:

/*Create the server */
int CreateServeur (unsigned short port, int *sock_srv, int nb_connect)  
{
  int l_ret = -1;   
  struct sockaddr_in l_srv_addr;  

  /* address initialisation */
  printf("creation serveur port %i\n", port);
  memset ((char*) &l_srv_addr,0, sizeof (struct sockaddr_in));
  l_srv_addr.sin_family      = PF_INET;
  l_srv_addr.sin_port        = port;
  l_srv_addr.sin_addr.s_addr = htonl (INADDR_ANY);

  /* main socket creation */
  if ((*sock_srv = socket (PF_INET, SOCK_STREAM, 0)) <= 0)
  {
    printf("server socket creation error");
  }
  else
  {
    if (bind (*sock_srv, (struct sockaddr *) &l_srv_addr, sizeof (struct sockaddr_in)) == -1)
    {
        close (*sock_srv);
        printf("bind socket error");
    }
    else
    {
        if (listen (*sock_srv, nb_connect) == ERROR)
        {
            close (*sock_srv);
            printf("listen socket error");
        }
        else
        {
            l_ret = 0;
        }
    }
  }

  return (l_ret);
}

This function doesn't return any error. The first log (printf("creation serveur port %i\n", port);) display the good port (5003) but the server wait connexion on port 35603 (netstat -a).

If it's not a endianness problem, I don't understand.

Totoscill
  • 19
  • 1
  • You need to translate your data from network packet endianness (big endian) to host (depends on the architecture). (See `ntohl` and `htonl`) – toasted_flakes Jan 05 '14 at 11:44
  • I'm not received data from network because connection failed on 5003 port. Server port is fixed. The problem is the server doesn't wait connection on the good port (waiting on port 35603). – Totoscill Jan 05 '14 at 15:26
  • `ntohs(35603) == 5003`. Draw appropriate conclusions – toasted_flakes Jan 05 '14 at 15:38
  • I understand your result but not why I must use "ntohs" function even if this solution correct my problem. In my opinion, byte swapping is only used for network data receiving. Could you confirm? – Totoscill Jan 05 '14 at 17:47
  • For the 3rd time, you need to use it to send and to receive data. That's how TCP works. Go read about it – toasted_flakes Jan 05 '14 at 18:08
  • OK. Thanks a lot. I will use this solution. – Totoscill Jan 05 '14 at 18:19
  • @grasGendarme That's not correct. You do not *need* to use it to send and receive data. However, the socket API that uses `l_srv_addr.sin_port` expects the port to be given in a big (or "network") endian format regardless of the host endian, and that's why you need to do `l_srv_addr.sin_port = htons(port)` – nos Jan 07 '14 at 14:45
  • @nos Yeah that's what I meant, you need to use endianness conversion to be able to make the connection. – toasted_flakes Jan 07 '14 at 14:51