15th Chapter Sockets

Source: Internet
Author: User

Create:

#include <sys/types.h>

#include <sys/socket.h>

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

Domain

Af_unix, af_local//Address family is the file name

Af_inet//ipv4

Af_inet6//ipv6

Types

1. Stream:sock_stream//Flow

2. Datagram:sock_dgram//Datagram

The descriptor returned by the socket, called with Read and write

Close system call ends socket connection

Different domain has different address formats:

Af_unix: Defined in Sys/un.h

Struck Sockaddr_un {

sa_family_t sun_family;

Char sun_path[];

};

Af_inet: Defined in Netinet/in.h

struct SOCKADDR_IN {

short int sin_family;

unsigned short int sin_port;

struct IN_ADDR sin_addr;

}; which

Sruct in_addr {

unsigned long int s_addr;

};

Naming: To use a well-created socket, you need to name it.

#include <sys/socket.h>

int bind (int socket, const struct SOCKADDR *address, size_t Address_len); succes return 0.

To establish a socket queue: save an unhandled request

#include <sys/socket.h>

int listen (int socket, int backlog); The maximum number of backlog bits saved successfully returned 0

Receive connection:

After the server program establishes and names the socket, it can wait for the client to establish a connection to the socket via the Accept system call.

#include <sys/socket.h>

int accept (int socket, STRUCCT sockaddr *sddress, size_t *address_len);

    

15th Chapter Sockets

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.