Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Open C 套接字: getsockname 方法

From 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 05:40.
201 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×