sock结构和socket结构的区别和联系

socket是为引用层提供的统一结构,也就是所谓的BSD socket
而sock则是inet地址族为上层提供的一个抽象,
我们所说的建立一个套接字其实就是建立一个sock结构,其中包含了一个连接的绝大部分信息.
socket就是通常使用的socket接口,处于用户空间
例如在intsocket(int family, int type, intprotocol)调用时候,首先会掉用sock_create,而sock_create回调用sock_alloc()分配一个具体的socket,接下来sock_create将根据protocol调用具体的sock create函数创建sock结构

int sock_create(int family, int type, int protocol, struct socket **res)
{
   int i;
   struct socket *sock;

   /*
    *  Check protocol is in range
    */
   if (family < 0 || family >= NPROTO)
       return -EINVAL;
       
#if defined(CONFIG_KMOD) && defined(CONFIG_NET)
   /* Attempt to load a protocol module if the find failed.
    *
    * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
    * requested real, full-featured networking support upon configuration.
    * Otherwise module support will break!
    */
   if (net_families[family] == NULL) {
       char module_name[30];
       
       sprintf(module_name,"net-pf-%d",family);
       request_module(module_name);
   }
#endif

   if (net_families[family]==NULL)
       return -EINVAL;

/*
*  Check that this is a type that we know how to manipulate and
*  the protocol makes sense here. The family can still reject the
*  protocol later.
*/

   if ((type != SOCK_STREAM && type != SOCK_DGRAM &&
        type != SOCK_SEQPACKET && type != SOCK_RAW && type != SOCK_RDM &&
#ifdef CONFIG_XTP
       type != SOCK_WEB  &&
#endif
        type != SOCK_PACKET) || protocol < 0)
           return -EINVAL;

/*
*  Allocate the socket and allow the family to set things up. if
*  the protocol is 0, the family is instructed to select an appropriate
*  default.
*/
   
   if (!(sock = sock_alloc())) {
       printf("no more alloc sockets");
       return -ENFILE;     /* Not exactly a match, but its the closest posix thing */                      
   }

   sock->type = type;

   if ((i = net_families[family]->create(sock, protocol)) < 0) {
       sock_release(sock);
       return i;
   }

   *res = sock;
   return i;
}
链接: http://oldlinux.org/cgi-bin/LB5000XP/topic.cgi?forum=23&topic=7&show=

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值