C - socket programming client server - connection on host name

On my server I have this code at the moment:

      #define h_addr h_addr_list[0]

      serverAddr.sin_port = htons(port);

      /* Set IP address to localhost */
      hostname[1023] = "\0";
      gethostname(hostname, 1023);
      printf("HostName: %s\n", hostname); // this one prints correctly

      my_hostent = gethostbyname(hostname);
      printf("Host: %s\n", my_hostent->h_addr);
      printf("IP: %c\n", inet_ntoa(my_hostent->h_addr));
      serverAddr.sin_addr.s_addr = *hostname;

And on client side, I have it that you must write the host as parameter so I could write -h www.abc.com in this example I let myself say that my server hosts on www.abc.com also, but they never communicate at the moment, but when I print the hostname it says the same.

client code.

#define h_addr h_addr_list[0]

struct hostent *server;

server = gethostbyname(hostname);
serverAddr.sin_addr.s_addr = server->h_addr;

the "hostname" variable is the parameter from program start.

this is client error:

 warning: assignment makes integer from pointer without a cast
   serverAddr.sin_addr.s_addr = server->h_addr;

this is server errors:

server.c:42:18: warning: assignment makes integer from pointer without a cast
   hostname[1023] = "\0";
                  ^
server.c:43:3: warning: implicit declaration of function ‘gethostname’ [-Wimplicit-function-declaration]
   gethostname(hostname, 1023);
   ^
server.c:48:3: warning: implicit declaration of function ‘inet_ntoa’ [-Wimplicit-function-declaration]
   printf("IP: %c\n", inet_ntoa(lol->h_addr));
   ^

Can anyone see what my failure is with sockets and connecting them together?

At the moment if I set both sides to INADDR_ANY it will work and automatically connect,

Upvotes: 3

Views: 8769

Answers (2)

dbush
dbush

Reputation: 223872

The problem is that serverAddr.sin_addr.s_addr is a uint32_t and server->h_addr is a char *.

The h_addr field is actually an alias for h_addr_list[0], where h_addr_list is a char **. This field points to an array of address structures, which can be either struct in_addr or struct in6_addr.

In the case of gethostbyname, it will be a struct in_addr, so you need to cast it to that and assign it to serverAddr.sin_addr instead of serverAddr.sin_addr.s_addr:

serverAddr.sin_addr = *((struct in_addr *)server->h_addr);

This is not a valid declaration:

hostname[1023] = "\0";

What you want is this:

char hostname[1023] = {0};

This will initialize the whole array to zeros.

Upvotes: 4

John Bollinger
John Bollinger

Reputation: 180201

server.c:42:18: warning: assignment makes integer from pointer without a cast
   hostname[1023] = "\0";
                  ^

The "\0" is a string literal representing an array of two const chars, both zero. In your assignment statement, as in most other contexts, the expression is converted to a pointer to the first char. Evidently, hostname is a char array or char *, so that hostname[1023] is an lvalue representing a single char. You are trying to assign a char pointer to that char.

You want a char literal instead:

hostname[1023] = '\0';

or, equivalently, just

hostname[1023] = 0;
server.c:43:3: warning: implicit declaration of function ‘gethostname’ [-Wimplicit-function-declaration]
   gethostname(hostname, 1023);
   ^
server.c:48:3: warning: implicit declaration of function ‘inet_ntoa’ [-Wimplicit-function-declaration]
   printf("IP: %c\n", inet_ntoa(lol->h_addr));
   ^

You have failed to #include the headers declaring functions gethostname() and inet_ntoa(). On a POSIX system, those would be

#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

Upvotes: 3

Related Questions