Storage size of sockaddr_in variable isn't known

c, freebsd, gcc, sockets

Solution

I cut and paste your code into a file (removing only the #include f.h and closed off the function call.) It compiles just fine on Linux.

I suspect there may be header files differences on BSD. For socket programming, I typically include ALL these header files. And I know my socket code compiles on BSD as well. I suspect one of these header files brings in the definition for sockaddr_in. I recall when I ported by socket code to BSD, I had to explicitly add a few of these.

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/time.h>
#include <stdlib.h>
#include <memory.h>
#include <ifaddrs.h>
#include <net/if.h>
#include <stdarg.h>
/* the next two includes probably aren't relevant for you, but I typically use them all anyway */
#include <math.h>
#include <sys/termios.h>

Hope this helps

Problem

I have a piece of code that used to work in some environment a long time ago. I'm pretty sure it was a FreeBSD machine so I got FreeBSD 8.3 and I'm trying to make this file but it's not working. When I try to compile it it complains with: ``` f.c: In function 'tcp'> f.c:24: error: storage size of 'socket_stru' isn't known f.c:29: error: 'IPPROTO_TCP' undeclared (first use in this function) ... ``` I've been looking around and I see these are all specified in the sys/socket.h file. This is my actual file: ``` #include <stdio.h> #include <string.h> #include <netdb.h> #include <sys/socket.h> #include <unistd.h> #include "f.h" int tcp4 (in_addr_t ip, int port, int qsize ) { struct sockaddr_in socket_stru; // line 24 socket_stru.sin_family = AF_INET; socket_stru.sin_port = htons(port); socket_stru.sin_addr.s_addr = ip; int actual_socket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); // line 29 ... ``` I feel like my code somehow doesn't "read" the sys/socket.h file so it doesn't know about socket_stru and IPPROTO_TCP, but I'm just really lost. Any ideas?

Original source