×
Namespaces

Variants
Actions

Open C 套接字: listen 方法

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

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

Open C 套接字: listen 方法

listen方法监听套接字连接。

  int listen (int s, int backlog);

s参数是一个套接字,backlog参数定义待处理的连接队列可以增长到的最大长度。实际的最大队列长度会是在参数backlog中指定值的1.5倍以上。

下面是listen函数的用法:

#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
void listen_example()
{
int sock_fd;
int newsock_fd;
struct sockaddr_in addr;
struct sockaddr_in ss;
struct sockaddr_in new_socket;
unsigned int len;
unsigned int addr_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));
listen(sock_fd,1);
close(sock_fd);
}

内部链接

Open C 套接字概览

Internal Links

Open C Sockets Overview Open C Sockets: listen method

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

×