C lang-Solaris 8-64-bit Socket Program 128-Network unreachable error

Posted on 2007-10-15
Last Modified: 2013-12-27
I have a simple C socket program/function that works fine when compiled in 32-bit mode, but when compiled in 64-bit mode and then ran the connect function returns a 128-Network unreachable error all the time.  OS is Solaris 8.

The module is compiled with the command:  cc -xarch=v9 -c tcps.c -lnsl -lsocket
The compiler is Sun Forte
The output of the compile is:
"tcps.c", line 72: warning: implicit function declaration: readn
"tcps.c", line 180: warning: implicit function declaration: inet_addr
"tcps.c", line 207: warning: implicit function declaration: close

file tcps.o
tcps.o:         ELF 64-bit MSB relocatable SPARCV9 Version 1

The program using the tcps.o module is also a 64-bit.

An excerpt of the source with the includes and open socket (connect function is the one failing) is below.

#include <sys/utsname.h>
#include <netinet/in.h>
#include <netdb.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <sys/socket.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <inet/tcp.h>
#include <errno.h>

#define  INADDR_NONE    0xffffffff

#define MAX_PACKET  1500

#define HOSTNAMELEN     32

int tcp_client_open (char *host, char *service, int port)
  int fd;
  int on = 1;
  unsigned long inaddr;
  struct hostent tcp_host_info;
  struct hostent *hp;
  struct servent *sp;
  struct sockaddr_in tcp_srv_addr;
  struct servent tcp_serv_info;
   * Initialise the address structure for the server
  memset ((char *) &tcp_srv_addr, '\0', sizeof (tcp_srv_addr));
  tcp_srv_addr.sin_family = AF_INET;
  /* determine port info */
  if (service != NULL)
    if ((sp = getservbyname (service, "tcp")) == NULL)
      fprintf (stderr, "Specified Service Undefined: %s\n", service);
      return (-1);
    tcp_serv_info = *sp;
    if (port > 0)
      tcp_srv_addr.sin_port = htons (port);
      tcp_srv_addr.sin_port = sp->s_port;
    if (port <= 0)
      fprintf (stderr, "tcp_open(): Must specify service or port no.\n");
      return (-1);
    tcp_srv_addr.sin_port = htons (port);
  /* determine host address */
  if ((inaddr = inet_addr (host)) != INADDR_NONE)
    memcpy ((char *) &tcp_srv_addr.sin_addr, (char *) &inaddr, sizeof (inaddr));
    tcp_host_info.h_name = NULL;
    if ((hp = gethostbyname (host)) == NULL)
      fprintf (stderr, "Invalid Host Name: %s\n", host);
      return (-1);
    tcp_host_info = *hp;
    memcpy ((char *) &tcp_srv_addr.sin_addr, hp->h_addr, hp->h_length);
  /* connect to host/port */
  if ((fd = socket (AF_INET, SOCK_STREAM, 0)) < 0)
    fprintf (stderr, "Socket Open Failed: %d-(%i)%s\n",fd,errno,strerror(errno));
    return (-1);
  if(connect (fd, (struct sockaddr *) &tcp_srv_addr, sizeof (tcp_srv_addr)) < 0)
    fprintf (stderr, "Connection failed-(%i) %s\n",errno,strerror(errno));
    close (fd);
    return (-1);
  return (fd);

Any help or suggestions would be great.
Question by:mjwest042400
    LVL 40

    Expert Comment


    check if your os supports 64 bits applications:

    use isainfo -kv

    Please see the link


    Author Comment

    Yes it does, the same program uses the 64-bit Oracle client and that part of the application is Ok as well.

    isainfo -kv
    64-bit sparcv9 kernel modules
    LVL 22

    Accepted Solution

    Your problem is that you do not have all of the include files necessary. You need to pay heed
    to compiler warnings, that's why they are there, to warn you.

    You do not have the include file  <arpa/inet.h> which is needed for the inet_addr function.  The compiler
    takes the default calling sequence since it does not know what the proper prototype is for the function.
    In 32 bit mode the default happens to match the actual calling sequence, but in 64 bit it does not. This
    means that when it returns a -1 indicating an error, the test against INADDR_NONE fails, and you end
    up using the address ff.ff.ff.ff  for the destination. So when the connect call is made it rejects the
    address with a network unreachable.

    Author Comment

    Adding the arpa/inet.h include resolved the compiler warning, inet_addr still was not working properly though.  I did however end up removing the inet_addr usage as it was not strictly needed.  Thank you.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. Please see for the updated article. It is avail…
    This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
    Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
    Video by: Grant
    The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now