变量isn'中sockaddr_的存储大小;不知道
我有一段代码,很久以前在某些环境中使用。我很确定这是一台FreeBSD机器,所以我得到了FreeBSD8.3,我正试图创建这个文件,但它不起作用 当我试图编译它时,它会抱怨:变量isn'中sockaddr_的存储大小;不知道,c,sockets,gcc,freebsd,C,Sockets,Gcc,Freebsd,我有一段代码,很久以前在某些环境中使用。我很确定这是一台FreeBSD机器,所以我得到了FreeBSD8.3,我正试图创建这个文件,但它不起作用 当我试图编译它时,它会抱怨: 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) ... 我四处查
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)
...
我四处查看,发现这些都是在sys/socket.h文件中指定的。这是我的实际文件:
#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
...
#包括
#包括
#包括
#包括
#包括
#包括“f.h”
int tcp4(输入地址ip、int端口、int qsize)
{
套接字中的struct sockaddr\u;//第24行
插座结构sin系列=AF INET;
插座结构sin端口=htons(端口);
插座结构sin地址s地址=ip;
int actual_socket=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);//第29行
...
我觉得我的代码没有“读取”sys/socket.h文件,所以它不知道socket\u stru和IPPROTO\u TCP,但我真的迷路了
有什么想法吗?我将您的代码剪切并粘贴到一个文件中(仅删除#include f.h并关闭函数调用)。它在Linux上编译得很好 我怀疑BSD上可能存在头文件差异。对于套接字编程,我通常包括所有这些头文件。我知道我的套接字代码也在BSD上编译。我怀疑其中一个头文件引入了sockaddr_in的定义。我记得,当我通过套接字代码移植到BSD时,我必须显式地添加其中一些头文件
#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>
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
/*接下来的两个include可能与您无关,但我通常都会使用它们*/
#包括
#包括
希望这有帮助我也有同样的问题,但是下面的内容为我解决了这个问题
#include <arpa/inet.h>
#包括
其他答案对我都不起作用。在查看了sys/socket.h
文件后,我甚至在中没有看到结构sockaddr\u的定义
在使用相应的struct sockaddr.*
类型时,对我有效的方法是#包括以下文件之一:
- 如果您在
中使用的是struct sockaddr\u,包含
如果您使用的是struct sockaddr\u un
,#include
如果您使用的是struct sockaddr\u ns
,#include
如果您使用的是struct sockaddr\u ndd
,#include
有关套接字编程头文件的更多信息,请参见。根据您的需要
#包括
#包括
#包括
#包括
#包括
只需将#include
添加到您的源代码中,您就可以开始了。哇!它可以编译所有这些标题!现在开始逐个取出,直到我知道是哪一个。谢谢!!这是#include
。更多详细信息如下:
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>