×
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 套接字: getsockopt, setsockopt 方法

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

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

Open C 套接字: getsockopt, setsockopt 方法

getsockopt, setsockopt方法获取或设置套接字选项。

int getsockopt (int s, int level, int optname, void * restrict optval, 
socklen_t * restrict optlen);
 
int setsockopt (int s, int level, int optname, const void *optval, socklen_t optlen);

getsockoptsetsockopt系统调用操作与套接字关联的选项。选项可以存在于多个协议层;但它们总是处于当前的"socket"层。

下面是getsockoptsetsockopt系统调用的用法:

#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
void SocketOptions()
{
int sock_fd;
int optval = 1;
unsigned int optlen = sizeof(optval);
int rdoptval;
sock_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
setsockopt(sock_fd,SOL_SOCKET,SO_KEEPALIVE,&optval,optlen);
getsockopt(sock_fd,SOL_SOCKET,SO_KEEPALIVE,(void*)&rdoptval,&optlen);
close(sock_fd);
}

内部链接

Internal Links

This page was last modified on 15 December 2011, at 08:40.
100 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.

×