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?
什么好主意吗?
5 个解决方案
#1
6
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.
我将您的代码剪切并粘贴到一个文件中(只删除包含f的代码)。h关闭函数调用。它在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上可能有头文件的差异。对于套接字编程,我通常包括所有这些头文件。我知道我的socket代码也编译在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
希望这有助于
#2
6
None of the other answers worked for me. After taking a look inside the sys/socket.h
file, I didn't even see a definition for struct sockaddr_in
.
其他的回答都不适合我。看了看里面的系统/插座。h文件,我甚至没有看到struct sockaddr_in的定义。
What worked for me was to #include
one of the following files when using the corresponding struct sockaddr_*
type:
在使用相应结构的sockaddr_*类型时,#包含以下文件中的一个:
- if you're using
struct sockaddr_in
,#include <netinet/in.h>
-
如果您使用的是struct sockaddr_in, #include
。 - if you're using
struct sockaddr_un
,#include <sys/un.h>
-
如果您使用的是struct sockaddr_un,则包括
。 - if you're using
struct sockaddr_ns
,#include <netns/ns.h>
-
如果您使用的是struct sockaddr_ns,则包括
。 - if you're using
struct sockaddr_ndd
,#include <sys/ndd_var.h>
-
如果您使用的是struct sockaddr_ndd,则包括
。
More information on the header files for socket programming can be found here.
关于套接字编程的头文件的更多信息可以在这里找到。
#3
4
I had the same problem, but the following include fixed the issue for me
我也有同样的问题,但是下面的问题包括了我的问题。
#include <arpa/inet.h>
#4
4
Just add #include <resolv.h>
to your source and you are good to go.
只需添加# include <决心。h> 到你的来源,你很好。
#5
2
According to freebsd developer's handbook you need
根据freebsd开发人员手册,您需要。
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#1
6
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.
我将您的代码剪切并粘贴到一个文件中(只删除包含f的代码)。h关闭函数调用。它在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上可能有头文件的差异。对于套接字编程,我通常包括所有这些头文件。我知道我的socket代码也编译在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
希望这有助于
#2
6
None of the other answers worked for me. After taking a look inside the sys/socket.h
file, I didn't even see a definition for struct sockaddr_in
.
其他的回答都不适合我。看了看里面的系统/插座。h文件,我甚至没有看到struct sockaddr_in的定义。
What worked for me was to #include
one of the following files when using the corresponding struct sockaddr_*
type:
在使用相应结构的sockaddr_*类型时,#包含以下文件中的一个:
- if you're using
struct sockaddr_in
,#include <netinet/in.h>
-
如果您使用的是struct sockaddr_in, #include
。 - if you're using
struct sockaddr_un
,#include <sys/un.h>
-
如果您使用的是struct sockaddr_un,则包括
。 - if you're using
struct sockaddr_ns
,#include <netns/ns.h>
-
如果您使用的是struct sockaddr_ns,则包括
。 - if you're using
struct sockaddr_ndd
,#include <sys/ndd_var.h>
-
如果您使用的是struct sockaddr_ndd,则包括
。
More information on the header files for socket programming can be found here.
关于套接字编程的头文件的更多信息可以在这里找到。
#3
4
I had the same problem, but the following include fixed the issue for me
我也有同样的问题,但是下面的问题包括了我的问题。
#include <arpa/inet.h>
#4
4
Just add #include <resolv.h>
to your source and you are good to go.
只需添加# include <决心。h> 到你的来源,你很好。
#5
2
According to freebsd developer's handbook you need
根据freebsd开发人员手册,您需要。
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>