Core concepts and models of network programming
1. Question
How to program network?
2. The essence of network programming
Using the interface function provided by the operating system, the application has the ability to send and receive network data.
- The network interface is a function provided by the operating system at the code level
- Network programming is just a fancy way to play a series of system functions
- Applications use the networking capabilities of the operating system through network interfaces
- Network programming is a domain specific C language programming
3. Core concepts of network programming
- Protocol: predefined data rules for data communication
- Address: an integer value used to identify a device in network communication
- Port number:
- The value specified by the device for sending and receiving data, which is used to identify the specific connection
- It can be understood as: the data channel used for network communication in the device
- role
- Server: the device waiting for connection
- Client: the device that initiates the connection
- Network knowledge charging station
- Is the web address the IP address? What is the URL? What is the domain name?
- The web address is not an IP address, but the address of network information resources (such as the address of specific web pages), i.e. URL
- Domain name is an alias of IP address. Multiple domain names can point to the same IP address
- Must the protocol be binary data that you can't understand?
- Agreement is a kind of agreement, that is, predefined rules
- Protocols can be defined based on text or binary
- Small end system
- The system adopts small end mode, that is, the low byte of data is placed in the low address of memory
- Big end system
- The system adopts the big end mode, that is, the low byte of data is placed in the high address of memory
- Network byte order
- The network byte order adopts the large end mode, so the byte order conversion is required in the small segment system.
- Is the web address the IP address? What is the URL? What is the domain name?
4. Network programming mode
1. Prepare network connection 2. Connecting remote devices 3. Send and receive data 4. Close connection
- Discussion on network programming interface
#include<sys/types.h> #include<sys/socket.h>
Function prototype | Function description |
---|---|
int socket(int domain, int type, int protocol); | Create sockets to prepare for network connections |
int connect(SOCKET s, const struct sockaddr * name, int namelen); | Connect to a remote device at the specified address |
ssize_t send(int s,const void* buf,size_t len,int flags); | Send data to remote device |
ssize_t recv(int sockfd, void *buf, size_t nbytes, int flags); | Receive data from remote device |
int close(int fd); | Close the connection and destroy the socket |
5. Programming experiment
//1-1.c #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #include<arpa/inet.h> #include<stdio.h> #include<unistd.h> #include<string.h> int main() { int sock = 0; struct sockaddr_in addr = {0}; char* tosend = "GET /index.html HTTP/1.1\nHOST: www.dt4sw.com\nUser-Agent: TEST\nConnection: close\n\n"; int len = 0; int r = 0; char buf[128] = {0}; sock = socket(PF_INET,SOCK_STREAM,0); if(sock == -1) { printf("socket err\n"); return -1; } addr.sin_family = AF_INET; addr.sin_addr.s_addr = inet_addr("47.99.217.175"); addr.sin_port = htons(80); if(connect(sock,(struct sockaddr*)&addr,sizeof(addr)) == -1) { printf("connect err\n"); return -1; } printf("connect success\n"); len = send(sock,tosend,strlen(tosend),0); printf("send bytes = %d\n",len); do { int i = 0; r = recv(sock, buf, sizeof(buf), 0); if( r > 0) { len += r; } for( i = 0; i < r; i++) { printf("%c",buf[i]); } } while ( r > 0); printf("\n"); printf("recv bytes = %d\n",len); close(sock); return 0; }
Operation results:
connect success send bytes = 81 HTTP/1.1 200 OK Content-Type: text/html Last-Modified: Wed, 19 May 2021 14:09:18 GMT Accept-Ranges: bytes ETag: "0bb8c8eb84cd71:0" Server: Microsoft-IIS/7.5 Date: Mon, 01 Nov 2021 13:59:57 GMT Connection: close Content-Length: 22138 <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="utf-8" /> <title>Ditai software official website-major c/c++Embedded training course</title> <meta name="keywords" content="Ditai software,c/c++,machine learning,Computer vision applications,Research and development of embedded vision application software" /> <meta name="description" content="Tang zuolin is committed to strengthening the education of students and practitioners c/c++Basic skills training helps employees master the core development skills needed by the enterprise. At the same time, detai software has strong R & D strength in the field of pattern recognition and intelligent system. It is specialized in one-stop services such as machine learning algorithm research and development, overall solution design of computer vision application system, and embedded vision application software research and development." /> <link rel="stylesheet" href="./static/css/style.css" /> <script src="./modules/jquery/lib/10.0.1.min.js"></script> <script src="./modules/superSlide/lib/2.1.3.js"></script> </head> <body> <div class="header" id="header"> <div class="wrap clearfix"> <div class="logo"> <a href="/"> <img src="./sources/logo.png" alt="" /> <h1>Ditai software </h1> </a> </div> <ul class="nav" id="nav"> <li class="item current"> <a href="./index.html" target="_blank">home page</a> </li> <li class="item"> <a href="./course.html" target="_blank">Course training</a> </li> <li class="item"> <a href="./detail.html" target="_blank">Course Details </a> </li> <li class="item"> <a href="./teacher.html" target="_blank">Teacher introduction</a> </li> <li class="item"> <a href="./college.html" target="_blank">Introduction to di Tai's future</a> </li> <li class="item"> <a href="https://dt4sw.ke.qq.com/ " target="_ Blank "> online class</a> </li> <li class="item"> <a href="https://Linxiit.taobao.com "target =" _blank "> Taobao store</a> </li> <li class="line"> <i></i> </li> </ul> <div class="taggs"> <img src="./sources/taggs.png" alt=""> </div> </div> </div> <div class="banner" id="banner"> <div class="list"> <ul> <li> <img src="./sources/banner-191215-1.jpg" alt="" /> </li> <li> <img src="./sources/banner-191215-2.jpg" alt="" /> </li> </ul> </div> <div class="dots"> <ul></ul> </div> <div class="prev"></div> <div class="next"></div> </div> <div class="home-tags"> <div class="wrap"> <div class="main"> <ul class="list clearfix"> <li class="item"> <div class="cell"> <img src="./sources/tags-191215-1.png" alt=""> <h3>Boss Teaching</h3> <p>Former Motorola architect and AI expert</p> </div> </li> <li class="item"> <div class="cell"> <img src="./sources/tags-191215-2.png" alt=""> <h3>Practical course</h3> <p>Design courses according to the needs of enterprises</p> </div> </li> <li class="item"> <div class="cell"> <img src="./sources/tags-191215-3.png" alt=""> <h3>Free trial</h3> <p>A lot of free trial content</p> </div> </li> <li class="item"> <div class="cell"> <img src="./sources/tags-191215-4.png" alt=""> <h3>cost performance</h3> <p>The course price is less than one twentieth of that of offline training institutions</p> </div> </li> </ul> </div> </div> </div> <div class="home-hots"> <div class="wrap"> <div class="head"> <h2>Popular online courses</h2> <a href="javascript:;" target="_blank">See more ></a> </div> <div class="main"> <ul class="list2 clearfix"> <li class="item"> <div class="cell"> <div class="img"><a href="https://ke.qq.com/course/176295?taid=985334217355431&tuin=157aae4e" target="_blank"> <img src="./sources/cource-a3.jpg" alt=""> </a></div> <div class="exp"> <h3><a href="https://ke.qq.com/course/176295? taid=985334217355431&tuin=157aae4e" target="_ Blank "title =" "> QT experimental analysis tutorial</a></h3> </div> </div> </li> <li class="item"> <div class="cell"> <div class="img"><a href="https://ke.qq.com/course/223662?taid=1342112855714222&tuin=157aae4e" target="_blank"> <img src="./sources/cource-a2.jpg" alt=""> </a></div> <div class="exp"> <h3><a href="https://Ke. QQ. COM / course / 223662? Taid = 1342112855714222 & tuin = 157aae4e "target =" _blank "title =" "> detailed explanation of Linux kernel linked list</a></h3> </div> </div> </li> <li class="item"> <div class="cell"> <div class="img"><a href="https://ke.qq.com/course/175018?taid=878303632337834&tuin=157aae4e" target="_blank"> <img src="./sources/cource-a4.jpg" alt=""> </a></div> <div class="exp"> <h3><a href="https://ke.qq.com/course/175018? taid=878303632337834&tuin=157aae4e" target="_ Blank "title =" "> Advanced C language course</a></h3> </div> </div> </li> <li class="item"> <div class="cell"> <div class="img"><a href="https://www.bilibili.com/video/av75437389?from=search&seid=3113614819828990758" target="_blank"> <img src="./sources/cource-a5.jpg" alt=""> </a></div> <div class="exp"> <h3><a href="https://Www.bilibilibili. COM / video / av75437389? From = search & seid = 3113614819828990758 "target =" _blank "title =" "> practical tutorial on data structure</a></h3> </div> </div> </li> <li class="item"> <div class="cell"> <div class="img"><a href="https://ke.qq.com/course/278029?taid=1886963817004557&tuin=157aae4e" target="_blank"> <img src="./sources/cource-a1.jpg" alt=""> </a></div> <div class="exp"> <h3><a href="https://ke.qq.com/course/278029? taid=1886963817004557&tuin=157aae4e" target="_ Blank "title =" "> C + + deep parsing course</a></h3> </div> </div> </li> </ul> <ul class="list clearfix"> <li class="item"> <div class="cell"> <div class="img"><a href="https://ke.qq.com/course/176295?taid=985334217355431&tuin=157aae4e" target="_blank"> <img src="./sources/cource-a3.jpg" alt=""> </a></div> <div class="exp"> <h3><a href="https://Ke. QQ. COM / course / 176295? Taid = 985334217355431 & tuin = 157aae4e "target =" _blank "title =" "> QT website</a></h3> <ul class="di"> <li class="fee"> <i class="icon-ios"></i> <span>9.00 element</span> </li> <li class="hits"> <span>2741 People have learned</span> </li> </ul> </div> </div> </li> <li class="item"> <div class="cell"> <div class="img"><a href="https://ke.qq.com/course/223662?taid=1342112855714222&tuin=157aae4e" target="_blank"> <img src="./sources/cource-a2.jpg" alt=""> </a></div> <div class="exp"> <h3><a href="https://ke.qq.com/course/223662? taid=1342112855714222&tuin=157aae4e" target="_ Blank "title =" "> linux kernel linked list</a></h3> <ul class="di"> <li class="fee"> <i class="icon-ios"></i> <span>9.00 element</span> </li> <li class="hits"> <span>2741 People have learned</span> </li> </ul> </div> </div> </li> <li class="item"> <div class="cell"> <div class="img"><a href="https://ke.qq.com/course/175018?taid=878303632337834&tuin=157aae4e" target="_blank"> <img src="./sources/cource-a4.jpg" alt=""> </a></div> <div class="exp"> <h3><a href="https://Ke. QQ. COM / course / 175018? Taid = 87830363237834 & tuin = 157aae4e "target =" _blank "title =" "> Tencent C</a></h3> <ul class="di"> <li class="fee"> <i class="icon-ios"></i> <span>9.00 element</span> </li> <li class="hits"> <span>2741 People have learned</span> </li> </ul> </div> </div> </li> <li class="item"> <div class="cell"> <div class="img"><a href="https://www.bilibili.com/video/av75437389?from=search&seid=3113614819828990758" target="_blank"> <img src="./sources/cource-a5.jpg" alt=""> </a></div> <div class="exp"> <h3><a href="https://www.bilibili.com/video/av75437389? from=search&seid=3113614819828990758" target="_ Blank "title =" "> Tencent data structure</a></h3> <ul class="di"> <li class="fee"> <i class="icon-ios"></i> <span>9.00 element</span> </li> <li class="hits"> <span>2741 People have learned</span> </li> </ul> </div> </div> </li> </ul> </div> </div> </div> <div class="home-ad"> <div class="wrap"> <div class="img"> <a href="https://v.douyu.com/author/rZwYYPy2owbK " target="_blank"> <img src="./sources/home-ad.jpg" alt=""> </a> </div> </div> </div> <div class="home-student"> <div class="wrap"> <div class="head"> <h2>Ditai software C/C++Practical course</h2> <p>There are few such detailed and in-depth on the market C/C++Technology courses to truly meet the needs of enterprises </p> </div> <div class="main clearfix"> <div class=""></div> <div class="table"> <div class="thead"> <ul class="tr"> <li>full name</li> <li>remarks</li> </ul> </div> <div class="tbody"> <ul class="tr"> <li>Classmate Zhao</li> <li>I've been convinced by ZTE offer,Finally went to Shenxin, with an annual salary of 19 w</li> </ul> <ul class="tr even"> <li>Classmate Feng</li> <li>Get Nubia offer,Driver protocol development</li> </ul> <ul class="tr"> <li>Classmate Li</li> <li>Undergraduate students (Electronics Major) get 9 K Game server development</li> </ul> <ul class="tr even"> <li>Su Tongxue</li> <li>One, Shenzhen Jixiang Tengda Technology Co., Ltd., embedded software development engineer (8) K)</li> </ul> <ul class="tr"> <li>Classmate Yang</li> <li>Undergraduate, get 11 K Work in the financial industry. From the original 3.5K Job hopping to 11 K</li> </ul> <ul class="tr even"> <li>Classmate Gao</li> <li>Graduate students have won many outstanding enterprises offer,Such as Hisense Haikang Huichuan technology jinshanyun nxp</li> </ul> <ul class="tr"> <li>Classmate Zhu</li> <li>MECO Intelligent Technology offer</li> </ul> <ul class="tr even"> <li>Classmate Zhang</li> <li>One, get 5.5K Embedded Software Engineer (Xiamen)</li> </ul> <ul class="tr "> <li>LV classmate</li> <li>(Medical equipment) used to work in a small company with a salary of 5 k. Change jobs to a listed company and get 15 K</li> </ul> <ul class="tr even"> <li>Classmate Zhang</li> <li>Graduate student, got the certificate of beacon communication offer,Wage confidentiality</li> </ul> <ul class="tr "> <li>Classmate Wang</li> <li>Master, mtk Drive, Ledo interactive interview passed, Tencent interview passed</li> </ul> <ul class="tr even"> <li>Classmate Li</li> <li>Didi is convinced that Haikang ZTE Zhaoyin network technology is ready to sign Didi, but others refuse</li> </ul> <ul class="tr "> <li>Classmate yuan</li> <li>Get Huawei offer,Salary confidentiality</li> </ul> </div> </div> </div> <div class="online"> <a href="tencent://Message /? UIN = 360361550 & menu = yes "target =" _blank "> I want a high salary</a> </div> </div> </div> <div class="home-case" id="homeCase"> <div class="wrap"> <div class="head"> <h2>Enterprises recognize that students' trust is the conscience education institution</h2> <p>The rich knowledge reserve makes the students feel like a duck to water in the work of the enterprise</p> </div> <div class="main"> <div class="list"> <ul class="clearfix"> <li class="item"> <div class="cell"> <div class="mode"> <div class="img"> <figure> <img src="./sources/home-case-1.jpg" alt=""> </figure> </div> </div> </div> </li> <li class="item"> <div class="cell"> <div class="mode"> <div class="img"> <figure> <img src="./sources/home-case-2.jpg" alt=""> </figure> </div> </div> </div> </li> <li class="item"> <div class="cell"> <div class="mode"> <div class="img"> <figure> <img src="./sources/home-case-3.jpg" alt=""> </figure> </div> </div> </div> </li> <li class="item"> <div class="cell"> <div class="mode"> <div class="img"> <figure> <img src="./sources/home-case-4.jpg" alt=""> </figure> </div> </div> </div> </li> <li class="item"> <div class="cell"> <div class="mode"> <div class="img"> <figure> <img src="./sources/home-case-5.jpg" alt=""> </figure> </div> </div> </div> </li> <li class="item"> <div class="cell"> <div class="mode"> <div class="img"> <figure> <img src="./sources/home-case-6.jpg" alt=""> </figure> </div> </div> </div> </li> <li class="item"> <div class="cell"> <div class="mode"> <div class="img"> <figure> <img src="./sources/home-case-7.jpg" alt=""> </figure> </div> </div> </div> </li> </ul> </div> </div> </div> </div> <div class="home-faq"> <div class="wrap"> <div class="head"> <h2>common problem</h2> </div> <div class="main"> <ul class="list clearfix"> <li class="item"> <div class="cell"> <h3>How much do you charge?</h3> <p>You can feel the quality and service of the course for less than 200 yuan</p> <a href="javascript:;" target="_blank">View details</a> </div> </li> <li class="item"> <div class="cell"> <h3>Can Ditai software pay in installments?</h3> <p>Installment payment can be made, and three interest free payments are supported</p> <a href="javascript:;" target="_blank">View details</a> </div> </li> <li class="item"> <div class="cell"> <h3>How long can Di Tai learn software?</h3> <p>Everyone's situation is different, which is related to their time and efficiency, and your previous learning.<br>If you care about how long you can find a job, you can consult your teacher and give relevant suggestions according to your specific situation</p> <a href="javascript:;" target="_blank">View details</a> </div> </li> <li class="item"> <div class="cell"> <h3>How to learn the course of Ditai software?</h3> <p>It mainly focuses on on-demand content. It supports free switching and viewing of up to three devices. Now the player supports windows,mac,ios,Android. Specific how to operate, you can consult the teacher</p> <a href="javascript:;" target="_blank">View details</a> </div> </li> <li class="item"> <div class="cell"> <h3>What courses do Ditai software have?</h3> <p>Contains C Introduction to language, C Language purchase courses, C++Depth analysis course, data structure practice course,qt Experimental analysis course, Mr. Tang's private dishes, Makefile Detailed explanation, operating system principle and implementation, written test questions.</p> <a href="javascript:;" target="_blank">View details</a> </div> </li> <li class="item"> <div class="cell"> <h3>Is it suitable for me to switch to software development?</h3> <p>Everyone's situation is different. We don't think everyone is suitable for changing careers in software development. If your situation is special and makes you worry about the possibility of changing careers, don't make an impulsive decision. You might as well talk to the teacher about your situation and listen to the teacher's suggestions</p> <a href="javascript:;" target="_blank">View details</a> </div> </li> </ul> </div> <div class="foot"> <div class="online"> <a href="http://" target="_ Blank "> ask the teacher, are you suitable for software development</a> <span>Promise not to sell courses or disturb users. You can consult at ease</span> </div> </div> </div> </div> <div class="footer" id="footer"> <div class="wrap clearfix"> <div class="sublog"> <img src="./sources/logo.png" alt="" /> <dl> <dd>Contact address: No. 165, Yingbin Avenue, Jinniu District, Chengdu, Sichuan</dd> <dd>Tel.: 13327212778</dd> </dl> </div> <ul class="subcode"> <li> <img src="./sources/rcode-1.jpg" alt=""> <h3>Plus wechat, one-to-one consultation</h3> </li> <li> <img src="./sources/rcode-2.jpg" alt=""> <h3>WeChat official account</h3> </li> </ul> </div> </div> <div class="bottomer" id="bottomer"> <div class="wrap"> <div class="copyright"> <p>Copyright: Ditai software Filing No.:<a href="http://Www.beian.miit.gov.cn "target =" _blank "title =" Filing No. "> Shu ICP Bei No. 20001175</a></p> </div> <dl class="sublink"> <dt>Links:</dt> <dd><a href="https://ke.qq.com/?tuin=157aae4e" target="_ Blank "title =" Tencent classroom "> Tencent classroom < / a > < / DD > <dd><a href="https://Chuanke. Baidu. COM / "target =" _blank "title =" Baidu teaching "> Baidu Teaching < / a > < / DD > <dd><a href="https://chuanke.baidu.com/" target="_ Blank "title =" Baiwen "> Baiwen < / a > < / DD > <dd><a href="http://Www.openedv. COM / "target =" _blank "title =" atom STM32 "> atom STM32 < / a > < / DD > <dd><a href="http://www.elecfans.com/" target="_ Blank "title =" e-enthusiast "> e-enthusiast < / a > < / DD > <dd><a href="https://www.csdn.net/" target="_blank" title="CSDN">CSDN</a></dd> <dd><a href="https://c. Runoob.com/compile/11 "target =" _blank "title =" rookie online compilation "> rookie online compilation < / a > < / DD > <dd><a href="https://www.360dhf.cn" target="_ Blank "title =" Bumblebee "> Bumblebee < / a > < / DD > <dd><a href="http://Www.360dhf. CN / dhfplayer. HTML "target =" _blank "title =" player "> player < / a > < / DD > </dl> </div> </div> <div class="foxbar" id="foxbar"> <dl class="list"> <dd> <a href="javascript:;" class="wx"></a> <div class="drop"> <ul class="rcode"> <li> <img src="./sources/rcode-2.jpg" alt=""> </li> </ul> </div> </dd> <dd> <a href="javascript:;" class="tel"></a> <div class="drop"> <ul class="tel"> <li> <p>TEL: 133-2721-2778</p> </li> </ul> </div> </dd> <dd> <a href="tencent://message/?uin=360361550&Menu=yes" class="online"></a> </dd> <dd> <a href="javascript:;" class="que"></a> <div class="prop"> <div class="inner"> <div class="fo"> <a href="javascript:;" class="close">close</a> <h2>Consult us</h2> <ul> <li>c/c++What are the development prospects?</li> <li>How is the course learned?</li> <li>choose C/C++still java?</li> <li>Is my situation suitable for software development?</li> </ul> </div> <div class="go"> <a href="tencent://message/?uin=360361550&Menu=yes" target="_ Blank "> consult your teacher now</a> </div> </div> </div> </dd> <dd> <a href="javascript:;" class="itop"></a> </dd> </dl> </div> <script src="./static/js/app.js"></script> </body> </html> recv bytes = 22463