Linux socket (1)

Source: Internet
Author: User

Linux socket (1)

Socket features are determined by three attributes: domain, type, and protocol.

1. Domain

AF_UNIX UNIX domain protocol (File System socket)

AF_INET ARPA Internet Protocol (UNIX network socket)

... Omitted

2. Type

1) stream socket: SOCK_STREAM

2) datagram socket: SOCK_DGRAM

3. Protocol

Generally, this parameter is determined by the socket type and socket domain. You do not need to select this parameter. setting this parameter to 0 indicates that the default protocol is used.

Socket address.

1) AF_UNIX

Struct sockaddr_un {

Sa_family_t sun_family;/* AF_UNIX */

Char sun_path [];/* pathname */

};

2) AF_INET

Struct sockaddr_in {

Short int sin_family;/* AF_INET */

Unsigned short int sin_port;/* port number */

Struct in_addr sin_addr;/* Internet address */

}; This structure is defined in the header file netinet/in. h.

The IP address structure in_addr is defined:

Struct in_addr {

Unsigned long int s_addr;

};

1. Named socket

# Include <sys/socket. h>

Int bind (int socket, const struct sockaddr * address, size_t address_len );

2. Create a socket queue

# Include <sys/socket. h>

Int listen (int socket, int backlog );

3. Accept connections

# Include <sys/socket. h>

Int accept (int socket, struct sockaddr * address, size_t * address_len );

4. Request connection

# Include <sys/socket. h>

Int connect (int socket, const struct sockaddr * address, size_t address_len );

5. Disable socket

Close ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.