linux网络编程之socket函数

来源:互联网 发布:java点餐系统源代码 编辑:程序博客网 时间:2024/05/21 07:54

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.