- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
在 C 语言中,绑定(bind)
Socket 的典型方式如下:
int server_socket_fd = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in addr;
int port_number = 55555;
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
addr.sin_port = htons(port_number);
int result = bind(server_socket_fd,(struct sockaddr *)&addr , sizeof(addr));
if(bind_result > 0)
{
// Stuff
}
我想知道为什么从 sockaddr_in
到 sockaddr
的转换有效,因为我找不到任何说明它有效的文档。似乎每个人都这样做。
为什么类型转换在这里起作用?
我不是在问我们为什么投它,这已经被回答了here .我在问为什么它有效。
最佳答案
允许将结构指针转换为不同的结构指针并返回。这在 C standard 的第 6.3.2.3p7 节中有详细说明。 :
A pointer to an object type may be converted to a pointer to a different object type. If the resulting pointer is not correctly aligned for the referenced type, the behavior is undefined. Otherwise, when converted back again, the result shall compare equal to the original pointer. When a pointer to an object is converted to a pointer to a character type, the result points to the lowest addressed byte of the object. Successive increments of the result, up to the size of the object, yield pointers to the remaining bytes of the object.
上述段落中关于对齐的限制在 6.2.5p28 节中有进一步详细说明:
A pointer to void shall have the same representation and alignment requirements as a pointer to a character type.48) Similarly, pointers to qualified or unqualified versions of compatible types shall have the same representation and alignment requirements. All pointers to structure types shall have the same representation and alignment requirements as each other. All pointers to union types shall have the same representation and alignment requirements as each other. Pointers to other types need not have the same representation or alignment requirements.
据推测,bind
函数知道它拥有哪种套接字描述符并将 struct sockaddr *
转换回 struct sockaddr_in *
。
关于c - 为什么从 sockaddr_in 到 sockaddr 的转换有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51287930/
这个问题在这里已经有了答案: C++ Redefinition Header Files (winsock2.h) (15 个答案) 关闭 6 年前。 我使用 Visual Studio 2012
struct sockaddr { unsigned short sa_family; // address family, AF_xxx char sa_da
当我运行时 cc -c -o rawudp.o rawudp.c 我明白了 在 rawudp.c:1:0 包含的文件中: rawudp.h:48:23: 错误:未知类型名称‘sockaddr’ mak
我想知道确定使用哪个 sockaddr 变体的正确方法。 来自Beej's Guide to Network Programming : if (p->ai_family == AF_INET) {
我目前正在编写一个使用网络结构(例如 sockaddr)的 C 库。在我的代码中,我使用这个三元条件来推断这种结构的大小。 addrlen = sourceaddr->sa_family ==
在尝试以下操作时,第二个 sockaddr 中的地址发生了变化: /*Stuff*/ sockaddr add1, add2; recvfrom(/*socket*/, /*buffer*/, /*c
我正在尝试创建一个 void mksockaddr(int af, int proto, char addr[], struct sockaddr* dst) 来创建一个 sockaddr 结构,这是
我正在尝试学习网络编程,并在此过程中学习 C。我对作为通用地址的结构 sockaddr 和 sockaddr_in 感到困惑。我的书是这样说的: Thus, we can fill in the fi
根据msdn的这篇文章( http://msdn.microsoft.com/en-us/library/windows/desktop/ms740496(v=vs.85).aspx ) 结构因选择的
我正在打开一个 Linux 数据包套接字并尝试将接收到的数据包读入一个结构中: type msg struct { n, oobn, flags int p, oob []byte
我一直在阅读 Beej 的网络编程指南,在他的一个示例中,他将指向 struct sockaddr 的指针转换为 struct sockaddr_in6 指针,如下所示。 void *addr
'operator sockaddr *()'在这里是什么意思?类 Raw 是一个内部类,sockaddr 是一个结构。 struct sockaddr { __SOCKADDR_COMM
var sa_tab:[sockaddr?] = [sockaddr](repeating: sockaddr(), count: Int(RTAX_MAX)) let addr:sockaddr =
我遇到过这段代码: (stuct sockadrr*)&servaddr 这是什么意思?您以某种方式将结构强制转换为 servaddr? 我只是想弄清楚这段代码的语法及其在 C 中的含义。 最佳答案
概览 我正在用 C++ 编写一个简单的测试游戏服务器。目前,我有一个监听任何人的绑定(bind)套接字,当某种数据包类型到达时,我想将 sockaddr 结构保存到一个对象中,该对象将传递给一个单独的
我正在编写一个从命令行提供地址的 UDP 套接字程序。 为了使发送和写入更容易,我使用 getaddrinfo 将地址转换为 sockaddr 结构:sockaddr_in 或 sockaddr_in
手册页未涵盖在发生错误时通过引用传递到 accept() 函数的 sockaddr 变量的状态。 是否可以安全地假设如果在客户端连接和您接受它之间出现问题,导致 accept() 的返回值小于 0,那
目前我正在尝试部署一个用C++编写的网络应用程序,由于网络问题,它看起来陷入了死循环: [isaev@feck-5 ~]$ sudo strace -p 26252 -f -e trace=netwo
我用 C 编写了一个简短的 TCP 服务器示例,它持续监听端口 12701 上的连接,并将对等方的 sockaddr.sa_family 的值输出到标准输出。程序在无限循环中调用 accept(),它
我在 Mac OS X 中做了一些干预(本质上是拦截 C 调用),我注意到 ping 应用程序试图调用 addrlen 值为 16 的 sendto 函数。在 sys/socket.h 中我可以清楚地
我是一名优秀的程序员,十分优秀!