linux网络编程之socket函数

socket()--Create Socket


  Syntax


  #include <sys/types.h>
  #include <sys/socket.h>

  int socket(int address_family,
                  int type,
                  int protocol)

The socket() function is used to create an end point forcommunications. The end point is represented by the socket descriptor returnedby the socket() function.

Parameters


address_family
    (Input) The address family to be used with the socket. Supported value sare:
    AF_INET    For interprocess communications between processeson the same system or different systems in the Internet domain using theInternet Protocol (IPv4).
    AF_INET6    For interprocess communications between processeson the same system or different systems in the Internet domain using theInternet Protocol (IPv6 or IPv4).



type

    (Input) The type of communications desired. Supported values are:

    SOCK_DGRAM         Indicates a datagram socket is desired.
    SOCK_STREAM     Indicates a full-duplex stream socket is desired.


protocol

    (Input) The protocol to be used on the socket. Supported values are:

    0     Indicates that the default protocol for the type selected is to be used. For example, IPPROTO_TCP is chosen for the protocol if the type was set to SOCK_STREAM and the address family is AF_INET.

Return Value


socket() returns an integer. Possible values are:

    -1 (unsuccessful)
    n (successful), where n is a socket descriptor.


Usage Notes


    The socket address families and types supported by sockets are defined in <sys/socket.h>. The protocols are defined in <netinet/in.h> (Internet protocols).


NAME          


       socket - create an endpoint for communication

SYNOPSIS        


       #include <sys/types.h>          /* See NOTES */
       #include <sys/socket.h>

       int socket(int domain, int type, int protocol);

DESCRIPTION         


       socket() creates an endpoint for communication and returns a
       descriptor.

       The domain argument specifies a communication domain; this selects
       the protocol family which will be used for communication.  These
       families are defined in <sys/socket.h>.  The currently understood
       formats include:

       Name                Purpose                          
       AF_UNIX, AF_LOCAL   Local communication              
       AF_INET             IPv4 Internet protocols           
       AF_INET6            IPv6 Internet protocols           
       

       The socket has the indicated type, which specifies the communication
       semantics.  Currently defined types are:

       SOCK_STREAM     Provides sequenced, reliable, two-way, connection-
                       based byte streams.  An out-of-band data transmission
                       mechanism may be supported.

       SOCK_DGRAM      Supports datagrams (connectionless, unreliable
                       messages of a fixed maximum length).

       SOCK_RAW        Provides raw network protocol access.

     


       
RETURN VALUE   
      
       On success, a file descriptor for the new socket is returned.  On
       error, -1 is returned, and errno is set appropriately.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值