Header files required for socket programming

Source: Internet
Author: User

There are three types of sockets:
1. Streaming socket (socket_stream)
Provides reliable connection-oriented data transmission services. Data is considered as a byte stream with no length restrictions. For example, FTP uses this protocol.
2. datagram socket (socket_dgram)
Provides a connectionless data transmission service, which does not guarantee reliability.
3. original socket (socket_raw)
This interface allows direct access to lower-level protocols, such as IP and ICMP.

II. The basic socket system has the following calls:
Create a socket: socket ()
Bind local port: BIND ()
Establish a connection: connect (), accept ()
Listening port: Listen ()
Data transmission: Send (), Recv ()
Input/Output multiplexing: Select ()
Disable 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; // ip address
Unsigned char sin_zero [8]; // fill
}

Four common functions
1 socket ()
Header file:
# Include <sys/types. h>
# Include <sys/socket. h>
Function prototype:
Int socket (INT domain, int type, int Protocol)
Domain: protocol type, generally af_inet
Type: Socket Type
Protocol: used to specify the number of the transport protocol used by the socket. It is usually set to 0.

2 BIND ()
Header file:
# Include <sys/types. h>
# Include <sys/socket. h>
Function prototype:
Int BIND (INT sockfd, struct sockaddr * my_addr, int addrlen)
Sockfd: Socket Descriptor
My_addr: A sockaddr pointer pointing to information including the local IP address and port number.
Addrlen: usually set to sizeof (struct sockaddr)

3 connect ()
Header file:
# Include <sys/types. h>
# Include <sys/socket. h>
Function prototype:
Int connect (INT sockfd, struct sockaddr * serv_addr, int addrlen)
Sockfd: Socket descriptor of the target server
Serv_addr: pointer containing the IP address and port number of the target machine
Addrlen: sizeof (struct sockaddr)

4 listen ()
Header file:
# Include <sys/socket. h>
Function prototype:
Int listen (INT sockfd, int backlog );
Sockfd: Socket descriptor returned by the socket () system call
Backlog: specify the maximum number of requests in the Request queue. incoming connection requests will wait for accept () in the queue.

5 accept ()
Header file:
# Include <sys/types. h>
# Inlcude <sys/socket. h>
Function prototype:
Int accept (INT sockfd, void * ADDR, int addrlen)
Sockfd: indicates the socket descriptor to be monitored.
ADDR: Usually a pointer to the sockaddr_in variable, which is used to store the information of the host requesting the Connection Service.
Addrlen: sizeof (struct sockaddr_in)

6 send ()
Header file:
# Include <sys/socket. h>
Function prototype:
Int send (INT sockfd, const void * MSG, int Len, int flags );
Sockfd: Socket descriptor used to transmit data
MSG: pointer to send data
Flags: 0

7 Recv ()
Header file:
# Include <sys/types. h>
# Include <sys/socket. h>
Function prototype:
Int Recv (INT sockfd, void * Buf, int Len, unsigned int flags)
Sockfd: Socket descriptor for receiving data
Buf: buffer for storing data
Len: Buffer Length
Flags: 0

8 sendto ()
Header file:
# Include <sys/types. h>
# Include <sys/socket. h>
Function prototype:
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 prototype:
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)
-----------------------------------

Data Structure of netinet/if_ether.h ether_arp

Netinet/ether. h Ethernet bytes and ASCII bytes conversion, including functions such as ether_ntoa () and ether_aton

Netinet/IP. h this header file and Linux/IP. H seems very similar. It also has the data structure of the iphdr and also includes the timestamp structure. In my understanding, the IP address under the Linux folder. H is the IP header file compiled by Linux hackers. This is the header file defined by GNU at the beginning, and also includes the ipheader structure definition in BSD. Likewise, there are TCP. H files in this directory.

The data structure of Linux/IP. h iphdr, as well as some IP layer data definitions. Likewise, there are TCP. H, UDP. H, and so on.

Linux/if. h. The main socket header file seems to be modified from UNIX if. h, which defines the macro of the interface information of the NIC, for example, iff_up. there are also several important interface data structure definitions, including ifreq, ifconf, ifmap

The data structure definition of the Linux/if_packet.h raw data packet, including sockaddr_pkt and sockaddr_ll. You cannot miss this file if you want to receive the original data packet. Similarly, if_ppp.h, if_tun.h, etc.

Netinet/in. h does more. Port macro definition, famous IP (such as loopback), sockaddr_in structure, network byte conversion (ntoh, Hton ....)... There are too many. If it's okay, add this file to the header file.

The netdb. h file is named like its name, including the hostent structure (host environment) and several functions (gethostbyname) for obtaining host information ). It seems that this is the environment that defines the host, such as hostname.

Net/BPF. h Berkeley's packet filtering header file. If you want to use BPF for packet filtering, pay attention to this file.

Net/Ethernet. h includes several Ethernet data structures, ether_addr (MAC frame structure), and ether_header (Ethernet frame header)

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

<Sys/types. h> // primitive system data types (including many types of redefinition, such as pid_t and int8_t)
<Sys/socket. h> // socket-related function declarations and struct definitions, such as socket (), BIND (), connect (), and struct sockaddr.
<Sys/IOCTL. h> // I/O control operation-related function declaration, such as IOCTL ()
<Stdlib. h> // some struct and macro definitions, such as exit_failure and exit_success.
<Netdb. h> // some struct definitions, macro definitions, and function declarations, such as struct hostent, struct servent, gethostbyname (), gethostbyaddr (), and herror ().
<ARPA/inet. h> // some function declarations, such as inet_ntop () and inet_ntoa ()
<Netinet/in. h> // some struct declarations and macro definitions, such as struct sockaddr_in, proto_icmp, and inaddr_any.

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

Write common header files in socket in Linux # 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> to: http://blog.chinaunix.net/space.php? Uid = 21237130 & Do = Blog & id = 159722

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.