×
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 套接字: getsockname 方法

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

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

Open C 套接字: getsockname 方法

getsockname - 获取套接字名称

int getsockname (int s, struct sockaddr * restrict name, socklen_t * restrict namelen);

getsockname系统调用返回指定套接字的当前名称namelen应被初始化指出name所指向的空间容量。返回时,该参数含有返回名称

的实际大小(按字节)。

下面是getsockname函数的用法:

#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));
 
len=sizeof(ss);
getsockname(sock_fd,(struct sockaddr*)&ss,&len);
close(sock_fd);
}

内部链接

Internal Links

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