Linux Sockets (1)

Source: Internet
Author: User

The properties of a socket are determined by three properties: domain, type, protocol.

1. Domain

Af_unix UNIX Domain protocol (file system sockets)

Af_inet arpa Internet Protocol (Unix network sockets)

... Omitted


2. Type

1) Flow socket: Sock_stream

2) Datagram Socket: Sock_dgram


3. Agreement

It is generally determined by the socket type and socket field, and usually does not need to be selected. Setting this parameter to 0 means 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 */

}; The structure is defined in the header file Netinet/in.h

The IP address structure in_addr is defined as:

struct IN_ADDR {

unsigned long int s_addr;

};


1. Naming sockets

#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 the connection

#include <sys/socket.h>

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


4. Request a connection

#include <sys/socket.h>

int connect (int socket, const struct SOCKADDR *address, size_t Address_len);


5. Close the socket

Close ();



Linux Sockets (1)

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.