18

The following code is a socket programming sample for a TCP client.

But when I run this, connect() is returned as Address family not supported by protocol.

I have heard, this problem will happen if the platform does not support ipv6.

But AF_INET I wrote is ipv4.

Also my server, that is CentOS6.4, is configured within an inet6 addr .

Does anyone know why?

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

int
main(){
    struct sockaddr_in server;
    int sock;
    char buf[32];
    int n;
    sock = socket(AF_INET,SOCK_STREAM,0);
    perror("socket");
    server.sin_family = AF_INET;
    server.sin_port = htons(12345);
    inet_pton(AF_INET,"127.0.0.1",&server,sizeof(server));
    connect(sock,(struct sockaddr *)&server,sizeof(server));
    perror("connect");
    memset(buf,0,sizeof(buf));
    n = read(sock,buf,sizeof(buf));
    perror("read");
    printf("%d,%s\n",n,buf);
    close(sock);
    return 0;
}
John Smith
  • 7,243
  • 6
  • 49
  • 61
user1345414
  • 3,745
  • 9
  • 36
  • 56

3 Answers3

8

The code passes the wrong destination address and wrong number of arguments to inet_pton(). (For the latter the compiler should have warned you about, btw)

This line

 inet_pton(AF_INET, "127.0.0.1", &server, sizeof(server));

should be

 inet_pton(AF_INET, "127.0.0.1", &server.sin_addr);

Verbatim from man inet_pton:

int inet_pton(int af, const char *src, void *dst);

AF_INET

[...] The address is converted to a struct in_addr and copied to dst, which must be sizeof(struct in_addr) (4) bytes (32 bits) long.


Not related to the problem, but also an issue, is that read() returns ssize_t not int.

The following lines shall be adjusted:

int n;
[...]
printf("%d, %s\n", n, buf);

to become:

ssize_t n;
[...]
printf("%zd, %s\n", n, buf);
alk
  • 69,737
  • 10
  • 105
  • 255
4

Set the server address like this;

addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr(host);
addr.sin_port = htons(port);
Guillaume Jacquenot
  • 11,217
  • 6
  • 43
  • 49
sundq
  • 735
  • 2
  • 9
  • 28
  • Situation’s changed to 'Connection refused'. Looks like step forward. – user1345414 Dec 04 '13 at 07:06
  • I'm sure the `Connection refused` means the server is not exist or the server down – sundq Dec 04 '13 at 07:24
  • "*Connection refused*" means the ip-address is reachable, but there is noone listening on the specified port, so the machine with the ip-address "*refuses*" to connect. – alk Dec 04 '13 at 07:32
  • My server doesn’t work. I’ll ask at another thread so that this thread will be off topic. – user1345414 Dec 04 '13 at 08:06
0

I seen this error during bind. Cause was of using localhost instead of IP:

./myprogram localhost:7777
*** exception! `bind' failed for `localhost:7777' (97, Address family not supported by protocol)

./myprogram 127.0.0.1:7777
OK! Listening...

In addition: this error happens on one Linux host and does not appear on another. I check and compare network settings on this machines (lo device, /etc/hosts, /etc/host.conf, etc) and not found essential difference

dyomas
  • 700
  • 5
  • 13