Linux Socket Learning (i) __linux

Source: Internet
Author: User
Tags unix domain socket

outline I. Socket two. BSD socket programming Preparation 1. Address 2. Port 3. Network byte order 4. Semi-correlated with all-related 5. Network programming model three. Socket interface Programming Example IV. using the Kqueue five. Using a stream

I. Socket INTRODUCTION

In Unix systems, everything is a file (Everything is a). All IO operations can be viewed as IO operations on files, followed by the mode of operation: Open-> read/write-> close, open operations (such as the Open function) to obtain "file" Access, return file descriptor, subsequent operations are through this file descriptor. Many system calls rely on file descriptors, which are unsigned integers, and each user process corresponds to a file descriptor that can be found by file descriptors. On the Unix-like platform, the standard input output of the console and the standard error output have corresponding file descriptors, respectively, 0,1,2. Are they defined in Unistd.h [CPP] view plain copy print? #define STDIN_FILENO 0/* Standard input file descriptor */#define STDOUT_FILENO 1/* Standard output file des Criptor * * #define STDERR_FILENO 2/* Standard error file Descriptor * *

In a Mac system, you can view the files and ports that a process opens by using Activity Monitor.


When the Unix kernel joins the TCP/IP protocol, a new IO operation is introduced into the system, but the network IO is much more complex than the local one because of the unreliability of the network connection. This series of interfaces, called the BSD Socket API, was developed by the University of Berkeley and eventually became the standard for web development interfaces. Network communication is essentially interprocess communication, but these two processes are generally on different computers in the network. Of course, the Socket API actually provides a way to use the local IPC: UNIX Domain Socket, which is not discussed here. The socket mentioned in this article, if no exception, is an Internet socket.

In a local process, each process can be identified by PID, and how is the process identified for a computer on the network. Computers in a network can be identified by an IP address, a process in a computer can be identified by an unsigned integer (port number), so processes in a network can be identified by IP address + port number.

two. BSD Socket Programming Preparation

1. Address

In the program, how do we save an address? The sockaddr in  <sys/socket.h> is the type of struct that describes the socket address. [CPP]   View plain  copy  print? /*  * [xsi] structure used by kernel to store most addresses.   */   struct sockaddr {       __uint8_t    sa_len;     /* total length */       sa_ family_t sa_family;  /* [xsi] address family */        char        sa_data[14];    /* [ xsi] addr value  (Actually larger)  */  };  

In order to conveniently set the socket address of the language network communication, the SOCKADDR_IN structure is introduced (corresponding to the UNIX Domain socket Sockaddr_un) [CPP] view plain copy print?   /* * Socket address, Internet style.       * * struct sockaddr_in {__uint8_t sin_len;       sa_family_t sin_family; in_port_t sin_port;//is the network byte order struct IN_ADDR sin_addr;//in_addr exists reason is historical reason, its essence is to represent an IP address of 32-bit integer char si N_zero[8];//bzero is purely for the compatibility of sockaddr};

In actual programming, it is often necessary to cast sockaddr_in to the sockaddr type.


2. Port

When it comes to ports we often associate hardware, the port in the network programming is actually a logo, or a system of resources. The system provides a mechanism for port allocation and management.


3. Network byte order

Before we talk about the network byte order (Endianness), let's talk about the byte order first. A byte sequence is also called an end order, which is the sequence of bytes in a computer that holds multibyte data. Typically, the order in which data is stored in memory or in bytes when the network is transmitted. The commonly used byte order is the big-end order (Big-endian), the small-order (Litle-endian, and the uncommon mixed-order Middle-endian). Different CPUs may use different byte sequences, such as x86,pdp-11 and other processors for small-order, Motorola 6800,POWERPC 970, etc. using a big-end order. A small sequence is a low byte bit stored at the lower end of the memory address, high order refers to the high byte stored in the low-end memory. For example, what is a big-end sequence and a small-order: for example, a 4-byte integer 16 in the form of 0x12345678, the leftmost is high.

Big-endian sequence

Low

> >

> >

High

12

34

56

78

Small end sequence

Low

> >

> >

High

78

56

34

12

The TCP/IP protocols use the byte sequence as the big-endian sequence, and we call the byte sequence used in the TCP/IP protocol a network byte order. When programming, you can use the associated interfaces defined in Sys/_endian.h to do the local byte order and the network byte-order interchange. [CPP] view plain copy print? #define NTOHS (x) __darwin_osswapint16 (x)//16-bit integer network byte sequence host byte order #define HTONS (x) __darwin_osswapint16 (x)//16-bit integer host byte order to network byte order

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.