Linux Application Development (13): Linux Network Programming

Source: Internet
Author: User

1. There are three types of socket:


(1) stream socket: sock_stream.

Stream sockets use the TCP protocol to provide reliable and connection-oriented communication streams.


(2) datagram socket: sock_dgram.

The datagram socket uses the UDP protocol to define a connectionless service.


(3) original socket: sock_raw.

The original socket is directly based on the IP protocol and is mainly used for testing new network protocols.

2. Network Address:

(1) In socket programming, struct sockaddr is used to record network addresses. Its definition is:

Struct sockaddr {

U_short sa_family; // protocol family, in the form of "af_xxx", for example, af_inet (IP protocol family)

Char sa_data [14]; // a 14-byte Protocol address

}


(2) In actual applications, the data structure of the recorded network address is: struct sockaddr_in, which is defined:


Struct sockaddr_in {

Short int sin_family; // protocol family


Unsigned short int sin_port; // port number

Struct in_addr sin_addr; // protocol-specific address


Unsigned char sin_zero [8]; // enter 0

}

The reason why sockaddr_in is used to replace sockaddr is that it is relatively simple to operate. The data structure in sockaddr_in is the subdivision of sa_data [14] Under sockaddr: Port, address, useless parts, let them store them separately.


(3) In struct sockaddr_in, there is a new data structure, struct in_addr, which is defined:

 
Typedef struct in_addr {

 
Union {

 
Struct {

 
Unsigned char s_b1,

 
S_b2,

 
S_b3,

 
S_b4;

 
} S_un_ B;

 
Struct {

 
Unsigned short s_w1,

 
S_w2;

 
} S_un_w;

 
Unsigned long s_addr;

 
} S_un;

 
} In_addr;

 
(4) the IP address is usually expressed by adding a number (192.168.0.1), while the IP address used in struct in_addr is expressed by a 32-bit integer, to complete the conversion, we can use the following two functions:

 
Int inet_aton (const char * CP, struct in_addr * indium );

 
Char * inet_ntoa (struct in_addr in );

 
In the function name, a represents ASCII, N represents network, and inet_aton converts an IP address in the form of A. B. C. D to A 32-bit IP address, which is stored in the indium pointer.

 
Inet_ntoa converts a 32-bit IP address to A. B. C. D.


3. byte sequence Conversion

Different types of CPU may have different bytes storage sequence for variables: Some systems are high, low, and some systems are low, and high. The data sequence transmitted over the network must be agreed. Therefore, when the internal byte storage order is different from the network byte order, the data must be converted.


Byte order conversion function:


(1) htons, host to net short, converting the unsigned short type from the host sequence to the network Sequence


(2) htonl, host to net long, which converts the unsigned long type from the host sequence to the network Sequence


(3) ntohs, net to host short


(4) ntohl, net to host long



Among them, (1) (2) used for sending data, (3) (4) used for receiving


4. IP address and Host Name

Indicates that a host can use an IP address or a host name in the network.


Struct hostent * gethostbyname (const char * hostname)


Struct hostent {// hostent = hostentry, host record

Char * h_name; // host name


Char * h_aliases; // host alias


Int h_addrtype; // address type of the host af_inet


Int h_length; // host address Length


Char ** h_addr_list; // host IP address list

}

# Define h_addr h_addr_list [0]; // The first IP address of the host


5. socket programming functions:


(1) create a socket


(2) bind, used to bind the IP address and port number to the socket


(3) connect, which is used to establish a connection with the server


(4) Listen: set the maximum connection requirements that can be processed by the server


(5) Accept, used to wait for socket connection requests from the client


(6) Send and send data


(7) Recv, accept data











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.