socket brief

Thirteen kinds of socket types:
  1. stream socket (SOCKET_STREAM)
        to provide reliable data transmission connection-oriented services. Data is regarded as a byte stream, no length limit. In this example, FTP agreement.
  2. Datagram socket (SOCKET_DGRAM)
        provide connectionless data transfer services, does not guarantee reliability.
  3. The original socket formula (SOCKET_RAW)
        This interface allows, IP, ICMP direct access to the lower level protocols such as.

There are two basic socket system call has the following:
  create socket: Socket ()
  to bind the local port: the bind ()
  to establish a connection: connect (), accept ()
  listening port: the listen ()
  data transmission: send (), the recv ()
  input / output multiplexer: select ()
  Close the socket only: closesocket ()
    
third data type
   struct the sockaddr
      {
           unsigned Short sa_family like; // address family, usually AF_INET
           char sa_data [14]; / / 14-byte protocol address
      }

  struct the sockaddr_in
      {
      short int sin_family; // Address Group
           unsigned short int sin_port; // port number
           struct in_addr in_addr; // ip address
           unsigned char sin_zero [8]; // fill
      }

four common functions
    1 socket ()
       header: 
         #include <SYS /types.h>
         #include <SYS / socket.h>
       function prototype:
         int socket (int Domain, int type, int protocol)
              Domain: protocol type, usually AF_INET
              type: type socket
              protocol: used to specify the socket transport protocol number is usually set to 0 to

    2 bind ()
       header:
         #include <SYS / types.h>
         #include <SYS / socket.h>
       Function Prototype:
         int the bind (int sockfd, struct sockaddr * my_addr, int addrlen)
              sockfd: Socket descriptor
              my_addr: is a point with local ip and port number information of pointer type sockaddr
              addrlen: Often set to sizeof (struct the sockaddr)

    . 3 Connect ()
       header:
         #include <SYS / types.h>  
         #include <SYS / socket.h>
       function prototype:
         int Connect (int sockfd, struct serv_addr the sockaddr *, int addrlen)
               sockfd: object server the socket descriptor
               serv_addr: ip pointer contains the destination machine address and port number
               addrlen: the sizeof (struct the sockaddr)

    . 4 the listen ()
       header:
         #include <SYS / socket.h>
       prototype:
         the listen int (int sockfd, int backlog);
            sockfd: socket () system call returns socket descriptor
            backlog: specifies the maximum number of requests in the request queue, the incoming connection request waiting in the queue accept () thereof.

    5 accept ()
       header:   
         #include <SYS / types.h>
         #inlcude <SYS / socket.h>
       Function Prototype:
         int Accept (int sockfd, void * addr, int addrlen)
               sockfd: is listening socket descriptor
               addr: sockaddr_in usually a pointer variable, the variable is used to store information of the host connection request made and services
               addrlen: the sizeof (struct sockaddr_in)
       
    . 6 Send ()
        header:
          #include <SYS / socket.h>
        prototype:
            int send (int sockfd, const void * msg, int len, int flags);
                 sockfd: to transfer data socket descriptor
                 msg: pointer data to be transmitted to 
                 the flags: 0
      
    . 7 the recv ()
        header:
          #include <SYS / types.h>
          #include <SYS / socket.h>
        Function Prototype:
          int the recv (int sockfd, void * buf, int len, unsigned int the flags)
              sockfd: Socket receive data descriptor
              buf: storing data buffer
              len: size of the buffer
              the flags: 0

    . 8 the sendto ()
       header:
         #include <SYS / types.h>
         #include <SYS / socket.h>
       function prototype:
         int the sendto (int sockfd, MSG const void *, int len, unsigned int the flags, const struct to the sockaddr *, int tolen);
        
    
    9 recvfrom()
       头文件:
         #include <sys/types.h>
         #include <sys/socket.h>
       函数原型:
         int recvfrom(int sockfd, void *buf, int len, unsigned int flags, struct sockaddr *from, int fromlen)

    
    10 read() write()
         int read(int fd, char *buf, int len)
         int write(int fd, char *buf, int len)
       
    11 shutdown()
         close(sockfd)
         int shutdown(int sockfd, int how)
-----------------------------------

netinet / if_ether.h ether_arp data structure

netinet / network ascii byte and byte conversion ether.h ether Zhen, including ether_ntoa (), ether_aton function definition such

netinet / ip.h ip header file and linux / ip.h seem very similar, there are data structures iphdr, but also includes a timestamp structure, I understand that, ip.h linux folder is written in linux hackers header files, and this is the beginning of a gnu on the definition of header files, and also includes ipheader structure definition of bsd. Similarly there tcp.h and other files in this directory

Data structures linux / ip.h iphdr, and data layers define ip, empathy there tcp.h, udp.h etc.

linux / if.h main socket header files, it seems modified from the unix if.h, it defines the interface card information macros, such as IFF_UP. In addition there are several important data structures defined interface, including ifreq, ifconf, ifmap

Data structure definitions linux / if_packet.h the original data packet, including sockaddr_pkt, sockaddr_ll, I would like to receive the original data packet can not pass this document. Similarly there if_ppp.h, if_tun.h etc.

netinet things / in.h this document made it much. Port macro definition, known IP (such as Loopback), the sockaddr_in structure, the network-byte converter (ntoh, hton ....). . . Anyway, too much, all right, then put the file to the header file that contains in it

netdb.h file to its name, including structural hostent (host environment), the host of several functions to obtain information (gethostbyname). It seems that this is the definition of the host environment, such as hostname, etc.

net / bpf.h berkeley packet filtering headers, packet filtering with bpf want to pay attention to what this document

net / ethernet.h Ethernet data structure comprises several, ether_addr (mac frame structure), ether_header (Ethernet frame header)

-------------------------------

<sys / types.h> // primitive system data types ( comprising many types redefined as pid_t, int8_t etc.)   
<SYS / socket.h> // declaration and the function definition of the structure associated with the socket, such as socket (), bind (), connect () , and the like defined struct sockaddr
<sys / ioctl.h> // I / O control operation related to the function declaration, such as the ioctl ()
<stdlib.h> // some structure and macro definitions, such as EXIT_FAILURE, EXIT_SUCCESS like
<netdb.h> // define certain structure, macro definitions and function declarations, such as struct hostent, struct servent, gethostbyname ( ), gethostbyaddr (), herror () like
<arpa / inet.h> // declare some functions, such as inet_ntop (), inet_ntoa () like
<netinet / in.h> // declare some structure, macro definitions, such as struct sockaddr_in, PROTO_ICMP, INADDR_ANY etc.

------------------------------

under linux socket write common header file
 
#include <sys/socket.h>   //connect,send,recv,setsockopt等
#include <sys/types.h>      

#include <netinet/in.h>     // sockaddr_in, "man 7 ip" ,htons
#include <poll.h>             //poll,pollfd
#include <arpa/inet.h>   //inet_addr,inet_aton
#include <unistd.h>        //read,write
#include <netdb.h>         //gethostbyname

#include <error.h>         //perror
#include <stdio.h>
#include <errno.h>         //errno

#include <string.h>          // memset
#include <string>
#include <iostream>
 

Guess you like

Origin www.cnblogs.com/whj0402/p/11609354.html