Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sockaddr_in undeclared identifier

Tags:

c

sockets

I am following along with beej's guide to networking and it's been going REALLY good because I understand everything very well and he explains it great. however, when I want to test out some of the cool things he's showing me, it won't work!. :(

I am not sure where exactly sockaddr_in is declared but maybe somebody here will so help me out!

Here is what I have so far (testing out converting an ip in string form to an ip to a 4 byte integral and vice versa):

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>

int main(void)
{
  sockaddr_in sin;

  inet_pton(AF_INET, "192.168.2.1", &sin.in_addr);
  char ip[INET_ADDRSTRLEN];
  inet_ntop(AF_INET, &sin.in_addr, ip, INET_ADDRSTRLEN);
  printf("%s\n", ip);

  return 0;
}

Again I am completely clueless as to where everything is so if it's something really stupid on my part, sorry!

Edit: I'm on a Linux Debian distro called Mint if that helps at all?

like image 723
user1169094 Avatar asked Feb 01 '12 20:02

user1169094


1 Answers

The standard says:

The header shall define the sockaddr_in structure

You have to include <netinet/in.h> and declare sin like this:

struct sockaddr_in sin;
^^^^^^

To find out why you need the struct keyword, see this C FAQ. Long story short, there's no implicit typedef in C.

like image 143
cnicutar Avatar answered Sep 20 '22 04:09

cnicutar