简体   繁体   English

我可以使用临时地址结构调用 bind() 吗?

[英]Can I call bind() with a temp addr struct?

The bind() function gets const struct sockaddr *addr as one of parameters. bind()函数获取const struct sockaddr *addr作为参数之一。 Can I pass a temp sockaddr structure, that will be deleted immediately after bind() calling?我可以传递一个临时sockaddr结构,它将在调用bind()后立即删除吗?

void bindMe(int socket) {
    struct sockaddr_in addr = {...};
    bind(socket, (struct sockaddr_in*)&addr, sizeof(sockaddr_in));
}
// addr is no more exist after function calling, 
// but I still want to work with the socket.

Also, there is a lot of function in POSIX take pointer to structs.此外,POSIX 中有很多函数都带有指向结构的指针。 How can I determine if I can free the struct after function calling?如何确定在函数调用后是否可以释放结构?

Yes, you can do that.是的,你可以这样做。 bind is a synchronous system call, so once it returns, the kernel is all done with the parameters. bind是一个同步系统调用,所以一旦它返回,内核就完成了所有参数。

If the socket is non-blocking and bind returns EINPROGRESS to indicate that the bind is happening asynchronously, it's still safe to free the struct.如果套接字是非阻塞的并且bind返回EINPROGRESS以指示绑定是异步发生的,那么释放结构仍然是安全的。 The kernel copies the struct internally while the operation completes asynchronously.当操作异步完成时,内核会在内部复制结构。

How can I determine if I can free the struct after function calling?如何确定在函数调用后是否可以释放结构?

Read the documentation.阅读文档。 Most POSIX functions are synchronous, and the parameters can safely be deallocated after the function returns, but that's not always true.大多数 POSIX 函数都是同步的,并且可以在函数返回后安全地释放参数,但这并不总是正确的。 If you're unsure, read the documentation.如果您不确定,请阅读文档。 And obviously if you have multiple threads in your program, you shouldn't be concurrently modifying the data from another thread while an API call is in progress.很明显,如果您的程序中有多个线程,则在 API 调用正在进行时,您不应该同时修改来自另一个线程的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 结构中的指针“ addr”被分配了内存,但是当我在另一个函数中调用它时,它将无法工作 - A pointer “addr” in a struct was allocated memory but when I call it in another function, it can't work 我可以 bind() 到 in6addr_any( ie::) 和 sendto() localhost - can I bind() to in6addr_any( ie ::) and sendto() localhost getsockname()可以将端口分配给struct addr_in吗? - getsockname() can assign port to struct addr_in? IN6ADDR_ANY_INIT可以自动绑定到链接本地地址吗? - Can IN6ADDR_ANY_INIT automatically bind to a Link Local address? connect(2)调用是否使用完整的结构sockaddr_in还是可以满足in_addr结构? - Does the connect(2) call make use of a full struct sockaddr_in or could it just suffice with a struct in_addr? 将struct in_addr转换为文本 - convert struct in_addr to text 使用inet_pton将计算出的网络地址存储在“ struct in6_addr”或“ struct in6_addr.sin6_addr”中 - use inet_pton to store computed network address in ''struct in6_addr'' or “struct in6_addr.sin6_addr” 我可以在同一个套接字描述符上调用 bind() 然后 connect() 吗? - Can I call bind() and then connect() on same socket descriptor? 参数与原型不兼容:struct in_addr - Argument is incompatible with prototype: struct in_addr 包括在struct中更改sizeof in_addr - include changes sizeof in_addr in struct
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM