1
votes

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.

1
Learn about ntohs and ntohl and friends in your code. - bmargulies
Then your code is the problem...it is not "endian-safe" (don“t know if this is the correct english term) - deviantfan
Please show the code that sets up the socket. - nos
I add an answer below. - Totoscill

1 Answers

0
votes

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.