WEB开发网
开发学院软件开发Java tcp connection setup的实现(三) 阅读

tcp connection setup的实现(三)

 2009-09-10 00:00:00 来源:WEB开发网   
核心提示: 然后来看inet_csk_accept的源码,这里有个阻塞和非阻塞的问题.非阻塞的话会直接返回的,就算accept队列为空.这个时侯设置errno为-EAGAIN. Java代码 structsock*inet_csk_accept(structsock*sk,intflags,int*err)

然后来看inet_csk_accept的源码,这里有个阻塞和非阻塞的问题.非阻塞的话会直接返回的,就算accept队列为空.这个时侯设置errno为-EAGAIN.

Java代码

struct sock *inet_csk_accept(struct sock *sk, int flags, int *err) 
{ 
 struct inet_connection_sock *icsk = inet_csk(sk); 
 struct sock *newsk; 
 int error; 
 
 lock_sock(sk); 
 
 /* We need to make sure that this socket is listening, 
 * and that it has something pending. 
 */ 
 error = -EINVAL; 
///sk也就是主socket,他的状态我们前面也讲过会一直是TCP_LISTEN. 
 if (sk->sk_state != TCP_LISTEN) 
 goto out_err; 
 
///然后判断accept队列是否为空 
 if (reqsk_queue_empty(&icsk->icsk_accept_queue)) { 
///如果是O_NONBLOCK,则返回0,此时下面的inet_csk_wait_for_connect也就会立即返回. 
 long timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK); 
 
 /* If this is a non blocking socket don't sleep */ 
 error = -EAGAIN; 
 if (!timeo) 
  goto out_err; 
///休眠或者立即返回. 
 error = inet_csk_wait_for_connect(sk, timeo); 
 if (error) 
  goto out_err; 
 } 
///得到sock并从accept队列中remove. 
 newsk = reqsk_queue_get_child(&icsk->icsk_accept_queue, sk); 
 WARN_ON(newsk->sk_state == TCP_SYN_RECV); 
out: 
 release_sock(sk); 
 return newsk; 
out_err: 
 newsk = NULL; 
 *err = error; 
 goto out; 
}

上一页  1 2 3 4 5 6 7 8  下一页

Tags:tcp connection setup

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接