13 Types of Sockets:
1. Streaming Sockets (Socket_stream)
Provides reliable, connection-oriented data transfer services. Data is treated as a byte stream with no length limit. For example, the FTP protocol uses this.
2. Datagram-type sockets (Socket_dgram)
Provides non-connected data transfer services and does not guarantee reliability.
3. Original socket (Socket_raw)
This interface allows direct access to lower-level protocols such as IP,ICMP.
The two basic sockets system has the following notes:
Create socket: Socket ()
Binding the Port: Bind ()
Establish connection: Connect (), accept ()
Listening port: Listen ()
Data transfer: Send (), recv ()
Input/Output multiplexing: Select ()
Close socket only: Closesocket ()
Three data types
struct SOCKADDR
{
unsigned short sa_family; Address family, generally af_inet
Char sa_data[14]; 14-byte Protocol address
}
struct SOCKADDR_IN
{
short int sin_family; Address family
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 file:
#include <sys/types.h>
#include <sys/socket.h>
Function Prototypes:
int socket (int domain, int type, int protocol)
Domain: protocol type, typically af_inet
Type:socket type
Protocol: Used to specify the transport protocol number used by the socket, usually set to 0
2 bind ()
Header file:
#include <sys/types.h>
#include <sys/socket.h>
Function Prototypes:
int bind (int sockfd, struct sockaddr *my_addr, int addrlen)
Sockfd:socket Descriptor
MY_ADDR: is a pointer to a SOCKADDR type that contains information such as a native IP address and port number
Addrlen: Often set to sizeof (struct sockaddr)
3 Connect ()
Header file:
#include <sys/types.h>
#include <sys/socket.h>
Function Prototypes:
int connect (int sockfd, struct sockaddr *serv_addr, int addrlen)
SOCKFD: Socket descriptor for destination server
SERV_ADDR: A pointer containing the IP address and port number of the destination machine
addrlen:sizeof (struct sockaddr)
4 Listen ()
Header file:
#include <sys/socket.h>
Function Prototypes:
int listen (int sockfd, int backlog);
Sockfd:socket () The socket descriptor returned by the system call
Backlog: Specifies the maximum number of requests in the request queue, and incoming connection requests will wait in the queue for accept () them.
5 Accept ()
Header file:
#include <sys/types.h>
#inlcude <sys/socket.h>
Function Prototypes:
int accept (int sockfd, void *addr, int addrlen)
SOCKFD: Is the socket descriptor being monitored
Addr: Usually a pointer to the SOCKADDR_IN variable that holds the information for the host that made the connection request service
addrlen:sizeof (struct sockaddr_in)
6 Send ()
Header file:
#include <sys/socket.h>
Function Prototypes:
int send (int sockfd, const void *msg, int len, int flags);
SOCKFD: The socket descriptor used to transfer data
Msg: A pointer to send data
flags:0
7 recv ()
Header file:
#include <sys/types.h>
#include <sys/socket.h>
Function Prototypes:
int recv (int sockfd, void *buf, int len, unsigned int flags)
SOCKFD: Socket descriptor for receiving data
BUF: Buffer for storing data
Len: The length of the buffer
flags:0
8 SendTo ()
Header file:
#include <sys/types.h>
#include <sys/socket.h>
Function Prototypes:
int sendto (int sockfd, const void *msg, int len, unsigned int flags, const struct SOCKADDR *to, int tolen);
9 Recvfrom ()
Header file:
#include <sys/types.h>
#include <sys/socket.h>
Function Prototypes:
int recvfrom (int sockfd, void *buf, int len, unsigned int flags, struct sockaddr *from, int fromlen)
Ten read () write ()
int read (int fd, char *buf, int len)
int write (int fd, char *buf, int len)
Shutdown ()
Close (SOCKFD)
int shutdown (int sockfd, int how)
-----------------------------------
Data structure of Netinet/if_ether.h Ether_arp
Netinet/ether.h Ethernet byte and ASCII byte conversion, including Ether_ntoa (), Ether_aton function definition
Netinet/ip.h this header file and Linux/ip.h seem very similar, there are IPHDR data structure, but also includes the timestamp structure, I understand that the Linux folder under the Ip.h is an IP header file written by a Linux hacker, and this is a header file that was first defined by GNU and includes the Ipheader structure definition in BSD. In the same vein, files such as tcp.h in this directory
Linux/ip.h IPHDR data structure, as well as some IP layer data definition, the same as tcp.h,udp.h, etc.
Linux/if.h main socket header file, which seems to modify the If.h from UNIX, defines the interface information for the NIC macros, such as IFF_UP. There are also several important interface data structure definitions, including Ifreq,ifconf,ifmap
Linux/if_packet.h the data structure definition of the original packet, including the SOCKADDR_PKT,SOCKADDR_LL, you cannot miss this file if you want to receive the original packet. and If_ppp.h,if_tun.h and so on.
Netinet/in.h This file is a lot of things. Port macro definition, famous IP (such as loopback), Structure sockaddr_in, network byte conversion (Ntoh,hton .... )。。。 Anyway, it's too much, just add this file to your head file.
The netdb.h file, such as its name, includes a structure hostent (host environment), which obtains the host's information for several functions (gethostbyname). It seems that this is the environment that defines the host, such as hostname, etc.
Net/bpf.h Berkeley Packet Filter header file, want to use BPF to filter the packet to pay attention to this file
Net/ethernet.h includes several ethernet data structures, ETHER_ADDR (MAC frame structure), Ether_header (the head of the Ethernet frame)
-------------------------------
<sys/types.h>//primitive System data types (contains many types of redefinition, such as pid_t, int8_t, etc.)
<sys/socket.h>//socket-related function declarations and struct-body definitions such as sockets (), bind (), connect () and struct SOCKADDR definitions, etc.
<sys/ioctl.h>//I/O Control operation-related function declarations, such as IOCTL ()
<stdlib.h>//Certain structure definitions and macro definitions, such as exit_failure, exit_success, etc.
<netdb.h>//Some struct definitions, macro definitions, and function declarations such as struct hostent, struct servent, gethostbyname (), gethostbyaddr (), Herror ( ) and other
<arpa/inet.h>//Certain function declarations, such as Inet_ntop (), Inet_ntoa (), etc.
<netinet/in.h>//Certain structure declarations, macro definitions, such as struct sockaddr_in, proto_icmp, Inaddr_any, etc.
------------------------------
Linux socket writing common header files #include <sys/socket.h>//connect,send,recv,setsockopt, etc.
#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> from:blog.chinaunix.net/u3/102500/showart_2065640.html
Socket header File