Linux Network Programming framework

Source: Internet
Author: User
Tags ack htons

OSI layer seven model and TCP four layer model

OSI layer seven model and TCP four layer model

BS and CS server architecture

(1) Introduction to the CS architecture (client server, clients servers architecture)
(2) BS Architecture Introduction (broswer server, browser server architecture)

TCP protocol

(1) Three handshake required to establish the connection
(2) Condition of establishing connection: Client initiates connect when server listen
(3) Four handshake required to close the connection
(4) Server or client can initiate shutdown

Note: These handshake protocols are already encapsulated within the TCP protocol, and socket programming interfaces do not normally

How TCP guarantees reliable transmission
(1) TCP requires that both parties must shake hands before transmitting valid information to establish a connection to communicate
(2) When the receiver of TCP receives the packet, it will ack to the sender, and if the sender does not receive an ACK, it will lose the packet retransmission.
(3) Valid data content for TCP is accompanied by a checksum to prevent the content from being corrupted during delivery
(4) TCP automatically adjusts the adaptation rate based on network bandwidth ( sliding window technology )
(5) The sender will give each division message number, the receiver will check the number, once the order error will be re-transmitted.

Socket-based programming

1. Establish the connection
(1) socket. The socket function is similar to open, which opens a network connection and, if successful, returns a network file descriptor (int type), after which we operate the network connection through this network file descriptor.
(2) Bind
(3) Listen
(4) Connect
2. Sending and Receiving
(1) Send and write
(2) Recv and read
3. Auxiliary function
(1) Inet_aton, inet_addr, Inet_ntoa
(2) Inet_ntop, Inet_pton
4. Representing IP address related data structure
(1) are defined in Netinet/in.h
(2) struct sockaddr, this structure is used to represent an IP address in the network programming interface, note that this IP address is not differentiated IPv4 and IPv6 (or is compatible with IPV4 and IPV6)
(3) typedef uint32_t in_addr_t; The type used to represent the IP address within the network
(4)

structin_addr{in_addr_t s_addr;};(5)structSockaddr_in{__sockaddr_common (sin_); in_port_t Sin_port;/*Port number.*/structIN_ADDR sin_addr;/*Internet address.*//*Pad to size of ' struct sockaddr '.*/unsignedCharsin_zero[sizeof(structSOCKADDR)-__sockaddr_common_size-sizeof(in_port_t)-sizeof(structin_addr)];};

(6) struct SOCKADDR This structure is the Linux network programming interface used to represent the IP address of the standard structure, bind, connect and other functions need this structure, the structure is compatible with IPV4 and IPV6. In practical programming this structure is populated by a struct sockaddr_in or a struct sockaddr_in6.

Client process

Server process

htons function to convert host sequence to network order
Network byte order Nbo: Store in order from high to low, and use uniform network byte order on the network to avoid compatibility issues. (big-endian mode)

Host byte sequence (hbo,host byte order): Different machine HBO is not the same, related to CPU design, the order of data is determined by the CPU, and operating system independent.

Client and server code

#include <stdio.h>#include<sys/socket.h>#include<sys/types.h>/*See NOTES*/#include<arpa/inet.h>#include<string.h>#defineServer_ip "192.168.149.137"#defineServer_port 5005#defineBACKLOG 100#defineDebuf (x) printf (x)typedefstructcommu{Charname[ -]; intAge ;} info;intMain () {intSOCK_FD =-1;//Socket Descriptor    intSERVER_FD =-1;//Connection Descriptor    structsockaddr_in server_addr; structSockaddr_in client_addr;//Customer Addresssocklen_t len =0;//Receive length    intRET =-1; Charsend_buf[ -]; Charrecv_buf[ -]; //1, open the socketSOCK_FD = socket (Af_inet,sock_stream,0); if(-1==sock_fd) {Perror ("Socket"); return-1; } printf ("sock_fd =%d.\n", SOCK_FD); //2. Connect connection Serverserver_addr.sin_family = af_inet;//IPV4Server_addr.sin_port = htons (Server_port);//Set Port modeSERVER_ADDR.SIN_ADDR.S_ADDR = inet_addr (SERVER_IP);//Set IPret = Connect (SOCK_FD, (structSOCKADDR *) &server_addr,sizeof(SERVER_ADDR));    if(-1==ret) {Perror ("Connect"); return-1; } debuf ("successful resume connection \ n");    Info st1;  while(1) {printf ("Please enter student's name \ n"); scanf ("%s", St1.name); printf ("Please enter student age \ n"); scanf ("%d",&st1.age); //1. Sendret = Send (Sock_fd,&st1,sizeof(info),0); memset (Send_buf,0, strlen (SEND_BUF)); printf ("sent a student message \ n"); //2. Waiting for a replyRecv (Sock_fd,recv_buf,sizeof(RECV_BUF),0); printf ("%s\n", RECV_BUF); memset (Recv_buf,0,sizeof(RECV_BUF)); //3. Information Processing    }    return 0;}
Server
#include <stdio.h>#include<sys/socket.h>#include<sys/types.h>/*See NOTES*/#include<arpa/inet.h>#include<string.h>#defineMy_ip "192.168.149.137"#defineMy_port 5005#defineBACKLOG 100#defineDebuf (x) printf (x)typedefstructcommu{Charname[ -]; intAge ; intcmd; }info;intMain () {intSOCK_FD =-1;//Listener Descriptor intCLIENT_FD =-1;//Connect FD structsockaddr_in server_addr; structSockaddr_in client_addr;//Customer Addresssocklen_t len =0;//Receive length intRET =-1; Charrecv_buf[ -]; Charsend_buf[ -]; //1, open the socketSOCK_FD = socket (Af_inet,sock_stream,0); if(-1==sock_fd) {Perror ("Socket"); return-1; } printf ("sock_fd =%d.\n", SOCK_FD); //2.bind binding socket and native IP portsserver_addr.sin_family = af_inet;//IPV4Server_addr.sin_port = htons (My_port);//Set Port modeSERVER_ADDR.SIN_ADDR.S_ADDR = inet_addr (MY_IP);//Set IPret = bind (SOCK_FD, (structSOCKADDR *) &server_addr,sizeof(SERVER_ADDR)); if(-1==ret) {Perror ("Bind"); return-1; } debuf ("bind ok\n"); //3.listen Setting the listening portRET = Listen (SOCK_FD, BACKLOG);//backlog for queued processing if(-1==ret) {Perror ("Listen"); return-1; } debuf ("Listen ok\n"); //4. Blocking wait for connectionCLIENT_FD = Accept (SOCK_FD, (structSOCKADDR *) &client_addr, &Len); if(-1==client_fd) {Perror ("Listen"); return-1; } debuf ("successful resume connection \ n"); //you can communicate after your resume is completedmemset (Recv_buf,0,sizeof(RECV_BUF)); Info st1; while(1) { //1. Server ReceivedRecv (Client_fd,&st1,sizeof(ST1),0); printf ("student Name:%s\n", St1.name); printf ("student Age:%d\n", St1.age); memset (Recv_buf,0,sizeof(RECV_BUF)); //2, the server processing data//3, reply to the clientSend (CLIENT_FD,"OK",2,0); } return 0;}

Linux Network Programming framework

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.