zoukankan      html  css  js  c++  java
  • Berkeley套接字

    Berkeley套接字(也称为BSD套接字应用程序接口(API)包括了一个用C语言写成的应用程序开发库,主要用于实现进程间通讯,在计算机网络通讯方面被广泛使用。

    Berkeley套接字(也作BSD套接字应用程序接口)刚开始是4.2BSD Unix操作系统(于1983发布)的一套应用程序接口。然而,由于AT&T的专利保护着UNIX,所以只有在1989年Berkeley大学才能自由地发布自己的操作系统和网络库。

    Berkeley套接字应用程序接口形成了事实上的网络套接字的标准精髓。 大多数其他的编程语言使用与这套用C语言写成的应用程序接口[1] 类似的接口。 这套应用程序接口也被用于Unix域套接字(Unix domain sockets),后者可以在单机上作为进程间通讯(IPC)频道的接口。

    这种基于流的传输层接口(TLI)应用程序接口为套接字应用程序接口提供了一种选择。 不过,最近提供TLI应用程序接口的的系统同时也提供Berkeley套接字应用程序接口。


    Berkeley套接字接口
    ,一个应用程序接口(API),使用一个Internet套接字的概念,使主机间或者一台计算机上的进程间可以通讯。 它可以在很多不同的输入/输出设备和驱动之上运行,尽管这有赖于操作系统的具体实现。 接口实现用于TCP/IP协议,因此它是维持Internet的基本技术之一。 它是由加利福尼亚的伯克利大学开发,最初用于Unix系统。 如今,所有的现代操作系统都有一些源于Berkeley套接字接口的实现,它已成为连接Internet的标准接口。

    Berkeley套接字接口 

    套接字接口的接入有三个不同的级别,最基础的也是最有效的就是raw socket级别接入。 很少的应用程序需要在外向通讯控制的这个级别接入,所以raw socket级别是只为了用于开发计算机Internet相关技术的。 最近几年,大多数的操作系统已经实现了对它的全方位支持,包括Windows XP。

    使用Berkeley套接字的系统 

    由于Berkeley套接字是第一个socket,大多数程序员很熟悉它们,所以大量系统把伯克利套接字作为其主要的网络API。一个不完整的列表如下:

    • Windows Sockets (Winsock) ,和Berkeley Sockets很相似,最初是为了便于移植Unix程序。
    • Java Sockets
    • Python sockets
    • Perl sockets

    头文件 

    Berkeley套接字接口的定义在几个头文件中。这些文件的名字和内容与具体的实现之间有些许的不同。 大体上包括:

    <sys/socket.h>
    核心BSD套接字核心函数和数据结构。
    AF_INET、AF_INET6 地址集和它们相应的协议集PF_INET、PF_INET6. 广泛用于Internet,这些包括了IP地址和TCP、UDP端口号。
    <netinet/in.h>
    AF_INET 和AF_INET6 地址家族和他们对应的协议家族 PF_INET 和 PF_INET6。在互联网编程中广泛使用,包括IP地址以及TCP和UDP端口号。
    <sys/un.h>
    PF_UNIX/PF_LOCAL 地址集。用于运行在一台计算机上的程序间的本地通信,不用于网络通讯。
    <arpa/inet.h>
    处理数值型IP地址的函数。
    <netdb.h>
    将协议名和主机名翻译为数值地址的函数。搜索本地数据以及DNS。

    套接字API函数 

    这个列表是一个Berkeley套接字API库提供的函数或者方法的概要:

    • socket() 创建一个新的确定类型的套接字,类型用一个整型数值标识,并为它分配系统资源。
    • bind() 一般用于服务器端,将一个套接字与一个套接字地址结构相关联,比如,一个指定的本地端口和IP地址。
    • listen() 用于服务器端,使一个绑定的TCP套接字进入监听状态。
    • connect() 用于客户端,为一个套接字分配一个自由的本地端口号。 如果是TCP套接字的话,它会试图获得一个新的TCP连接。
    • accept() 用于服务器端。 它接受一个从远端客户端发出的创建一个新的TCP连接的接入请求,创建一个新的套接字,与该连接相应的套接字地址相关联。
    • send()recv(),或者write()read(),或者recvfrom()sendto(), 用于往/从远程套接字发送和接受数据。
    • close() 用于系统释放分配给一个套接字的资源。 如果是TCP,连接会被中断。
    • gethostbyname()gethostbyaddr() 用于解析主机名和地址。
    • select() 用于修整有如下情况的套接字列表: 准备读,准备写或者是有错误。
    • poll() 用于检查套接字的状态。 套接字可以被测试,看是否可以写入、读取或是有错误。
    • getsockopt() 用于查询指定的套接字一个特定的套接字选项的当前值。
    • setsockopt() 用于为指定的套接字设定一个特定的套接字选项。

    更多的细节如下给出。

    socket() 

    socket() 为通讯创建一个端点,为套接字返回一个文件描述符。 socket() 有三个参数:

    • domain 为创建的套接字指定协议集。 例如:
      • AF_INET 表示IPv4网络协议
      • AF_INET6 表示IPv6
      • AF_UNIX 表示本地套接字(使用一个文件)
    • type 如下:
      • SOCK_STREAM (可靠的面向流服务或流套接字
      • SOCK_DGRAM (数据报文服务或者数据报文套接字
      • SOCK_SEQPACKET (可靠的连续数据包服务)
      • SOCK_RAW (在网络层之上的原始协议)
    • protocol 指定实际使用的传输协议。 最常见的就是IPPROTO_TCPIPPROTO_SCTPIPPROTO_UDPIPPROTO_DCCP。这些协议都在<netinet/in.h>中有详细说明。 如果该项为“0”的话,即根据选定的domain和type选择使用缺省协议。

    如果发生错误,函数返回值为-1。 否则,函数会返回一个代表新分配的描述符的整数。

    原型:
    int socket(int domain, int type, int protocol);
    

    bind() 

    bind() 为一个套接字分配地址。当使用socket()创建套接字后,只赋予其所使用的协议,并未分配地址。在接受其它主机的连接前,必须先调用bind()为套接字分配一个地址。bind()有三个参数:

    • sockfd, 表示使用bind函数的套接字描述符
    • my_addr, 指向sockaddr结构(用于表示所分配地址)的指针
    • addrlen, 用socklen_t字段指定了sockaddr结构的长度

    如果发生错误,函数返回值为-1,否则为0。

    原型
    int bind(int sockfd, const struct sockaddr *my_addr, socklen_t addrlen);
    

    listen() 

    当socket和一个地址绑定之后,listen()函数会开始监听可能的连接请求。然而,这只能在有可靠数据流保证的时候使用,例如:数据类型(SOCK_STREAM,SOCK_SEQPACKET)。

    listen()函数需要两个参数:

    • sockfd, 一个socket的描述符.
    • backlog, 一个决定监听队列大小的整数,当有一个连接请求到来,就会进入此监听队列,当队列满后,新的连接请求会返回错误

    一旦连接被接受,返回0表示成功,错误返回-1。

    原型:

    int listen(int sockfd, int backlog);
    

    accept() 

    当应用程序监听来自其他主机的面对数据流的连接时,通过事件(比如Unix select()系统调用)通知它。必须用 accept()函数初始化连接。 Accept() 为每个连接创立新的套接字并从监听队列中移除这个连接。它使用如下参数:

    • sockfd,监听的套接字描述符
    • cliaddr, 指向sockaddr 结构体的指针,客户机地址信息。
    • addrlen,指向 socklen_t的指针,确定客户机地址结构体的大小 。

    返回新的套接字描述符,出错返回-1。进一步的通信必须通过这个套接字。

    Datagram 套接字不要求用accept()处理,因为接收方可能用监听套接字立即处理这个请求。

    函数原型:
    int accept(int sockfd, struct sockaddr *cliaddr, socklen_t *addrlen);

    The accept() system call is used with connection-based socket types (SOCK_STREAMSOCK_SEQPACKET). It extracts the first connection request on the queue of pending connections for the listening socket, sockfd, creates a new connected socket, and returns a new file descriptor referring to that socket. The newly created socket is not in the listening state. The original socket sockfd is unaffected by this call.

    The argument sockfd is a socket that has been created with socket(2), bound to a local address with bind(2), and is listening for connections after a listen(2).

    The argument addr is a pointer to a sockaddrstructure. This structure is filled in with the address of the peer socket, as known to the communications layer. The exact format of the address returned addr is determined by the socket's address family (see socket(2) and the respective protocol man pages). When addr is NULL, nothing is filled in; in this case, addrlen is not used, and should also be NULL.

    The addrlen argument is a value-result argument: the caller must initialize it to contain the size (in bytes) of the structure pointed to by addr; on return it will contain the actual size of the peer address.

    The returned address is truncated if the buffer provided is too small; in this case, addrlen will return a value greater than was supplied to the call.

    If no pending connections are present on the queue, and the socket is not marked as nonblocking, accept() blocks the caller until a connection is present. If the socket is marked nonblocking and no pending connections are present on the queue,accept() fails with the error EAGAIN or EWOULDBLOCK.

    In order to be notified of incoming connections on a socket, you can use select(2) or poll(2). A readable event will be delivered when a new connection is attempted and you may then call accept() to get a socket for that connection. Alternatively, you can set the socket to deliver SIGIO when activity occurs on a socket; see socket(7) for details.

    For certain protocols which require an explicit confirmation, such as DECNet, accept() can be thought of as merely dequeuing the next connection request and not implying confirmation. Confirmation can be implied by a normal read or write on the new file descriptor, and rejection can be implied by closing the new socket. Currently only DECNet has these semantics on Linux.

    If flags is 0, then accept4() is the same as accept(). The following values can be bitwise ORed in flags to obtain different behavior:

    connect() 

    connect()系统调用为一个套接字设置连接,参数有文件描述符和主机地址。

    某些类型的套接字是无连接的,大多数是UDP协议。对于这些套接字,连接时这样的:默认发送和接收数据的主机由给定的地址确定,可以使用 send()和 recv()。 返回-1表示出错,0表示成功。

    函数原型:
    int connect(int sockfd, const struct sockaddr *serv_addr, socklen_t addrlen);

    The connect() system call connects the socket referred to by the file descriptor sockfd to the address specified by addr. The addrlenargument specifies the size of addr. The format of the address in addr is determined by the address space of the socket sockfd; seesocket(2) for further details.

    If the socket sockfd is of type SOCK_DGRAMthen addr is the address to which datagrams are sent by default, and the only address from which datagrams are received. If the socket is of typeSOCK_STREAM or SOCK_SEQPACKET, this call attempts to make a connection to the socket that is bound to the address specified by addr.

    Generally, connection-based protocol sockets may successfully connect() only once; connectionless protocol sockets may use connect() multiple times to change their association. Connectionless sockets may dissolve the association by connecting to an address with the sa_family member ofsockaddr set to AF_UNSPEC (supported on Linux since kernel 2.2).

    外部链接 [编辑]

    ref:

    http://zh.wikipedia.org/wiki/Berkeley%E5%A5%97%E6%8E%A5%E5%AD%97

    http://linux.die.net/man/2/connect

    http://linux.die.net/man/2/accept

  • 相关阅读:
    Swift代码实现加载WEBVIEW
    android 图片压缩
    Android图片压缩(质量压缩和尺寸压缩)
    Android压缩图片到100K以下并保持不失真的高效方法
    android图片压缩的3种方法实例
    几种颜色模型的转换公式
    RGB HSV HLS三种色彩模式转换(C语言实现)
    由RGB到HSV颜色空间的理解
    BZOJ 1052 HAOI2007 覆盖问题 二分法答案+DFS
    插入排序算法
  • 原文地址:https://www.cnblogs.com/cloud2rain/p/3083881.html
Copyright © 2011-2022 走看看