×
Namespaces

Variants
Actions
Revision as of 08:40, 15 December 2011 by hamishwillee (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Open C 套接字: bind 方法

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata

文章
dougcn 在 24 Jun 2008 创建
最后由 hamishwillee 在 15 Dec 2011 编辑

Open C 套接字: bind 方法

bind方法绑定一个名字到套接字。

int bind (int s, const struct sockaddr *addr, socklen_t addrlen);

bind系统调用指定本地协议地址到一个套接字。在套接字被socket函数创建时,它存在于某个地址族空间但还没有指定协议地址。bind系统调用要求addr被指定到该套接字。

下面是bind函数的用法:

#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
TInt GetSockName()
{
int sock_fd;
struct sockaddr_in addr,ss;
unsigned int len;
 
sock_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
 
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
addr.sin_port = htons(5000);
bind(sock_fd,(struct sockaddr*)&addr,sizeof(addr));
close(sock_fd);
}

注意: 为了最大限度的可移植性,应当在填值及传递到bind前总是清零套接字地址结构。

内部链接

Wiki Links

This page was last modified on 15 December 2011, at 08:40.
115 page views in the last 30 days.