C语言 sockaddr_in 变量的存储大小未知

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16154530/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:08:07  来源:igfitidea点击:

Storage size of sockaddr_in variable isn't known

csocketsgccfreebsd

提问by coconut

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.

我有一段代码,很久以前曾在某些环境中工作过。我很确定它是一台 FreeBSD 机器,所以我得到了 FreeBSD 8.3,我正在尝试制作这个文件,但它不起作用。

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:

我环顾四周,发现这些都在 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
...

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.

我觉得我的代码不知何故没有“读取” sys/socket.h 文件,所以它不知道 socket_stru 和 IPPROTO_TCP,但我真的迷路了。

Any ideas?

有任何想法吗?

采纳答案by selbie

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.

我将您的代码剪切并粘贴到一个文件中(仅删除了 #include fh 并关闭了函数调用。)它在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.

我怀疑 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>

Hope this helps

希望这可以帮助

回答by aanrv

None of the other answers worked for me. After taking a look inside the sys/socket.hfile, I didn't even see a definition for struct sockaddr_in.

其他答案都不适合我。在查看sys/socket.h文件内部后,我什至没有看到struct sockaddr_in.

What worked for me was to #includeone of the following files when using the corresponding struct sockaddr_*type:

#include在使用相应struct sockaddr_*类型时,对我有用的是以下文件之一:

  • if you're using struct sockaddr_in, #include <netinet/in.h>
  • if you're using struct sockaddr_un, #include <sys/un.h>
  • if you're using struct sockaddr_ns, #include <netns/ns.h>
  • if you're using struct sockaddr_ndd, #include <sys/ndd_var.h>
  • 如果你正在使用struct sockaddr_in#include <netinet/in.h>
  • 如果你正在使用struct sockaddr_un#include <sys/un.h>
  • 如果你正在使用struct sockaddr_ns#include <netns/ns.h>
  • 如果你正在使用struct sockaddr_ndd#include <sys/ndd_var.h>

More information on the header files for socket programming can be found here.

可以在此处找到有关套接字编程头文件的更多信息。

回答by slmyers

I had the same problem, but the following include fixed the issue for me

我遇到了同样的问题,但以下内容为我解决了这个问题

#include <arpa/inet.h>

回答by jetman

Just add #include <resolv.h>to your source and you are good to go.

只需添加#include <resolv.h>到您的来源,您就可以开始了。

回答by pmg

According to freebsd developer's handbookyou need

根据您需要的freebsd开发人员手册

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