27.Linux network programming socket becomes tcp high concurrent thread pool udp

Okay, let's start the class. Starting today, we will talk about 8 days in a row. Network programming is still taught in the Linux environment. Let's first take a look at what we have talked about in these 8 days, and sort it out with everyone. You first have A general impression, don't remember these, then there are 8 days of network programming classes, right? Then in the first 5 days, we will talk about the basic course of Linux network programming. The first three days are about concepts plus programming. Then the concepts will be mentioned to everyone every day, because there are many concepts and some theoretical things. , so we split it up, we can’t say that the concept is widely discussed on this day, right? It is boring for everyone to hear such words.

 So what do you learn every day? On the first day, I will talk about some basic concepts, such as what is a protocol? What are the common protocols, and for example, what are the 7-layer model and the 4-layer model? I will tell you about it on this day. This is the first day and then later, and finally we will tell you about the API functions of the socket, and then we write a client and a server to test it, and then it is over. Of course, on the first day, we also talked about concepts such as big-endian byte order and little-endian byte order. These are some basic concepts that require you to master. Well, when I talk about it, when I tell you, um, I will let you understand some things, and let you understand if there are things, and you must master them when you have something. It doesn't matter, for example, the protocol, what TCP protocol, UDP protocol, what ARP, RARP and so on. You really don’t need these protocols to write code, but why do you say these? One is that you need to master these basic concepts.

Another one may be asked during your interview, is it that you can’t just say that you can adjust a few functions, and then you don’t know the concept at all, that’s not okay, right? Then the next day, I will tell you about the 3-way handshake or 4-way wave, so what is the 3-way handshake? When the connection is established, the bottom layer of our kernel, hey, there is a 3-way handshake process. Note that this is implemented by the bottom layer, not by you. Then there is a 4-character waving, which is a 4-character waving process when disconnecting the link.

After talking about this, I will tell you about high-concurrency servers. When we talk about high-concurrency servers, we will start with what you are familiar with, because did we just tell you about the basic socket API on the first day? is basic? Then I will tell you about a high-concurrency one the next day, so what model to use, we have a multi-process version and a multi-threaded version, have we talked about multi-process before? We have also mentioned multi-threading, have you also said it? Then we make the application,

On the third day, I will tell you about the tcp state transition diagram. Please pay attention to what is the main part of this part? For example, it may be useful for interviews, or when you troubleshoot. Do you know what it means? You need to understand this part, and you don’t need to memorize it by rote, so before the interview, you should look through this to see if you can ask questions during the interview, and how many questions can you answer? That's it, isn't it? After talking about this, it is important to talk about other models of high-concurrency servers. What are the main ones? Multi-channel IO multiplexing, multi-channel io use.

Then there are 3 models in total, select-poll-epoll, among which select and epoll are what you need to master, especially epoll, especially for this pool, you can just understand the content, because it is borrowed from these two The one in the middle, if you say it has high efficiency, it is not high, and if you say it is low in efficiency, it is similar to it. In fact, there is no substantial change between this pool and this select in essence, and they are almost the same in essence. Under our linux operating system, we mostly use this epoll model because of its high efficiency.

Then on the fourth day, I will tell you about this, and I will introduce it to you temporarily. On the fourth day, I will tell you how to use this epoll, and let's write a case to test whether it can support high concurrency, whether multiple clients are connected at the same time, and see if it can be handled. On the fourth day at the end, I will tell you about the epoll reactor at the last time. When we play this part, I estimate that more than half of the people should be confused, because the more difficult this part is, then when you listen to it When listening to this piece, focus on listening to his thoughts instead of digging into the details, did you listen? Remember to make it clear that there are differences in this strategy. Some of them you need to master and you can write them down, while others you just need to read their thoughts.

Then on the fifth day, I will tell you about the thread pool, so this thread pool must pay attention to the thread pool. Have we talked about this multi-threaded programming before, right? If you have some basics in this area, it will be easier for you to listen to this thread pool. This part mainly uses mutex plus conditional quantity. I ask everyone if there are so many threads, do you want to operate shared resources? Is it necessary to use a mutex to operate a shared resource? Is it between that many threads? When they communicate, do we sometimes need this to let a certain thread wait for a certain condition to mature before executing it? What did you use at this time? Condition variable isn't it? I won’t go into details about this now, but I’ll talk about UDP and local socket communication later on. Have you mentioned this word before? Where did you say that? Did the corresponding party tell you this when they communicated before entering the city? Well, on the sixth day, I will tell you about libevent. This libevent is a third-party network framework. As long as you learn how to use it, it will be OK. You can learn how to use it.

Then when we talk about some commonly used APIs, and tell you about its ideas, let’s write one of the codes, and analyze two of the codes. This is libevent,

On the seventh and eighth days, let's talk about a project, a small project, so this project lasts for two days. We mainly write a web server. Think about what a web server does? Is it the one that receives the web request and processes the request? So is it the web server that we visit with the IE browser? yes, huh? Then you can use this browser not only IE, Firefox, 360, etc., as long as it is a browser, it can actually communicate with the web server. Of course, whether this is the IE browser or other browsers, what protocols are used? What protocol is used to communicate with the server? HTTP protocol, which belongs to a protocol of the application layer.

Okay, this is the 8-day course schedule for everyone, so let's see what we will talk about today? What are you talking about today? You need to have a general understanding of this first, you don’t need to memorize it rote, let’s talk about it every day. Okay, let's take a look at today's data goals. First, let me tell you about the osi seven-layer model and the tcp/ip four-layer model structure. Please pay attention to the requirements of these two models, and you still need to remember this. Why do you need to write it down? During the interview, ask, is it clear? As long as you do web server development, basic knowledge like this may ask you, and now you don’t have to memorize it by rote, what should you do? Before you apply for an interview, for example, if the company you are interviewing for does this and wants to develop a background service, you need to take a look at this. If you are looking for a job, you are doing game development and using qt You can see it or not, you know what it means. So before your interview, do you want to focus on your nature of finding the job, or do you need to do a targeted review? Is it because you have been doing qt development in the first place, so it doesn't make much sense to review linux thoroughly? If you know what it means, you must review it in a targeted manner.

 Then the second one will tell you what is a protocol, the protocol and the format of common network protocols, such as TCP protocol, UDP protocol, ARP protocol, etc. Everyone pay attention to these requirements and you have to understand why, because for example The person you interviewed asked you, so how many protocols do you know, and what does each protocol do? Is it enough for you to name a few? Don't be unable to tell at that time, right? At least after we finish these 8 days, you have to know at least two, which two are TCP UDP? Of course I know, two is not enough, is it? Okay, let's talk about the conversion between the network byte order and the host byte order later. We have some common functions, and you have mastered these functions.

Furthermore, you have to understand what network byte order is, and what is host byte order. This concept must be clear until the following four parts. First, let me tell you about the communication process of TCP and UDP. After talking about this process, we will Just compile the code for everyone, write a code, and see if the server and client we wrote can communicate. Of course, before we talk about the code, we will definitely introduce you to something? Commonly used API functions. You may not be able to remember these functions at the beginning, but it’s okay, because we use them every day, is it okay if you can’t remember them once, and it’s okay if you can’t remember them twice? Because from the first day to the eighth day, we will probably write this code at least 10 times. If you can’t remember it, it’s impossible, isn’t it? Unless you really haven't written or heard of it, have you? classmate? You don’t have to worry about this, because there are more functions, you don’t have to worry, we use it every day, how could you not remember it? Okay, so let me tell you about the 8-day course schedule and our learning goal today.

Take a look, and I will introduce you to the first basic concept of the network, the first protocol, and the protocol.

 First, let’s look at the concept of the agreement. This thing is summed up by myself, and you don’t need to memorize it. You should first understand it and see if you can understand it. What does agreement mean? That is to say, the agreement is agreed in advance, pay attention to it, it is agreed in advance, a set of rules that everyone abides by, the key words here, the first is agreed in advance. Is the second rule a common rule? In fact, such things are very common in real life. Is it a rule to believe in this traffic light? What rules? What are you doing at the red light? Stop, green light go first, amber light, right? Intermediate state? Yes or no? You can pass it on quickly, can't you? Of course, it is not recommended, and it is not recommended. From the perspective of the application, the protocol can be understood as the rules of data transmission and data interpretation. Can this sentence be understood? From an application point of view, protocols can be understood as rules for data transmission and data interpretation.

Well, for everyone, then you say that when the two parties are communicating, then you say that server a sent 10 bytes of data to server b, right? So if server b only receives 5, can he read all the contents? No, then people send 10, you receive 10, people send 20, you receive 20, then what does each part of the data mean? So it needs to be agreed in advance, do you understand? The event needs to be agreed, and if I don't make an agreement in advance, the other party will not know what you mean.

Here I will give you a simple and simplest example. Look at this student. For example, this is a picture sent by machine a to machine b. Let's take the simplest example as an example. Okay, so if a wants to send it to b, does a have to tell b how long the string is? Don't you tell him how much you charge? Yes or no? Okay, for example, he stipulated the first four characters and the first four bytes to indicate the length of the latter, is that okay? Well, for example, I have a 10 here, so if this 10 occupies four bytes, is it 010? Well, okay, here is the ten-byte data, ten-byte data, so first of all, can it send these four bytes, or of course you can send the whole thing to it? When the other party b is receiving, can he receive the first 4 bytes first, do you know what it means? This is called Baotou.

 We still have this concept later, he receives 4 bytes first, then after he receives 4 bytes, and then he calculates the length, does he know how many will be said later? Can you understand what this means? That's what it means, make an appointment in advance. Then, it can be subdivided into these 10 bytes of data. Well, it can be further subdivided. For example, in 4 bytes, it can have 4 bytes to represent the user id, 4 bytes to represent the discount code, and the latter to represent other information and so on. Can the other party receive these 10 characters, can it be split? These are the methods that are used frequently, and this is an application point of view. So what is an agreement? What is it actually? If you can’t understand the rules of data transmission and data interpretation, let’s talk about it step by step later, and you will gradually realize it when we talk about it. So what else can you think of about this in daily life, besides this traffic light? In daily life is not very much? Then I ask everyone, if we have classes, we have agreed to start class at 9:00, end at 12:00, start at 2:30 pm, and end at 5:30, right? rule? Yes or no? So teachers and students need to break up work, right? Is it the same for elementary school students? That's what it means, doesn't it mean that? What are these called? They are all called agreements, but to put it bluntly, they are rules and irregularities.

Let me ask everyone, and I will ask everyone again, if you talk about this agreement, is there at least two parties who abide by it? yes, isn't it? So if you are alone, so you set this rule for yourself, for example, you agree, for example, I wake up at 8 o'clock in the morning, then I have dinner at 8:30 o'clock, and then I go out at 9 o'clock, right? Then go home at 12 o'clock. Are these also a rule you set for yourself? What is it called? Similar to discipline, I set a set of rules for myself. If you talk about this rule, will you only try it out for yourself? Does anyone else use a set of rules? No need.

Everyone pay attention to what this agreement must be? At least both parties, for our class, this rule, this agreement, do both teachers and students have to abide by it? Of course, for students, there are many people waiting, these are called agreements, is it clear? Students, don't complicate the concept of this protocol, it's that simple, then you can also understand why each host uses a common language for communication between each host.

Let me give you an example here, what do you mean? So, for example, have you ever encountered such a situation while surfing the Internet? The webpage of your crazy book shows some garbled codes, or some things you can't understand. Do you know what it means? For example, when you, the machine on your side, or the browser on your side displays, what can you only display? This is in English or Chinese, but the data sent to you by the server of the other party cannot be understood by your browser. Are these words inconsistent with the agreement? Protocol mismatch.

But under normal circumstances, this kind of phenomenon of ours, this happens relatively rarely, does it happen relatively rarely, it must have not mentioned you, you, for example, your ie browser, when you access the web server, this The display is not normal, is there such a thing? For example, a bunch of garbled characters, this may often be a problem with the agreement, of course, isn't the agreement talking about something? It’s not about the ATP agreement. You two use different languages. They sent you English. Can you read the explanation in French? is not it? , Then you can give the simplest example, let’s say this, if the dignitaries of various countries visit abroad, do they need to bring an interpreter? Yes or no? Does the translation have to wait for at least two languages? You have to know Chinese, Chinese, and what else do you have to know? For example, in English, in this way, can you translate English into Chinese, etc. What do these belong to? They are all examples of agreements, so examples of agreements are very common in daily life, you can think about how many there are, can you cite which ones? Let's give you one.

What example? Let me give you an example, the ab server and the ab machine of both parties want to send files, and the files to be sent. Let's draw a picture of this in a moment. I will draw a picture for you directly. What is this? This is a machine. Students, this is a machine.

Okay, let’s have another one. This is machine b. This one is almost the same as the above example. Listen, what can you see? Can you understand? Well, now I have such a need. any request? What needs? I want to upload the files from machine a to machine b. Is this what we really use here is the ftp service? Do ftp friends still remember? What do you think about this? Its prototype is like this, machine a wants to send this file to machine b, it first sends a file like this, what is it sending? He first tells machine B which file he wants to send.

The first step, send a what? Is the filename ok? It’s up to you, it doesn’t matter, you don’t send the file name, you send other types, right? The file name is the first step. Then after machine b receives this file name, he will tell machine a that he has received it. Do you know what it means? If you are not well, he will tell him, so give him another one, right? What is this? What is this returning? Answer what you decide here yourself, let's give him this and answer OK, is that okay? Is it okay not to reply ok? You can answer one, it is up to you, answer, OK.

For the first time, that is to say, does Level B know which file Level A wants to pass on to him now? In fact, this is how the file is uploaded. After the other party knows your file name, he wants to create a file locally. Then after you send the data, he writes in the file. After writing, is the file equivalent to sending it? This way. Well, this is the first time, nice,

The second time you said what should you pass on? What should I pass on? Are you going to tell the other party how big your file is, right? good,

The second time is to pass the file size. Okay, after passing the size to him, for example, you stipulate that I will pass you a file size of four bytes, is that okay? Yes, you can decide on your own, and both parties need to negotiate. Then after the b machine receives the file size sent by the a machine, he will know how many data I will receive in a row next time, right? He just knew it. Then he also gave him something back? OK, I get it, I know how many you want to send me, don't you? Well, here's something for him too? OK, what is this? This is the second transmission, the second transmission. Well, at this time, at this time, the b machine already knows how much data the a machine will send to him. Well, with the third machine, a will really send data to him at the third time.

What is this rumored about? Classmates, what are you actually uploading? Is it the content of the file? The third time, transfer the content of the file, brothers, then when you transfer the content of the file, are you actually passing it in a while loop? He kept on uploading, he must have finished the file after the file was uploaded? What is this? After the transmission is over, what do you do after the transmission is over? Is it over? right? It's over after the pass.

Of course, how do I tell everyone that b knows that a has been passed on? How do I know? Because he already told him the file size because of the second step, right? For example, if I send you 1k, when he receives 1k, can he just close the link? Is the whole thing over? Brothers, our example, this example is a typical example of file transfer, can this be understood? Classmates, this kind of transfer of files between a and b, this kind of pass 3 times, the first transfer of the file name, the second transfer of the file size, and the third transfer of the file content, what do we say about this? ? It's a simple file transfer protocol, okay? is this the third time

this agreement? Is this agreement made by ourselves? This agreement was determined by ourselves. Later, this agreement was paid attention to. Later, this agreement has been continuously improved, improved, and optimized, and it has formed the ftp agreement we use today.

understand? Classmates, this is how this protocol came about, step by step, what upgrades, transformations, optimizations, etc., then this is equivalent to what I told you just now, you understand it as a prototype protocol for file transfer , should it be the most basic version? You just need to understand it that way. This transfers or uploads files from a, uploads files to server b, so what is needed? Need to see between a and b? Do students want to stipulate an agreement? Regulations or formulations, what do both parties need to pay attention to? What do you agree with? Rules, then this rule, what is it, is called the file transfer protocol. This protocol is a primary version of the FTP protocol. Which protocol is the file transfer out of? The classmate is ftp, a primary version of the ftp protocol, please pay attention, what happened after that? Constant improvement, right? Optimization, what is formed in the end? What kind of ftp protocol has been formed? What is it that I just said? It is an ftp protocol, a primary version, and a primary version of a file transfer
 

So let me ask everyone, can these three protocols of mine be optimized? These three steps, me, can the first step and the second step be combined into one step? That's ok, how do you decide on this? In short, you must let machine b know the agreement you set. If the protocol you set is to transmit twice, then machine b will receive three times. Can it receive it? Is there a problem? Students, in short, there must be something between your a and b? A negotiation mechanism, there is a negotiation between the two, in short, the two of them must abide by the same rules, right? It must be like this, and this is what we told you in this handout. Tell me about the same, how about you? This is enough to understand, right? What is this? The original version, the original agreement, then after continuous improvement, improvement, and processing, it finally formed a stable and perfect one, what? The file transfer protocol is the ftp protocol currently used.

 

 Okay, let me ask you again, then you said that if I have a machine, I have installed linux, and my b machine has installed windows

Can a and ba pass the file to b, is it possible? Yes, this agreement has nothing to do with the system, do you know what it means? It has nothing to do with the system. If you don’t believe me, let me give you an example, can you use Linux to surf the Internet? can it? The web server you visit may be, for example, it is Unix or something, other versions of linux or windows are possible. Is it possible that the server of Alibaba, for example, accessed by our windows is Unix or Linux, which is fine, it doesn't matter, this agreement has nothing to do with the specific operating system, and has nothing to do with the platform.

Let me add a sentence to you after figuring this out. Pay attention, what is the agreement? It has nothing to do with the specific platform, can you understand what it means? Let me give you the simplest example, can this c language be written under windows? Can it be written on Linux? Is it all right? So the c language is cross-platform, isn't it? But some languages ​​it can only run on Linux, so it's not cross-platform, is it? Doesn't this code work as soon as it is executed?

Well, that’s all we’ve said about the concept of this protocol. Then there are several common protocols. Let’s read the handouts here. Let’s read the handouts here. Have you opened your handouts? Let’s find a few for you in this big handout, so you need to know a few common protocols. You need to know one. For example, we have so many layers, a total of 4 layers, what are the transport layer, application layer, and network layer? , Network interface layer.

 

 What protocol does each layer have? This may not be known to you, let’s not talk about it, let’s take a look at these are common protocols, these are some protocols in the network, this section is more common, it is the TCP protocol, which is the network transmission control protocol. What is that called? For transmission control, this protocol is a protocol that we use a lot. If we do socket programming, should we use it on top of this protocol? Called Transmission Control Protocol? That is a link-oriented, what is reliable? Secure byte stream transfer, it is safe and secure.

Then there is another UDP protocol, which is called the User Datagram Protocol. This is an insecure and unreliable protocol that is not oriented to links. Are these two exactly a pair? So if you choose tcp, is it because it is safe and reliable? If you choose this, udp may be unsafe and unreliable, isn't it? There is also the protocol of the http protocol, we should know this, right? When the web server and the browser communicate with each other, the HTTP protocol is used. Its English name is called Hypertext Transfer Protocol. You must know this? You need to know? This is used a lot, and there is also the ftp protocol, have we already mentioned this, right? This is called a file transfer protocol. It is a protocol to transfer a file from machine a to machine b. Is it clear? There is also this IP protocol, what does this protocol mean? It's called the Internet Interconnection Protocol, and we'll say

There is also the icmp agreement, what is it called? Take a look at the control packet protocol. These two protocols are the arp protocol and the rarp protocol. We will talk about the arp protocol, which is called the forward address resolution protocol.

What's the meaning? Let me give you an example, we all need to know the mac address of the other party when we are performing data transmission, so if we don't know the mac address of the other party, we cannot perform data transmission. In this way, when we talk about the agreement, you will know if there is such an item in the format of the agreement. If you only know the IP of the other party, how do you know his Mac? Is the Mac a physical address? Yes or no? Students, your ip address can be changed, can your Mac address be changed? Is it impossible to change? Is your network card set for you when it leaves the factory? Yes or no? So what does ARP do? It is to find the Mac address of the other party through the IP address. That's it, is it clear? That's what you do.

There is also a rarp, which is just the opposite. What did you find through Mac? Find the ip, this is this, you have a look at these protocols, you have a look. We will talk about these 4 floors later, take a look, at least you, you, you, you, you, you, after we have taught a course, what do you need to know in it? TCP UDP, HTTP, FDP, right? IP protocol, what ARP protocol, etc., as long as you can name five or six, you can't just say it. I know TCP UDP, people wonder if you have learned network programming

You wouldn't say you only understand those few functions, would you? That's a bit too shallow, brothers, the 4-layer model, what I just talked about is the protocol, is it a concept?

 Students, an agreement is a set of rules jointly formulated by both parties, right? So in network communication, what does network communication represent? Indicates the two parties in communication, what are the two parties in communication? Called transferring data, called sending data and receiving data,

Called passing data, a set of rules for passing data and interpreting data. A set of rules. You can write according to your own words, in short, you can say it in your heart. Well, this is not dead answer, not dead answer,

Next, let's talk about the osi 7-layer model. Let's take a look at the model. Look at this picture. Students, I will summarize it here for you in detail. Whether it is your 7-layer model or the 4-layer model That’s okay, why I tell you this is mainly because I ask a lot during the interview, and sometimes people will also take these questions in the network test.

 

 If you don't understand it, you can't do it, right? Let's take a look at this network. Please note that the 7-layer model of this network is also called the osi model. What is it called? Its Chinese is open system interconnection, and its English is open system interconnection, right? Does Intel connection mean interconnection? Where is this Intel? Where have you seen the meaning of Intel communication? To communicate means to communicate with each other. Do you remember what IPC means? IPC.

Inter process communication. Is it the meaning of inter-process communication? Intel process? remember? This is interconnection, does it mean interconnection? So since it is said that the Internet is not certain, at least the two parties are not? Of course it could be multiple, isn't it? Then the International Organization for Standardization formulated the osi model, everyone think about this, you said the International Organization for Standardization, the formulation of the osi model was formulated by China? No, these many big rules in this world are not related to China, are they? Of course, now China has started to participate in the formulation of 5G standards, and it has been opposed by Australia. Have you heard of it? Huawei, right? Many cities have begun to deploy 5G, and many cities have begun to deploy. From 4G to 5G, has this changed? Seeing that there are not a few years, there are not a few years, then the model defines the standard of different computer interconnections, and the basic framework for designing and describing computer network communication lies in this 7-layer model. It is just an ideal model, and it is not used when it is actually used. Not this 7-layer model, which model, 4-layer model, is a 4-layer model. But although this seven-layer model is an ideal model, we still need to understand it. You just think about it like this and it’s over. Anyway, after you learn this, you don’t have to be afraid during the interview, right? know what it means? You have to have this motivation, let's take a look at what each model means.

The first physical layer, how about the physical layer that mainly formulates a lot of communication? Some physical communication equipment, for example, you know the cat, um, is it the modem, and the twisted pair, what is this called? This is called a communication medium, and is this optical fiber also an optical fiber? What twisted pair, are we using twisted pair? Students, do you still have optical fiber or something? And the cat Dom and so on, what are these included? In addition to this, I remember this phone, this, if you go to China Telecom to apply for this network, will they give you something back? Give you something back, what is it called, right? When connecting, if you are connected to telecommunications or mobile, does he know what it means to give a set of equipment? I forgot what the name is, those devices and so on, the most common device is cat dum.

What does Dum the Cat do? What are you doing? Friends, Cat Dem is used for analog-to-digital conversion and digital-to-analog conversion, so let me ask you, you said that your data can be sent through this network cable or through this light. Is it 01 that was sent? Can it still be 01? Is it 01, high or low level? If someone asks you, you, you must not make a mistake. If you send it, you say it is 01. You don’t know how they express 01, so it’s over, right? why? High stands for 1, isn't it the same? This means that this student is not this? High, what do you mean? Classmates, this is 1, this is 0, this is 1, this is 0, this is the one sent, and the 8, 8, and 10 are not one bit? Doesn't 8 bits represent a byte? Um, is it clear? Don't make a mistake about this. Whether it's twisted-pair wires, optical fibers, or cat dum, etc., what are these? They are all located on the physical layer, especially the cat dem, you need to know its function, you need it, if you want to connect to mobile and telecommunications, if you don’t have this cat dem If you don't, you will definitely not be able to access the Internet.

Don't even think about it, don't you think about it, it must be impossible? Cat Dum, what's next? Classmates, after yours comes in through the optical fiber of mobile or telecommunications, right? After this cable comes in, it is first connected to the cat Dem, and then connected to the router. If it is not connected to the router, can you directly plug it into your computer? It's okay, but in that case, can only one computer be plugged in? After you connect this router, can many people use it together? understand what do you mean? Can you put this router? Is it free wifi? Did you plug it in directly? Can you plug it in with a network cable? Wait, when everyone should go to school, in this dormitory, if there are 8 or 4 people in a dormitory, this is the way to use it, right? Is it all one way? Okay, so when this data is transmitted to this network, what does it convert into what? It is to convert the analog signal into a digital signal into an analog signal. Is the analog number just a code like 0101? In fact, what comes out of this network cable is a high level, what is this called? Is the analog signal 0101? What is it called? Digital signal. Okay, so when it is transmitted from the outside to the cat Dom, does he perform analog-to-digital conversion, to convert this analog signal into a digital signal, is it like this? It must be so.

You said this and I will ask you again, you said this when you surf the Internet on your computer, in the end your data sent to you by the outside, who finally received it? Which device is on your computer? Is it a network card? You have to explain this clearly. Okay, let's take a look at the first one, which is called the physical layer. The physical layer looks at the second data link layer. Then there is a very important word in the data link layer, what is it called? Data verification, when we talk about the Ethernet frame format later, you will see this. There is a layer of data verification, so what is the purpose of data verification? You can think about it, is it to see if there is any problem with this data, isn't it? Well, this defines the basic gear frame for network transmission. Please note that this frame is a network message format. As long as it is sent from your network card, it is in the format of an Ethernet frame. Let's talk about the Ethernet frame format later, and I will give you a detailed explanation. See that the network layer defines the network and defines the network.

Let me ask you again, do you know what is in this network from the inside? Where is the ip address? Is it at the network layer? ,Yes or no? Then two, the transmission path between the two hosts and point-to-point transmission, there is a heavy protocol called IP protocol, does the IP protocol have this ip address? Yes or no? When we talk about the format of the IP protocol later, I will explain it to you in detail as the network layer.

Then let me ask you again, there is a device in this network layer, do you know what device it is? There is a device or what is it called in the network layer. This device only parses the data of the network layer. Think about what device is used for this routing selection? What equipment? Is router selection a router? Who read the routing table ah? Your computer, your own brain can also be used as a small router, but if you are complicated, it will not work. Simple ones are fine.

When we communicate on the network, from machine a to sending data to machine b, often there are many layers in the middle? Is not so often through a lot of routers? A router is a bridge connecting two different network segments. Do you know what it means? I ask, let me ask you another question, one of us, one of us, in our class, do we use routers to communicate with each other? why? Because you are on the same network segment, do you understand the code? , because you are on the same network segment, isn't that the case? This, the transport layer has a protocol called TCP or UDP. We will tell you about these two protocols when we talk about programming. What are these two protocols called? In the transport layer, this layer is mainly to transfer data, so what about this? The end-to-end transmission is defined, so what about this? Network colleagues defined point-to-point transmission, so some students asked, what is point-to-point, and what is end-to-end? Let me explain to you here, this is my machine a, students, this is my machine b, right? Did you experience a lot of classmates in the middle, anyway, there are more than n, anyway, let’s not say how many, do you know what it means? Each of these values, each is called a node, clear? A node, then end-to-end refers to a to b, a and b, is it called end-to-end? Is it point to point? From a to this router, this is called a point, from this router to this router is also called a point, this is called point-to-point, clear code? That's what end-to-end and point-to-point mean.

Actually, can you see the literal meaning of this? Is the point right? Is the end both ends? right? The starting end and what? terminating end? This data is sent from end a to b, this end b, a and b are called two ends, so will there be many nodes in the middle? Is it true that each node is called a point?

What about the session layer? The channel for data transmission is established through the transport layer, which is called the session layer. These things exist, because we cannot directly perceive them. why? Because what are these? It is all implemented by the kernel for you. Did you do it? You didn't do it. Which layer are we standing on when programming? Standing on the protocol of the transport layer, this is what we are talking about. The programming of the socket a mentioned later is mainly about this, and there is also the application layer, and the presentation layer is the codec translation work.

Let me give you an example. If a web browser accesses this web browser and accesses a web server, I have gone through this translation between them. Do you know what it means? It has already been translated. For example, your server, your web server, what do you use to send it to the browser? UTF 8 encoding, so after the dialogue, if he said that he displayed Chinese first, would he still have to use UTF 8? So what does this browser use when sending you data? GBK, if you say that your browser cannot support UTF 8 format, can you display Chinese? Is it not displayed? It cannot be displayed, what do you understand? The two parties need a code and a translation job. Regarding this code, we will also mention it to you when we talk about project 1. What is a code and what is a code? Well, the latter application layer provides various services, so the ftp service is a program in the application layer, and there are ssh, email, etc. behind it, what are these? Both are application layer protocols.

Have you checked if this ftp is installed on your computer? Students, , is this ftp service? Which layer is this service actually on? Application layer, then think about it, which layer do you write the program on? Is it the application layer? Yes or no? Will you develop the latter thing? Students, what physical layer and data volume layer, do you develop this thing? Everything has already been formed, is it clear? When you use it, do you directly use the application layer protocol or something? Developing an application layer, a service program, generally so and so, so and so.

 Well, this is the 7-layer model, and I will sort out the 7-layer model for you. Here is a jingle, I made it up for everyone, you just memorize it. How about it? What layer is it? Which layer is it? What about digital networks? Chuanhui table, what else is there? What is the application of the online conference table? The application layer, the application is 7 layers, let's refine the physical layer for you, what is there in the physical layer? Communication media, such as what? Twisted pair, what else is twisted pair? Fiber is not it? Well, what other adjustments are there, is the regulator modemen? What is this modemen used for? Analog-to-digital conversion, and digital-to-analog conversion, understand? This is the second layer of the physical layer. Do you pay attention to whether the physical layer should be at the bottom? I write backwards, knowing what is good, what is this layer called? data link layer. What services does this layer provide? Is there data verification? Let me listen to you, you don't need to memorize it by rote, at least you should memorize the 7th floor first.

In the Internet of Things, what is the definition of the data link layer? Data format, what? Frame, data frame format, that is to say, what is sent from the network card is called a frame, and a frame is actually a formatted string. Just understand it like this, don't think about it too complicated. What is the agreement called here? Arp agreement, what else? rarp protocol. You have to remember one or two things clearly for each layer, so you don’t need to go up without knowing, what is the network layer, right? This defines the transfer of what to what? Is it point-to-point or end-to-end? Defined, point-to-point transmission, right? There is an important protocol at this layer, what is it called? IP protocol. Make it clear, when people ask you which layer your IP is on, you have to know the IP protocol, right? So there is a device called what device? Routers, don't get confused. OK, on ​​to the top. Brothers, the transport layer, what is the definition of the transport layer? Students, what is the definition of end-to-end? Transmission, end-to-end transmission. Then there is an important agreement called what agreement? Called tcp, what agreement? udp agreement is not it? Just remember this common thing

Okay, when we talk about the programming of this socket a in the transmission layer, the main thing is to write things clearly on this transmission layer protocol? OK, on ​​to the top. session layer. What is the main purpose of this layer? What establishes a session and what maintains a session? Establish a session and maintain a session? What is a session? Classmates, if the two parties, machine a and machine b, need to communicate, is there a conversation first? What do you understand it as? Relative to establishing a channel, can you understand? You are equivalent to establishing a channel, that is, if you want to be a classmate of others, do you have to establish a connection with others first? So what do you want to call someone? I want to call him before this transmission, this performance maintenance, is what the session does.

Then go up to the presentation layer, brothers? What are you mainly doing? What is the main work? For encoding, encoding and decoding, and translation, each layer has things to do well at each layer, and what is the next step? Application layer The application layer is mainly about these? What are the applications? Because do you understand the common fdp? Is ssh e mail? Email is also, that email is a service, email is a service, what else? http is not it? Also http too? You like this http protocol, can we use it directly? Let's talk about this later. This protocol was used in the two cases, called the http protocol.

Okay, here it is, this is the 7-story model. 7-layer model What do you mean by each layer of the model? Read it roughly. If you really can’t remember what each layer represents, don’t memorize it. You should remember the name of each layer first, you know? What protocol does each layer have? You have to know, well, let's take a look at this four-layer model next. It is the four-layer model we really use in the network process. Let's take a look at what's going on? Take a look at the first layer called the network result layer, then this network structure layer, which corresponds to the lower two layers of the 7-layer model, is it the physical layer and the data link layer? Can you understand the corresponding two layers? Okay, let's look at the network layer again. It corresponds to the 7-layer model, which is the network layer. What protocols are there in this layer? Is it an ip protocol? What equipment do you have? The devices working on this layer have routers, and then go up to the transport layer, so what is the corresponding 7-layer model? Transport layer, the transport layer in the 7-layer model.

 Okay, what are the two protocols in this transport layer? TCP and UDP, these two are the two most used protocols. Is it going up to the 3rd floor? Corresponding to the application layer? The top three layers of the 7-layer model correspond to the application layer, so what services does the application layer provide? What services does this have? Email is fine too, right? Whether ftp, ssh, or http are all called services, okay, let me ask if this service will exit? So what kind of process should the service be? Is it a daemon process? Brothers, take a look at our http service, haven't we never used it now? Classmates, it's useless, is he always on? Is the mail service doing the same? What else is ssh, so please pay attention to our connection, if I use a terminal device, if I use a terminal such as secureCrt, if I connect to this Linux virtual machine, do I use ssh? What do you think? Classmates, aren't these? ? Students, this is, can you understand? What's in here? It's all ssh.

 Okay, is this the ssh service? After you kill this, all your things will be turned off immediately. Try this yourself here, so we won't waste any time. Try it yourself. I remember that there was a student who used this terminal. He couldn’t connect to the virtual machine with this terminal tool. Later, he installed this ssh and it worked. Is it true that some people have not installed the above virtual machine, you have to install one, and it won’t work if you don’t install it, right? This, but we are talking about this thing, we have seen this thing, students, can I also connect this? My server?

This is not using ssh, which one to use? ftp, figured out, it's not the same. OK, so what are the protocols in each of these 4 layers? So what is the application level?

Telnet, FTP, email. Telnet is also a protocol ah

It is also a protocol. What transport layer has TCP UDP, and what does the network layer have? IP, ICMP, IGMP, what link layer has these, you can take a look, right? Every thing can be roughly said. Okay, let's give you a rundown and see what models are there in the four-layer model? Students, um, the four-story model, what is this called? This is called the TCP quadratic model. What is there? Classmates, what is the first one? The bottom network interface is the network interface layer, or the data link layer. The network interface layer goes up, the network layer goes up, the transport layer goes up, and the application layer corresponds to each corresponding relationship.

Tell you what this network layer corresponds to? Corresponds to what layer of the 7-layer model? Is the physical layer the physical layer and the data link layer, and then go up to the network layer, does the network layer correspond? Does it correspond to the network layer? This correspondence refers to the 7 model. You can see this clearly. Corresponding to the transport layer, and then going up, is it the last three layers, the upper three layers? Does the conversation mean it? session layer, presentation layer application layer. Well, here's the 7-story model and the 4-story model

, Then this piece of requirements for everyone is like this, you should figure out what is the name of each layer of the 7-layer model, and one more thing, you should also figure out what each layer is supposed to do, is there any agreement? Just figure it out? Let me tell you this when we write the code, you don't need it, understand? What is this mainly for you to deal with? Are you interviewing? Or how do we say this? Do you need to know these few knowledges when you learn this network programming? You can't just understand those functions, can you? Yes, let's talk about this, okay, friends, be quiet.

Okay, students, be quiet, what shall we talk about next? Tell me about the data communication process. For example, look at this picture. This picture shows that one of our computers A wants to send data to PC B. Brothers, are these two cross-network segments? Students, take a look, is the network very complicated? Don't care about whether he has cross-network segments or not. It doesn't matter whether there are cross-sites here. It's all like this, and it's all like this. For example, let's say that we are still not in the same network, or we may not be in the same network, so how does this a send data to b? Then I will tell you about this process. First of all, you have a look, so do you want to send data to the outside world? is that you? There must be a piece of data, right? There is so much data, take a look, this is the data part. Let's look at the data next. So let me ask you all the students, what do you say about our data distribution, right? Did we just talk about 4 models? 

So in these 4 layers, who has who first, who is first, who is behind, and your data will not be sent by the application program step by step? Yes, is that so? What is the first layer? Is it the application layer? Who prepared this data? Classmates, the data is equivalent to that prepared by the user program? Yes or no? That's definitely the case, well, the data part. Then, if the data is to be sent out, does it go through 4 layers, from top to bottom, and finally sent out by the network card? You figure it out, don't do it backwards, but isn't the process of ending just the opposite? Issued from the network card is not it? And then a little bit up, right? Ok, let's talk about the process of sending data first. Okay, now that the data is available, are you going to send it next? If you send it out, you have to go through 4 layers. How are these 4 layers added step by step? Well, the first step is like this, first use data, this matter needs to send data, this data. Alright, students, so what should we put in front of the data next? spell what? Do you have to go through the 4th floor? What to fight first? You must first spell the application layer and data, the application layer header or the fifth layer, do you know what it means? Did I tell you the 4th floor? What does the 4th floor mean? Your data part is the core, and the first few layers are put together for you bit by bit.

Let me ask again, did you do this process of fighting? Who didn't do it? The kernel does it for you. You have figured out that what you are doing is just preparing this data, which is what your program needs to do, and you don’t care about other things, including sending it out, and you don’t care about it, is it the network card that sends it out for you, right? Okay, this is the application layer, let's write it clearly for you, this is the data part, what part is this? Classmates, this is the application layer. You need to know which layers it has gone through, and then look at it next.

So what layer to go down from the application layer? Next is the transport layer. Is this the transport layer? Then there is a transmission layer, should you add the header of this transmission layer next? The header information transmission layer, okay, so that means that the application layer should be the transmission layer? So good, how did he do it? The kernel will stitch this part of the information of the transport layer to the front of the application layer for you, and it will look like this.

This is the transport layer, can you see what it means? Did the students add another layer? good? If you go further down, will it become the network layer if you go further down?

So in other words, is there another layer in front of the transport layer? Is it another layer?

What layer is this called? Network layer, take a look at this layer and I will analyze this process for you, is it a layer? OK, is it now the network layer? Then at the network layer, can your data be sent out? Brothers, did I say that in the last class, if your data is to be sent out, it must be sent out from the network card. What is the format of the data? Classmates, is it framed? That's dataframe format. OK, this is the network layer,

So where does the network layer go down? Is it the closest to the physical layer? Classmates, we are close to the physical layer, what happens when we go down? The network layer is called the network interface layer further down. You can call it any data volume layer. It doesn't matter. This is called the network interface layer. The data link layer can also be used, but the term is different.

Okay, so should we add another layer to the front? This is called the network interface layer. Alright guys, so are the 5 layers now there? So these 5 layers are all there, so what do you say your data should do next? Is the data ready? After preparation, this data is finally sent out by the network card? Your network card, who do you think is directly connected to the network card outside? Did you connect with the network card through double interception? Are you connected to the router? Or where did it go? Cat Dum is on it, right? Yes or no? That's Cat Dum, did you pick it up? Well, after your network card comes out, your network card data, when this data comes out of your network card, what is this data? Students, is it a data signal? Is it 0101, and then take this cat Dom, and convert this 01 into something? The high and low levels are sent out through the network cable, is this the case, can you understand? So through this picture, what can we think of through this picture? This data part, students, the data part should be our entire core, do you know what it means? In your network transmission, is it the data part that is transmitted in the end? So are the above things equivalent to control information? Is there any application layer, transport layer, network layer, network interface layer, are these all protocols? Classmates, is it all an agreement? Okay, so for example, what protocol does the application layer use? For example, if you use ftp, is it the FTP protocol? So good, you use the TCP method for the incoming layer, so does it use the TCP protocol? Fortunately, there must be some protocol at the network layer, the IP protocol, is this the next step? In the end, your network interface layer, should your data be encapsulated into a frame data frame format? In the end, the network card sends the data through the network cable. This is the case, so the process of our data from top to bottom is not a process of layer-by-layer packaging? yes, isn't it? Okay, let me draw a picture for you, the bottom is here, is this a layer-by-layer packaging process? Classmates, right? I'll write to you,

What is it from top to bottom? It's actually the sending process, right? Students, sender sender. What is this? Is it a layer-by-layer packaging process? is not it? this process. Students, this process is good, so I ask everyone, what about the receiver? The other party received the network card of the other party and received your data, is it this? Is this one at the bottom? Okay, let's see if he receives your data, this is, this is PC a, what's behind it? PC B, what did he receive first? is it this one? Is it this? Watch out for this, and up, brothers, up.

Then after he receives your data, after your network card receives the data, does he have to do some work? What job? For example, does the verification wait? What else is he going to do in the meantime? Did he unpack who? Do you want to remove the network interface layer for you? Can you understand? I'll remove it for you. After removing it, what's left? Is this the only layer? right? Let me put it here for you. Speaking of this, students, yours, um, I don’t think it’s good to draw like this, so let’s draw like this.

Is this the network layer now? The network layer passes through your network card. Did the students pass through the network interface layer? He will give you the data received by this network card on his network. It must be in frame format. He will first give you the network interface layer This header information will be removed for you. After removing it, will it go to the network layer next? In the network layer, is there only the network layer, transmission layer, application layer and data? Brothers, go up to the network layer, and then up, is it this layer? Pay attention to see if it is this layer? This layer, after unpacking the network layer, he removed the network layer. After removing it, is there only the above people left? Yes or no? Communication layer, application layer and data, isn't it students, look, is it at this layer? Well, are there only two floors left if you go up? Is it right? I picked it up for you directly, right? This is it. Of course, what layer is the teleportation going up? Students, it is the application layer. At this time, it is equivalent to whether your application has received data, right? For example, if you ftp, ftp, has the server received the data? Does he want to unpack the data package of the application layer? After unpacking, is the only data left at the end what you really want to send? Is it students, this is what you really want to post? This is the data part. So here I have a question for you to think about.

What question? Then your network, your mobile phone is recording traffic, students, right? For example, you only sent one word, so you can send it to the other party, right? One Chinese character per character, for example, two bytes. That’s right, students, so you said you, in fact, your traffic is two bytes? why? , because your program is going to go through a lot of packaging? After packing, there must be some control information in it, yes, then he was unpacked when the other party said it, isn't it like this? Then these control information also account for traffic, otherwise, the service provider will not lose a lot. Is it true that someone must have written down this part for you, do you know what it means? Then we talked about the process just now, what process should the students think about, what process should the receiver be? The receiver is layer by layer, is the receiver unpacking layer by layer? Right students? The receiver, this is a layer-by-layer unpacking process, can you understand? Students, the transmission process of this data is like this. This is the sender, so is it packaged layer by layer from the application layer down? good? So the receiver is from the bottom up, right? Of course, you have to know what is certain about receiving your data? Must be the network card first, right? Then go up to unpack layer by layer.

If you don't understand this very well, let me give you an example in real life, and you will understand. Then, for example, you want to send a letter, now, of course, you don’t write a letter now, right? Classmates, do you have WeChat now? What are you still writing? right? You are going to send a letter, so you have to send a letter. For example, if a boss wants to send a letter, if the boss wants to send a letter, is it possible that he wants this? He writes the letter, does he have to write the letter first? This is what he has to do, he needs this letter. Later, he called the secretary, and he said that Xiao Guo called a courier to send the letter to me.

Anyway, he doesn't care if the letter is written, right? Who did he send the letter to anyway? Gave this to his secretary, didn't he? After his secretary got the letter, would he first put the letter in the envelope? Is it equivalent to doing something? Is it the packing process? Put it in an envelope? So after putting it in the envelope, should he write the recipient's address, name and phone number on the envelope? Yes, then after writing the envelope, is he going to hand the letter to the courier? What did the courier do after receiving the envelope? Is he going to pack it in his package? For example, he has a box in the box, and then he puts the letter. After the courier finally receives the information, he should not take it away, should he take it to the courier center, and go to the place where the courier is sent Then go get it, and then pack all the information in the future? After packing, is this another packing process? Then after packing, he needs to put it in the car, put it in the car and lock the door, is it equivalent to a packing process? This is a layer-by-layer packaging process, right? Then after the freight driver delivers the goods to the other party, he will also send them to a city, for example, to Beijing, right? classmates? So what did you do first when you arrived in Beijing? Classmates, does opening this door mean starting to unpack? Open the door, take out the package, and then untie the package. After unpacking, it may not matter if there are still small packages in the package. You can still open it. After opening, you will open layer by layer, and finally take out the letter. After the letter arrives, should it be handed over to the courier and then delivered to the operator.

So is the whole process just a layer-by-layer packing and layer-by-layer unpacking process? Can you understand? Classmates, it is such a process, so if you don’t understand this, let me give you another one that you are interested in. When you have lunch, do you want to order food online? Good Meituan, for example, if you ordered Meituan takeout, first of all, the other party received your order, and from then on, after receiving your order, he knew that someone wanted to eat, right? After ordering, is he the chef who will stir-fry the dishes for you first? The process of cooking or fried rice, is it the process of preparing data? Well, it's ready, and then put it in the lunch box, isn't it equivalent to packing? Then put it in the lunch box, and then put it in a plastic bag, did you wrap it in another layer? What will be handed over after wrapping one layer? This food delivery person, after the food delivery person gets your meal, does he want to put it in the box behind his tram? After putting it in the box, is our whole process from preparing the fried rice to packing this, packing it into a box lunch, and then packing it into a plastic bag and handing it over to the courier? Is the whole process equivalent to layers? Layer packaging process? understand? Okay, so the courier, send this to this, where will it be delivered after it is delivered? Send it to the person who ordered the meal? Shouldn't his first step be to take the rice out of the box, or start unpacking it? Okay, after taking it out, he will give it to the person who ordered the meal. After the person who ordered the meal gets the meal, will he untie the plastic bag? After unpacking it, he checked to see if it was my meal. He opened the lunch box, oh, it was indeed my meal, so was it an unpacking process? The whole process is like this, can you understand? As for this example, here I am, what have I prepared for you? An example of this life, this example of sending express mail, this example of express delivery if you are interested in taking a look for yourself? The example of express delivery, that is to say, there are some things that you don't care about.

 Let me ask everyone, so you say the person you eat, the person you order, do you care who delivers it to you? Do you care who did it for you? What are these? It is equivalent to doing it in the background, right? So for us, for us, this part of our data is our own data. Do you care about the following application layer, transmission layer, network interface layer, and the process of sending this data out? Do you care? If you write code and even care about these things, let me tell you that 30,000 yuan a month is not much, is it? why? This workload is too much, and it is difficult to complete these. Has it been fully realized at the bottom layer, so what are you going to do? Write programs on the transport layer, so where is the transport layer? Is it true that the protocol you use on this layer is the TCP UDP protocol. The socket a programming we will talk about later is written on this layer, and you only need to write it here.

So what network layer is underneath? Do you control the network interface layer? Are you going to study this analog signal? digital signal? These are not the scope of your research, do you know what that means? , you can care about the things you should care about. Okay, so let's summarize the whole process for you at the end. There is only one sentence, just sum it up. data communication process. How to sum it up in one sentence? What is it on the sender side? What is it on the sender side? What are the data layers for? The packing process, on the receiving side, is a process of unpacking layer by layer. You can understand this sentence, is it clear? Then what I said, this picture has been drawn for you from the beginning to the end, you should be able to understand it, and you can understand the two processes of the students.

Okay, did I tell you about these 4 models in the whole process, did you tell you about each layer, what is the application layer, transport layer, network layer, and what network interface layer? We actually used 4 layers instead of 7 layers. Make it clear, but you know the correspondence between the 4th and 7th floors in your heart, and you are clear in your heart. In fact, this, um, after you read a lot of this, it is actually not difficult to remember. How to say? Take a look, what do I say about this layer?

This is the first floor, does it correspond to two floors? Is this all one floor? There are 3 floors behind, you only need to remember the seventh floor, and you can write it down. 2113 2 + 1 + 1 + 3 is not exactly 7 layers, so does it just correspond to this sequence, which is the network interface layer, network layer, transport layer, and application layer? This is not the case? Anyway, you don’t care how you memorize, you have to have a knack anyway, so clear that you have a knack.

Okay, speaking of which, you just have to take a look. Let's briefly tell you, how many design modes are there for this kind of network communication mode that we often see? In fact, some of you may know it, but you don’t know how to say it. Is one called CS and the other called BS? These are two common design patterns for network operation programs, right? Common design pattern? So what is CS mode? It is the client server mode, so this needs to deploy the client and server at both ends of the communication. Do you know what it means? This is obviously my thing SecureCRT, classmates, what kind of classmate am I, is this a client server? Yes or no? My Linux operating system? Is it a virtual machine? That is called server, this is called client, and this is called CS mode.

 Okay, look again, then I use a browser to access the web server, such as Baidu, what Taobao, Taobao Jingdong, etc., what do these belong to? It is called bs mode. What does b mean? Browser is called browser server mode, what is the protocol used? Http protocol, this is called bs mode, right? We don't have internet connection here, do we? Well, it’s enough for you to know about it. It’s called bs mode. These are two common modes, one is called cs and the other is called bs. Let’s take a look, then compare the advantages and disadvantages of these two modes. ? You just need to understand this, and you don’t need to memorize it by rote, or memorize it by rote.

Well, for example, for CS mode, let’s think about this CS mode, so what do you need to install locally? The client does not install the client, so if you want to install the client, then if the client has this malicious program or something bound, do you know? have no idea. Then this kind of user probability will affect the user's security.

Of course, for example, if you play this WeChat, can you also install this computer version? What can I wear? What are these mobile versions called? They are all called clients, and your mobile phone number APP is also called clients. Don’t be confused, are they all called clients? So these words, do you have to install them locally first, right? This machine, after installing this machine, can you communicate with this remote server? For example, WeChat chat, QQ chat, etc. are all fine, right?

So what's one bad thing about it? First of all, you need to install, do you need to install? Another one, do you know if there is any malicious program or virus on this client? Don't know, but it's good. Well, what's the good thing? He is stable and fast, but the premise of stability is that there is no virus, is it virus-free, is it fast? And can it cache data locally? Then you can think about QQ chat, and WeChat chat, including the picture you see, will the file you download be cached locally for you? Yes or no? This is its advantage, it can be cached locally and fast.

So what else is there? If the client interface of your client is done well, it will be very convenient for us to use, isn’t it? Such? Then think about it again, everyone, bad places, bad places, it is certain that you consume a lot of local resources, needless to say whether this is certain or not. If there is another one, think again, so do you need to develop this client? Let me give you an example. For example, if you have developed a set of software, such as Tencent, you said that he directly developed his server. Can he do it without providing a client? Ok? Do you know what it means? Because you write a server yourself, you use the CS mode, you only provide you to get a server out directly, you don’t provide the client to the other party, who knows how to use this client, does the server need you to open , you come to develop? Well, is that so? Think about it again, then this bs mode, is the client of bs mode developed by you? No, you just need to use a browser. As for whether you use IE browser, what Firefox, what 306 Baidu, etc., is it all right? Are they all following the same protocol? What protocol is called the HTTP protocol? understand? As long as there is an agreement on it.

I was thinking, so you said this, if we visit on this browser, will the efficiency be lower? why? Because it can't cache some data, do you need to lose it every time? Can it not be cached sometimes? Of course, some can also be cached, but it must be worse than installing the client locally, isn't it? And if you think about it again, is this agreement dead? Is it all http agreement? Can I switch to another agreement? It can’t be changed, please note that it can’t be changed, because of this, because this browser is not developed by you. Of course, if you are a web server, do you have to abide by this HTTP protocol? Can web browser access if we do not observe this? Can't access, do you know what it means? Not to mention the advantages and disadvantages of this, you and I just tell him so much, then let’s think about this http protocol, there is another advantage, what is the advantage? So when you develop, do you not need to develop this? Is the workload small, you only need to develop the server, or just develop the server? Furthermore, if you talk about this browser, is it limited by the platform? Do you accept it? So you said, can you visit Baidu on Linux?

Of course, is it possible for Windows? Is it all right on Apple? right? Students, the reverse is the same client, right? Students, can you install it under windows, can you install it on linux? can it? Certainly not, you don’t even need to think about it. Generally, when you download the client, it says for windows, for Linux, for Mac OS, etc., do you know what it means? Is it included? 64-bit and 30-bit are clearly explained to you. Don’t mess around. You are on your system. For example, if you have a 30-bit system, can you download a 64-bit system and install it? Can't fit it.

Can't fit it? Can 30-bit applications be installed on 64-bit systems? Yes, but not the other way around, understand? Not the other way around, your 64-bit applications cannot be installed on a 30-bit operating system, but 32-bit applications can be installed on a 64-bit operating system, right? Is it backward compatible? classmates? Good backward compatibility. This is the bs mode or cs mode of the client browser. Let's briefly talk about its priority analysis. You read this passage yourself.

good. Two common design patterns for this web application, the first C/S? The second B/S mode, right? Then write two sentences for you, advantages and disadvantages, in fact, are the advantages and disadvantages relative to each other? Brothers, what are the advantages of CS mode first? Students, is it fast, efficient, can be installed locally, can be installed locally, and can cache data, right? Data can be cached. In addition, what can I choose? The protocol is called flexible protocol selection, right? The choice of protocol is flexible. Brothers, take a look at this, what else is there? Advantages, right? Well, of course this display effect is not better? Because if you develop this interface, if you want to develop it better, is it more friendly for our users to use, and is it more friendly for users to use? How would you sum up this shortcoming in two points? Of course, does this client need and need to be developed by you? Client tools need to be developed by programmers, is the development cycle long? The workload is heavy, isn't it students, think about it, um, everyone can try it.

Do you say that the development cycle of a server is long, or that the development cycle of a client is long. Please note that sometimes the development cycle of a server is not long. It is clear that the real development cycle is long. Sometimes you can take a look at your Taobao client, see how many things are there, and then look at your Alipay, is there a lot? Yes they are a lot.

Of course, the amount of development of your client is large, and the server must be extended accordingly. Is it because they need to negotiate a lot of things and develop together? On your Alipay client, if you develop a set of software or an application for renting a house, do you need to make corresponding adjustments on your server side? Is it like this? It must be like this, because the development cycle is long and the workload is heavy, isn't it? So what are the disadvantages? It needs to be installed? Well, it needs to be installed locally, so it will have a certain impact on the customer's computer security. Because you didn't know if it was poisonous when you pretended it was poisonous, what if it was poisonous? , in case it happens to monitor your personal privacy, isn't it? For example, if you install this software, it happens to have such a function. It can read what is in some of your directories, file information, and then upload it to you? It is possible to upload to his own server. Do you know what it means? is allowed. For example, as long as you turn it on every day, it will send you what he is doing in the background, can you see it? Can't see it? Also no logs? You can't see it, well, look at this bs mode.

Is BS mode just a browser? what mode? Browser web server mode. OK, so what are the advantages, disadvantages? Classmates, first of all, do you know what is the biggest advantage? You don't need to develop this browser, you don't need programmers to develop the browser, you don't need to develop it, right? So what else? The development cycle, of course, if you don't need to develop this, of course the development cycle is not short. The development cycle is short, isn't it? Whether the workload is small is relative to you, you can just compare it.

So, so, so shortcomings, brothers, what are the shortcomings? Is the agreement to choose only what to choose? The only choice is the http protocol, right? Protocol options are limited, are there any disadvantages? Well, can't cache data, can't cache data, then right? This affects the use of customers, or the data cannot be cached, and the efficiency is affected. OK, you can list and write this yourself.

Well, that’s roughly what it means, do you understand? Classmates, what must it be? There are pros and cons, right? Of course, if we access the web server, do we have to use something? bs mode, right? On our seventh and eighth days, we will talk about this, um, web server development will tell you this, what will we use then? Of course, do we not need to write the browser? Visit him with a browser, visit a directory and read data? Ok, that's all for now.

Okay, everyone, open this handout, and let's look at the handout part. Let me tell you about this first. Did we tell you the basic concept of this protocol before, right? Okay, let's start talking about some commonly used protocols. Of course, this protocol does not mean that you can use it when you write code, but that you need to understand this protocol. understand? Let's talk about the most important protocol first, called Ethernet frame, so what is Ethernet frame? All the students send out from the network card are in Ethernet frame format, is it clear? They are all in Ethernet frame format, so this Ethernet frame is a protocol packaged in the network interface layer. Did you tell everyone that the network interface layer is also called the data link layer? Let's take a look at what kind of format this Ethernet frame is? See if you can understand.

 

 

 Well, the first ones are called Ethernet headers, this one is called Ethernet headers, these, and the latter ones are called data parts. Is it divided into two parts? Students, what does each part represent? What does each part mean? The header information has a total of 14 bytes. Please note that this is in terms of bytes. What do the first 14 bytes mean? The first 6 bytes represent the destination address. Think about it, does the destination address refer to the ip address or the Mac? How many bytes should the ip address be? How many 192168, what fraction? Is it in dotted decimal that there are four sets of data, that is four bytes, and that is four bytes, then the four bytes must be the ip address.

So what does it refer to in the network? In fact, it refers to an integer value. You will know later, so well, if you say that the 6 bytes you see must be a Mac address, is that a Mac address? You can see if the Mac on your system is 6 bytes, what do you think? Brothers, how do you see the ip address, is it ifconfig? Look at this, students, where is it? Is this a hardware address? The mac address is the hardware address, don’t get confused, okay, 00: 0c: 29: 7 F: 37: 1 B Some students said these, it’s not, there are 12 letters in total, why are they six byte? What's the meaning? How many bytes is 00? 00 is a byte, please pay attention to 00, this 00 is two numbers in hexadecimal, is the two numbers in hexadecimal mainly one byte? Well, in fact, there is 0x in front, students, isn’t 0x00 a byte? Isn't 0x0c also a byte? OK, so that's 6 bytes in total, 0C 297 F 1B , right? Hello everyone, then 1B, how much do you want to convert into decimal? How many? 1 This 1 refers to 16, right? What does b refer to? Is it 11 plus how much? 11 + 16=27, you have to be able to count, you have to be able to count, of course. We generally don't count hardware addresses, but this inet will. Well, it's the same as this principle. Is this 6 characters? So the destination address we said earlier refers to 6 bytes, which refers to the Mac address, so don't confuse it.

 Well, the original address is also 6 bytes, obviously it is also a Mac? Well, so what is purpose? What is the source? What is the purpose? Who are you going to send this data to? The Mac address of the destination host refers to the destination address, right? That is, where does the data end up, isn't it? So the original address? Obviously, where is this data sent from? For example, if my data is sent from host a, does the Mac address of host a refer to the original address? okay.

There are many types. Here I will tell you some of the more common ones. The type is two bytes long. Look at the type, if it is 0800, how many bytes is 0800? Look clearly, this is hexadecimal, isn't 0800 08 a byte? Is 00 also a byte? so it's two bytes? If the type is 0800, it means the ip data segment, is ip a protocol? Everyone remember that students, we talked about the process of data packaging, did we talk about it, is there something? What about the network layer? Well, are these all IP layer data at the network layer? Is it the data of the ip layer? Well, if 0800 means the ip layer, then the data of this ip layer will try forty-six to one thousand five, in fact, there are more than one.

what? The length of the ip datagram, so what about the Ethernet frame format? Its data length is 46-1500, do you know what it means? The maximum is no more than 1500. Well, how did this value come from? It is said that it is an experience value, do you know what it means? What is experience value? Is it through a lot of experiments to get a more appropriate value? You don't have to worry about it, anyway, if there is such a value, it's over. But the minimum value is stipulated, the minimum is 6 bytes, and the minimum value cannot be lower than 46.

Then there is a four-byte CRC behind, what does it mean? CRC check, so what is the check? What is checked is your previous data, right? Is that what you mean? Well, this is the 0800 IP segment, and its data length is 46-1500. I told you, let's take a look, if it is 0806, it means arp request arp request and reply, the protocol it uses is arp? What does the arp protocol do? Is it possible to obtain the other party's Mac address through the ip address? I'll tell you how I got it later. Then the length of this is 28 bytes. Please note that the data length of the arp request and response is 28 bytes. Obviously, it is not enough for 46 bytes? Then complete it later? Make up for as much as there is still a difference, how much is still worse now? Is it still 18 short? Then you don’t need to care about what to fill in. Anyway, you know that filling is enough. Is it okay to fill in the blanks? Yes, of course you made it up? Classmates, did you make it up? Make it clear, you didn't make it up, and then read on.

So if 8035 is rarp request and response, rarp, then this is also 28 bytes, and it needs to be filled later? Okay, so this is this, this is what I told you just now? Ethernet frame format, so is this really what you need to understand? Is it necessary to understand? , you have to know what the Ethernet frame format roughly looks like? For example, what do you mean by the first 14 bytes? 662, representing the destination address, source address and type. Then there will be 46-1500 next, right? Students, how long is this data at this time? Do you want to see its type? If it is arp, ask for its data type. Is its length the longest 46? Of course it has been completed, right? If it is an IP datagram, is it up to 1500? Wait, you have to be clear about this.

What is there in the back? CRC check, and CRC check this. So let's talk to you with a specific example. which example? Taking the arp request and response as an example, let me tell you what is going on with this Ethernet frame format? Then this means that this is the specific protocol of the Ethernet frame format. Of course, this is based on the arp protocol as an example. this. Classmates, is this whole thing (the red part) an Ethernet frame format, right? Students, of course this Ethernet frame is an arp request or response. Do you know what it means? It is a data format of arp request or response. Let's take this as an example to tell you. Here I have prepared this picture for you, ready to go. Well, let me delete these two for you first. First of all, this is done before, and then I will put these two up for you, and move this data down, so that we can say that it is more convenient to look at.

 

 

 It seems that this line has to be redrawn, so you know what's going on? Just let me draw a rough picture for you. So what do you mean? That is to say, does my host a want to send data to this host b?

So what is the current data? The data is sent from a to b, brothers, so how about sending it to b? What does he want to do? He wants to send arp request and response, of course, is the response sent to a by b? Is it my classmates? Okay, let's take a look at this format and see if you can understand it. Let me tell you about this format first. Okay, let's not talk about 662 before, right? , isn't it classmates? Now there are hardware types, what hardware protocols, and what protocol address lengths and ip address lengths. This does not mean that everyone needs to memorize this, as long as you can look it up and say it according to this format, don’t you need to memorize it clearly? ? There is no need to memorize this thing, and because of whether it is or not, we don’t need to write code, but it’s enough for you to understand this thing.

Is it my classmates? Okay, let me ask everyone, if you are asked to analyze this protocol, you are given a message and a piece of data. This piece of data is in this format, and you are asked to analyze it. Can you analyze it? How to solve it? Is it possible to intercept by bits? Do you understand what it means? For example, there are 6 in front of you, so if I read out 6 directly, will I know his Mac address? Will I know something after reading 6 more? My own what? The address of the original address. In fact, which function can be used to do this? Classmates, which function? This is the data, such as str, brothers, str.

So if I ask you to cut out the first three, the Ethernet destination address, original address and type, how do you cut it out? How to intercept? Isn't mamcopy over? Why are the students afraid to speak? mamcopy st r right? 6, Are the first 6 cut off? Then what? I added 6 to this S2, and cut another 6. Did I just cut out the middle one? Is it 12 more? Do you want to cut 2 more? Did you cut this out? Do you have all 3? understand? In this way, you can read this agreement and intercept this data.

This is usually the case, why do you want to talk about this? After you start working, if you see this type of agreement, as long as you can understand this type of agreement, you will be able to do everything else. Do you know what it means? What are those words? The field names are different, what what? The lengths are different, do they all mean the same thing? Okay, take a look and help me to see. I’ve filled in some values ​​here for you, so you don’t have to care about them. In fact, is my whole grid exactly the same as this one? Is it still the same? Look again, first of all you have to know what exactly are you doing with this ARP? Let me give you an example, students take a look, you take a look, brothers, let me tell you, this is one of my hosts, right? right? Classmate, this is a host, and I want to send a message to another person, this is one, this is a lot of them. Students, are there many? He wants to do this, he wants to send data to one of the hosts, but he doesn't know that this is a, and the others are all other machines. Then this a wants to send data to this other host, he only knows the IP address of the other host, but he does not know the Mac address of the other host.

Take a look, when I introduced this Ethernet link to you just now, is there a purpose, what? Mac address, well, he doesn't know this, what should he do? At this time, he needs to send arp request to all hosts, do you know what it means? For example, I want to send data to someone of you, but I didn't know your Mac address at the beginning, what's the matter? It is equivalent to group sending first, then after group sending, there is only one person whose IP is the same as the IP in the data you sent, is there only one person? Is there a subnet for this? Do two people have the same ip? That's impossible. Could it be that after you came here, you purposely made the same one for someone, and if he couldn't get on that one, you couldn't get on it either, right? Do you understand what it means? It's impossible for two hosts to have the same ip address, is it? So that is to say, this a sends that kind of broadcast to all hosts. How do you know what a broadcast is? Is it full hair? It's equivalent to group sending, do you know? It is equivalent to group sending, so after all the hosts receive the arp request packet, they will compare whether their own IP is the same as the IP in the arp request packet, if they are the same, they will accept it, and if they are different, they will discard it. what do you mean? Just throw it away.

Then after the host receives the arp request packet, after it receives it, it fills in its own mac address in the response packet and sends it to whom? Send it to a, after a receives this reply, can he know it? The other party's Mac address has already been told to me, so I know it? Is that so clear? It's kind of like what? I don't know the name of one person now, I don't know if I called out, is there only one of us who agreed, and after he agreed, will he know who he is? Is it the same as this? Okay, then let's analyze this process for everyone to see if this is the case.

First look, these are our two hosts, master a and host b, right? Both IP and Mac have it, have it. Ok, then look at this ARP request, first of all, the destination address, what should you fill in? Should the destination address be filled in? Did you know at the beginning? Don't know what to fill in? Fill in f clearly? Fill in all f, this is the case that you do not know, the four bytes are 5, 6, this is 123456, this is the case that you do not know. Okay, do you know the original address Max address? don't you know You must know your address, isn’t it this, well, I just picked it up right away, so we don’t have to go to this one and just get another one, is this the original address? Well, in terms of type, we are very clear, are we going to make an arp request? So we fill in 0806. Is this 0806 just mentioned? Well, is this 0806 clear? These few bytes, students, two bytes, this is two bytes 0806, then let's look at it, the hardware type is 001, don't you care if it's hardcoded? What else? There is also this protocol type 0800, you don’t need to worry about this

These four are good, how long is the hardware address length, then you said that the hardware address length refers to the Mac address? How long is the Mac address? 6 bytes, is it 6 bytes? So how many bytes is the length of this hardware address? Students see if it is a byte, so you can fill in 06 here. Is 06 0X 06? , is it 06 in hexadecimal, right? 06 in hexadecimal is 6 bytes, isn't it? Okay, let's look at the length of this protocol again. Then think about the length of the protocol. What address does 04 refer to? ip address, this is the ip address. Okay, this is 04, so good, there is another op, what does op mean? Take a look at ours, our whole, this, this, this whole, can it represent both arp request and arp response, how does it distinguish? It’s for your op. If op is 1, it means a request. How many bytes does this occupy? Two bytes, so when filling in, fill in 0001, the implication is this 0X001? Is it two bytes? 01 table request, 01 means request.

Okay, let's see who the original Mac address refers to, is it yourself, so copy this directly, should you fill in this? Well, the address of the original Mac. Okay, let's continue to look at the original ip address, do you know? Are you sure you know your IP and don't? Which is better? this, fill in here

? Because in fact, in the process of network communication, your IP address is not filled in like this. What should this string 192.168.10.143 be converted into? Put it here after converting it into high-end byte order, that is, the big-endian mode of network byte order. After the turn, it is actually a 4-byte integer value, is it clear? You don't have to worry about that here, we will say later, it takes 4 bytes to put this. Next, continue to look up, then there is another destination Mac address, do you know? The destination Mac doesn't know what to fill in? I don’t know how to fill in ff, and then he filled in ff, did you fill it in?

, None of these things were done by you, who did them? What did the kernel do for you? Clearly, we are now analyzing for you how it works internally? Do you know the destination ip address later? I know, you won't be able to communicate even if you don't know this, right? 192.168.10.149, ok, fill in this, brothers, ok, now this arp request is there? After the arp request is received, it will broadcast. After the broadcast is over, if the host receiving this message thinks that this is not his own IP, what is he doing? If you discard it, you don’t need it, so there must be an IP when broadcasting, isn’t it this? Then he sees that this is his own IP, so he keeps this packet. After keeping it, he sees that it is an arp request, so he wants to return an arp response. Let's see how this response is returned? Look again at the destination address. So who answered this? Did b return to a? Don't get confused. OK, down. So how do you fill in the destination address? For the sending just now, should I fill in the address that should be filled in this a? Yes, so this should fill in the original address, isn't it this ? Is it a classmate? Don't get confused.

Well, what about the original? Was it originally the mac address of b? Hmm, so it should be this . Let it go, you don't have to worry about it, it's all written to death. It's clear, it's all fixed, right? 0806 Is it all fixed, right? 0001 This is also fixed, this is also fixed, this is all fixed, needless to say. Well, here has changed, the op has changed, if it remains the same, it can be clearly distinguished. Yes, request an answer? Can't tell. Well, it has been changed to 02 here, is it still two bytes? Well, it's 0002. What is it? It is to distinguish whether it is a request or a response.

 Ok, let's look at who the original mac address refers to? Did the original mac itself refer to the brothers of b? Did they refer to this? Well, what about the original ip address? Who is it, is it still referring to the ip address of b, is this it? This, is it this? Looks good, the latter one, where is the destination IP address? Does anyone mean this ? Yes, ok, put it here.

OK, what about the destination ip? Does the destination IP refer to this ? Okay, now I would like to ask everyone that b has sent the arp response packet to a, can a find the mac address of b after receiving it, can he find it? Can it be deduced from this picture? The second is either this or this, understand? So that’s how you get the other party’s mac address through the ip address. There are things in it, and you don’t need to remember these things if you can’t remember them? There is no need to memorize it, why we one, we don't.

 Second, during the interview, it is impossible to say that someone asks you how to fill in the agreement type, because he does not know whether or not to ask you, that is to say, what do you need to know? What did we tell you through this example? The format of this arp request of this Ethernet frame, does this Ethernet frame contain many kinds? Let me tell you that arp is just one of them, but no matter how many there are, it must follow this format. Is it this format? Students, the purpose is the original address type. Brothers, is there another 46-1500 in the back? data? Must follow this format, right? Of course, what if this is less than 46? Is it necessary to make up? Make up, all these operations are not done by you.

 

 What is the process that I just told you about? How does the kernel do this process? Is it like this? If someone gives you a message like this, can you refer to this picture and this format to separate out each part? Can it be taken apart? Very simple, it is mamcopy, is it enough to copy the fields? Is it because you know every long data? Do you know these 6 6, 2, 2, 2, is it enough to intercept by bit? It's that simple? Okay, that’s all I’ll say about this process, okay, I’ll tell you a sentence, I’ll write a sentence for you, you need to know the Ethernet frame format, I’ll tell you it’s the arp protocol, the key protocol you need to know What is the use of the agreement? What are you doing? Classmates, what is this use for? Did you pass something? What is obtained through the other party's ip address? Get the Mac address, that's all you need to do, right? Is there a reverse, right? Take a look for yourself, rart is a reverse address resolution protocol, which obtains the other party's address through the Mac address, what is it? ip address.

 Okay, then let's talk about these other protocols, so we won't tell you about the other protocols in such detail as the ARP protocol, but the routines are the same, do you understand? You just need to pass this arp protocol, hey, how do you know how to analyze it? So do you know all of the following, they are all similar, let's take a look at this ip protocol, ip protocol, what about this ip protocol? It is such a type, look at this handout, 4-digit version, 4-digit header length, these are also stipulated for you, each part has been written for you, do you need to remember? Classmate, you don’t need to memorize this thing, and you can’t remember it, and you can’t remember it, so don’t memorize it. Let’s take a look at it one by one. What does the 4-digit version mean? Check whether you are using ipv 4 or ipv6? Now most of the time we still use ipv 4, so why is there an ipv 6? Why are you guys bringing this up? What if the ip address is not enough in the future? Can this be expanded? Students, it can be expanded to do this, it is for future expansion, but for a long time now, we have been using ipv4, is it clear? There is also a 4-bit header length, we don’t need to care about this, look at the 8-bit survival time ttl, this is what you need to know.

 So what is ttl? Its Chinese interpretation is the meaning of the next jump. If we jump this time, it is not this or that. What do you think? Let me find it for you, and you will know after a ping.

local, look at this classmate, is there a tt l? Is this tt l equal to 64? What does that mean between the lines? Then we are doing, for example, when transmitting network data, if it has passed through more than 64 nodes and has not found the destination address, the packet will be discarded. Do you understand what that means? As long as it exceeds, what does it mean to exceed 64? This is my server a, students, did you go through many nodes in the middle, passed through many nodes, and finally reached b, this b, right? Alright, students, if there are more than 64 nodes in the middle, and this b has not been found, is this b discarded? Why do you want to do this? If you don't, my friends, is there any chance he won't find it? I can't find it, is it constantly transmitting on the network, jumping around? So will it affect the transmission of this network and cause network congestion? Do you know what it means? It will cause network congestion, and you need to know about the main purpose of this. It depends on the 32-bit original ip address at the back, which obviously knows what it means.

What does the 30-bit source ip address mean? How many bytes is a 30-bit source ip address? Classmates, is it 4 bytes? 4 bytes? What do the 4 bytes refer to? Is it just this, what is this? Is the IP value in dotted decimal notation? Of course, in this, it is actually a 4-byte integer value, 4-byte integer value, we will tell you later.

There is also a 30-digit destination IP, which goes without saying. Well, there is also an 8-bit protocol. 8-bit protocol, this is used to distinguish which protocol was used in the last protocol. So let me ask you what is the upper layer of the IP protocol? Which layer is the upper layer of the ip protocol? Students, is the ip at the network layer? What is the upper layer of the network layer? transport layer? Are there many kinds of transport layers? Including this kind of TCP, UDP, ICMP, IGMP, it is used to distinguish this, do you know what it means? You don't understand, let me show you a picture, where is that picture? There is a picture of this, you should have seen this when you have a preview, right? I see, students, where are you? Let me see.

That binary tree, that graph is similar to the bifurcation of a binary tree, this graph, see if this is a four-layer model? Students, where is the application program? On which floor? Is it the application layer? Since the application layer is TCP and UDP, which layer is it? Students, are these in the transport layer? You see, there are a total of transport layer protocols in the transport layer. How many are there? Are there several types? So the following is not the IP layer ah? right? Are these on the same level? What is the bottom layer? Is the network interface layer? Is it the first floor, the second floor, the third floor, the fourth floor? Is it to c? Is it a data frame? This is how the data frame came, and it came like this. You have to look at this picture to understand what it means? So that is to say, what layer is above the IP layer, it has a distinction, right? If, for example, 0 means SMP, 1 means this, 2 means this, 3 means this, can you tell the difference? This is how it came about, you have to be able to understand it, then there is a 16-bit header checksum that checks the data, of course he said that it only checks the IP header, right? Only the IP header is checked, so this is the IP header, right? Then there is data in the latter part, if there is any, if not, then there is no, does this depend on the specific needs? The specific needs are good. Which one is required for you to master? This 8-bit survival time, this must be known, it is enough to know what is going on, what is the 32-bit source IP address, destination address, and 8-bit protocol, right? Well, you see this has a total length of 16 bits.

 So I would like to ask everyone, what does 16 bits refer to? Are the few bytes that the students refer to two bytes? 16 divided by 8 equals two bytes, right? What is the maximum number that two bytes can represent? The 16 squares of 2 minus 1, the 16 squares of 0-2 minus 1, isn't it 65535? right? That's it, isn't it? So in other words, can the longest part of your data be this long? Because what is it? What does the 16-bit total length refer to? The length of the data part refers to the length of the data part. If you know the length of the data part, then there is nothing else. So if you give you this thing, what can you do according to this picture? Just split the data into it.

Is it similar to the arp we talked about earlier? Friends to that is similar. Look at this UDP, then UDP is relatively simple. UDP is relatively simple, so what are UDP? There are 16-bit source port numbers, so the source port number is 16 bits, so what is the maximum range of this port number? Is it just 0-65535? The 16th square of 2 minus 1, isn't that the case? Then you cannot use some of these ports. Before 1024, you may not use those ports. why? Those are the ports reserved by the system, if you don't want to use them, you can't use one if you use it, and your service won't work. What do you use? Have you cleaned it with the back? Use the latter, for example, other than 8000, 9000 can be used, but your service is not so few? You can allocate as many as you like, and you must have enough, right? Don't worry about this, there is still 16-bit destination port, destination port, who is this? Friends, is it the other party's, who do you want to send the data to? Do you know what the port number on the other side means? There is also a 16-bit UDP length. Are the 16-bit UDP and UDP length also the 16 side of 2 minus 1? The maximum length. What about 16-bit sums? Is it like the above data? Has this been tampered with? Have you modified it? or is it? And data, if there is this

 The previous part is a total of 8 bytes, right? , is it a total of 8 bytes? At this time, I am talking about UDP. Later you will see that TCP is more complicated than this. Okay, so which sentence is marked in red here that you must master? Is it possible to determine the only host computer in the network environment through the IP address? Do you know what it means? So are there more than 50 people in our class? More than 50 people? Does everyone have an IP? So does this IP identify its only host? Yes or no? For example, if one of you sends me a message, I will definitely be able to see who you are? Why? I know your IP, did you find this person? Right? In this way, in the second sentence, ports are used to distinguish different applications on the host, so do we have many services on this host? Don't confuse the service with the background process, it means the daemon process, right? So as long as it is a network service, it will provide external services, right? Then he wants to provide services to the outside world, so how the other party accesses him must know his port. So, in our server, ports are used to distinguish different applications. Well, for example, the most common 21 ports, what port? Port 20 is ftp, port 22 is ssh, and port 23 is relatively common. What do you think? How do you look at it, then we have a file that saves the service corresponding to this port, that is to say which port does each service use? This is saved in a file, how do you see it?

  

 Look at the students, is the one I told you right? f tb, let's see who the students are? Is it port 21? Which protocol is he using? Is this the UDP protocol? This is the TCP protocol, right? Look at 22 Who is it? Is it ssh? It has TCP protocol and UDP protocol.

Brothers, what about 23? Is it telnet? Is telnet remote login, students? Do you know what this telnet is for? I can log in to a remote computer with this machine, can I log in to a remote computer? Let’s take an example. Let’s say your boss has gone out. After he goes out, there is information on his computer. He needs to know that he can log in to his remote computer through a remote channel at this time, and then save the computer’s information. You can get some information, you can use telnet to remotely log in

In fact, do you also have remote login in your windows? For example, sometimes a classmate won’t ask you to guide him in one operation. Can you remotely obtain its remote recording permission, so you can operate it? This is for remote recording, what’s next, there are some other things, let’s see if there is anything we understand, see if there is also HTTP? Are you using port 80? Classmates, it’s like these things you need to know about these 21 23 80 These common senses are common sense, do you know many of these things after you see them a lot, and I won’t talk about them later, that is to say, these two sentences What do you need to know? A host can be uniquely identified through the IP address, of course, a host in the network environment, and a service on a host can be identified through the port number. Ok, can one host and one service be determined by adding IP and port? Is it like this? You need to know. This is this, look at this TCP protocol, the TCP data stream format like this. First of all, there is a 16-bit source port number in front. Do you know what this means? It is different from what we said before, is the maximum 65535? Well, the 16-bit destination port number also indicates the other party's port number, and the other party's port number also has a 32-bit serial number and a 32-bit confirmation serial number, which must be mastered.

 

 What's the meaning? What does the 32-bit serial number mean? When we send this data, we must send it out a little bit. Then when sending data, we will give it a number every time we send data, and give it a number. Then what is the number? Woolen cloth? You don't need to worry about this, anyway, the serial numbers are continuous. For example, if you start from 0, then send, send out the number 0, and then 123 will go all the way to the back. This, this, the serial number is good for this, so what is the use of this serial number? After the other party receives your data, he can reassemble the data for you according to the serial number. Do you understand? This is for this purpose. Let me give an example. For example, if we want to send a file to the other party, the file is very large, brothers are very large, then if you use multi-threaded reception, do you know what it means? It is received by multi-thread, so for example, my file is divided into 100 copies, and I only have 5 threads. Does each thread receive about 20 copies? How did you stitch this last file together? Can you give it a serial number? Then there is a process after the final receipt, is it the whole 20 yuan? Is it possible to splice these 20 pieces together? This serial number can be used for this purpose.

Okay, so what's the use of confirming the serial number? So every time you send data to the other party, has he received it? What did he give you back? Answer, there is a reply, and it will tell you in the reply, whether I have received this or not, what will happen if I do not receive it? Will retransmit, do you think this is safe, students? Is this safe? So does udp have this? Did udp tell you to ignore it, what is this similar to? It's a bit similar to this. When you send a letter, when you send a letter, you compare this udp to a regular letter. What do you mean? Is it ignored after sending it back? Is it possible to lose it? Students, will this udp be lost? Yes, there is a possibility that it may be lost.

Then you compare this TCB agreement to another registered mail, or express registered mail, is there a record for each of them? Every contact is recorded? So where did you lose this letter? He knows, so does the ordinary letter know? If you lose your ordinary mail, you lose it, right? You guys, have you ever been to a junkyard? What about the letter in the junkyard? There are a lot of letters in that scrap yard, how did they come here? Is it lost? What about some post offices? For example, when your post office sent letters, some of them forgot to send them.

What do you have? The courier couldn't find anyone when he went to send it. Did he just take the letter back? Well, this confirmation number, you may not be very clear about this part, we will tell you later, then the confirmation number is for the data you sent to the other party just now, and he gives you a response, then the response is here In the 32-bit confirmation serial number, this serial number tells you whether he has received the data or not? To retransmit, make sure that the data you send can be received by the other party, for this purpose.

You don't need to worry about the length of the next 4 digits, what? Don’t worry about keeping 6 digits, there are two more important things you need to know, ACK, you need to know this, there is also this SYN, and there is a FIN. Let me tell you one by one, what does ACK refer to? Confirmation, there is an acknowledgment in the confirmation packet, this ACK everyone pay attention to ACK only means that you have sent the data to the other party, and the ACK is only when the other party returns it to you, is it clear? You didn’t have this ACK when you sent it directly to the other party. This is the first time. I didn’t have it the first time. Is it clear? OK, so what does this SYN mean? SYN is when creating a request, there will be this SYN during the three-way handshake process, let me say hello to you first, we will have this later, and there will be some later, this SYN will be called request identification.

Looks good, what does the FIN in the back refer to? When you wave your hand four times to close the link, there will be such a thing called FIN, which is actually called fihish. The three letters before fihish mean the end of closing, right? end meaning. Then there is another 16-bit window size in the back. When we talk about sliding windows, let me tell you this, what does it mean? Think about it, everyone, you said that a sends data to b, is it possible to cause this situation? You send fast and receive slowly, is it possible? Then if the other party has not finished confiscating it and you send it again, it may cause data loss, so what should he do? He controls the amount of sending through the 16-bit window size, do you know what it means? I ask you to send it, and you send it again, and you can send as much as I ask you to send, so that the data will not be lost, isn’t it? Look at the 16-bit checksum and 16-bit emergency pointer in the latter part. Is this checksum similar to the CS checksum mentioned earlier? Or check whether the previous data is lost, whether it has been tampered with, and so on.

Why? Think about it, everyone, when we actually transmit data, when we send data, the data is transmitted in the network cable or in the light, is it high level? Then this level may be interfered by this external signal, do you know what it means? Restoring interference. Let me give you an example. Sometimes when you make a phone call, or if your computer receives a signal, sometimes the computer will make a sound, which is interfered by the signal. Is it clear? Yes, is there a barking sound? Anyway, when I listen to the earphones, as long as the mobile phone has a signal, it will beep when someone calls, right? how come? Is this signal being interfered with? Do you know what it means? After being disturbed, you said that your signal may have been tampered with? Hmm, will it be changed? Isn't it real? That's what it means. You don't have to worry about the 16-bit urgent pointer. Okay, I need you to understand the numbers listed here for everyone. We all know whether the TCP protocol is stable, safe, and reliable?

 It's a face-to-face link. What is the UDP we just talked about? Unstable, unreliable, unlinked, isn't that the exact opposite of this? What serial number is here to confirm the serial number, and the 16-bit window size needs your attention, and you need to pay attention to it. The serial number is TCP, which is safe and reliable. Each data packet has a serial number, so after the other party receives this serial number, when a lot of data comes over, when it finally stitches together the package, it uses this serial number, otherwise how would he know who came first? Who is next? Right, students, let me give you an example, so the data packet sent out first may arrive later, is it possible? It's possible, but it doesn't matter, it doesn't matter, it's okay.

why? Because you have a serial number, it is still spliced ​​according to this serial number when it is finally assembled. In this case, our final data is still an ordered data, isn't it like this? So what is this confirmation serial number? Use the confirmation serial number to know whether the other party has received it. If I send you the data, you will tell me whether you have received it? So how do you tell me? Will you give me back a confirmation serial number? For example, I sent you No. 1 data, and you returned No. 1, which means you received it, isn't it? There is also a 16-bit window size, what is this? For flow control, avoid sending too fast at the sending end and receiving too slowly at the receiving end, resulting in data loss.

Well, this, um, you just need to know about these agreements. What agreement did we talk about just now? Students, what else is there in the ip protocol? tcp agreement, what else is this? udp protocol,

ip protocol. First of all, you have to know which layer the ip protocol is on? At the network layer, you don't need to remember the format of this protocol, you just need to be able to speak out with reference to that format. do you understand? Or if someone gives you a package, you can analyze it for me, but if you analyze it, do you have to give me the format? Otherwise, how would I know how to parse it? You just need to refer to that protocol to parse it out.

What is the UTP protocol? Link-oriented, yes, secure, reliable data streaming. Do you like the format yourself? Do not remember the format. What is UDP? Face without link, unsafe, how? Unreliable datagram, what does the so-called datagram transmission mean? This thing needs to be transmitted, and it cannot be like TCP after transmission. What is this? Divide into pieces. Well, I understand, this must be like this, and it must be sent at once, why? Why all at once? Because what are you? No serial number, no serial number, no link, right? Then you send it twice, how does he know who is ahead and who is behind? Yes or no? That's all for now.

Okay, let's talk about this next, and give you a brief introduction to the programming of this socket. Let me give you a brief introduction first. It seems that there may be too much to say about the big and small ends in the morning, so let's not talk about it. Fine? Let's not talk about it, let's talk about it in the afternoon. I will mention this one for you. So what about socket programming? What does it essentially belong to? Also belongs to inter-process communication. Why do you say that? Why do you say that? Think about it, if there are at least two processes in this inter-process communication? So my data is sent from server a to server b, is it also communicating between the two processes? Of course, is this place cross-machine, through the network in the middle? Then we told you another sentence earlier, remember? What kernel must the two processes communicate through? Through the kernel is not it? Of course, if we talk about the network, what about the programming of our network? Socket programming, it does go through the kernel, but it has to go through the network, is there one more? Do you use the local machine through the network? Students, the local machine is not used. Are you on the same machine? No, but it is cross-network. After cross-network, it not only uses the kernel, but also uses the network. Is it still the network? Well, since I said this just now, if we want to communicate, the communication between two processes must pass through the kernel, so our soccer programming also has a kernel, which one? take a look? You may not be clear about this now, we must have a file descriptor if we want to communicate, right? There must be a file description, how to create this file description? It is to call a function called the socket function. After the socket is established, the kernel maintains two buffers for you, one is called the sending buffer and the other is called the receiving buffer. You listen to it first, then do you have to communicate with you when the other party says it? Communication is a matter for both parties. The other party also has a file descriptor. This file descriptor also has two buffers, which are also the receiving end and the sending end. Then the sending end is on the left of the sending end, and the receiving end is on the right.

 

 

 Take a look at the file on the left to describe its corresponding buffer. The sender corresponds to the receiving end of the other party. That is to say, the data you send is actually sent to his receiving end. Well, the transmission on the right side corresponds to the receiving end on the left side. This is the case. Let’s take a look. How many links do you think I have in this link? In fact, there are two links, called a socket pair and a link pair, so there are two links. So what are we going to talk about later? Half-link and half-close, that is to say, you can only close one kind, and it can be done. That is to say, for example, is it okay for my server to only receive but not send? Is it possible, or how? For example, if you send it, and you close the link after sending it, is it closed on this side? But you can accept it, there is a function called semi-closed, and we will tell you about such a function, so you can read it later.

So since it is said that there is this file descriptor, let's think about it. When we talked about this file descriptor earlier, what function did you use to operate this file description? which? Let's talk about it a few times, so let's think about where did this file description come from at the beginning? When we first talked about file description, was it file IO? do you remember? File IO, then open a file, it returns a file descriptor, then we use read, write, close to operate this file description, isn’t it these? If you think about the pipeline again, a pipeline, does this pipeline correspond to a cache area of ​​the kernel, then how many file descriptions does this cache have? How many pipes are there? Are there two pipes? One is the reading end and the other is the writing end, which is called a buffer area, corresponding to two file descriptors.

But if you look at our socket file description, it lies in the file description called socket. In the description of this socket file, does one file descriptor correspond to two buffers? Well, one file descriptor corresponds to reading and writing two buffers, isn't that the case? Or both the sending and receiving buffers are fine, then the sending buffer, do you mean writing or reading? Is it writing data? In fact, it is written to the sending end, and the reading is read from the receiving end.

Well, about this piece, why do people call it socket? What does socket mean in English? have no idea. Can you check the socket? Does socket mean socket? So what are the characteristics of the socket? Is there a socket? Brothers, well, then think about it, when we use this socket, you say you need to get this port by yourself? Do you just need to take this public socket back and plug it directly into the power supply? Then plug in the fan, will it turn on and off? It’s the same with us, so you don’t need to do this part of things. The core of these things has been done for you, is it clear? So what do you do when you use it? You just need to adjust the socket to create something? Get a file description score, and then you can directly operate this file descriptor when you do the next operation.

That's the case, so which layer do we actually stand on when we develop our socket API programming? Students, the protocol called transport layer TCP UDB was developed up, but do you care about the lower layer? No, do you care? The lower level is out of your control, what's the matter? Can't understand this classmate? Well, if you don’t understand, don’t talk about it, we will talk about how to use the specific functions in the afternoon, and I will tell you.

 So this means that I will briefly introduce it to you now, that is, if you want to program sockets, we need to know a file descriptor, right? Do we get two buffers when we have this file descriptor? Is the read and write buffer clear? Does the other party of the read and write buffer also have a buffer? There is a file description, isn't it? So in this case, we actually established a socket piece after the connection was established. I will also give you this. When I write the code in the afternoon, I will verify for you whether the socket chip can be verified. Then, including this, what are the api functions we will talk about later? Create a socket Through this function we can get a file descriptor. This is the operation of the file descriptor, I will simply mention it to you, go through it for you, and bind it.

 

 What's the meaning? Think about it, everyone, when we access this web server, do we need to know the port of the other party to access the other party's server? Well, for example, this, I told you just now, are the four fixed? 21 23 80 Are everything fixed? It's the same if you develop a server yourself. Is it clear that you want to bind a port? Can you not bind? classmates? It’s fine, but the other party doesn’t know who your port is or which one you are, and he can’t access you. If you don’t bind, then every time you start this service, this port will be random. Who knows you Which port is it? Today is, for example, 8000, tomorrow will restart 8001, and later 8005, so can this client find you? Can't find it? I can't find it, this bind does this.

Make a binding between the IP port and the file description, and see if the third listen is listening? What do you mean? For our file descriptor, do you want to call the listen function for this file description and set it as passive listening? Think about it, everyone, when we access the client to access the server, you say who is active and who is passive, is it the client that is active, and then the server is passive, and when the server wants to call listen, change the file descriptor from active to passive, it just In passive listening state. Then after adjusting this function, your process is in the listening state, so what can the client do? Can it be connected clearly? You can connect it.

 Well, this is listen, accept later. what? It is to receive a new connection, receive a new connection, this function will return a new file descriptor, this file descriptor is used to communicate with the client. Where are the calls of this function, including the above listen and bind function calls? On the server side, on the client side? Call it on the server side, call it on the server side, what is the connection behind it, who will call this function? You said the client is not it? Did we just say that the client actively connects to the server? This function is actively called by the client, so if you want to connect to the other party's server, what information should you know? You have to know who even? How is this identified? IP port, you have to know, did I say IP plus port, is it possible to determine a service of a server in a network? Is it like this? You have to know this is connect,

So after connecting, what to start next? After communication, it’s nothing more than reading data and sending data, isn’t it like this? Have we seen these two functions? Today we will introduce these two lines to you. In essence, the two functions are similar, except for one more parameter. We generally fill in 0 for this parameter.

 

 Okay, let's talk about this and this model this afternoon. Okay, so I told you some theoretical things in the morning, right? Some basic concepts, such as protocols, 7-layer model, 4-layer model, data communication process and protocol analysis, right? Then at the end, I will give you a brief introduction to this socket api programming, and then I will tell you about this programming-related stuff.

 

 Before talking about socket api, let me talk about a piece of knowledge, which one? The problem of endianness, if this is the case, everyone should have been exposed to it when learning C language, right? Should have been in contact, so there are two byte sequences in total, right? One is big-endian, what is the other? Little-endian, or high-endian or little-endian is also fine. What's the meaning? Take a look at what we say in this concept. Look at the big-endian low-order address to store high-order data, and the high-order address to store low-order data. Can you understand what it means? What does this little endian mean? The low-order address stores low-order data, and the high-order address stores high-order data. Is this small endian normal, right? The normal sequence, we don’t need to say too much about this, because we have been in contact with one, and the other is not too difficult in itself, especially you need to pay attention to it, what is it? When transmitting data on this network, it uses high-end byte order, so this high-end byte order is also called network byte order, is it clear? It is also called network byte order, you have to know this, here I have an example, let's just look at the example and you will know.

If you are asked to write a program to test whether your host is in high-endian or low-endian, can you write it? can it? Everyone pay attention to this? It may come out during the exam. You have to know this, I will open an example for you directly, let’s look at the program, because this program is very simple, Day 10, these two programs are basically the same, just look at one. Looking at this program, what is this dedicated to testing? Big endian or little endian? Take a look at what we use? Is the union used by the common body ah? or a union.

 

 Here I define a short type, and then define this c, right? c Is the length of this array the same as this one? Is this two bytes 6? , um, is this c also two bytes? um, huh? Ok, this is an Int 10, are they the same? That's the same. Next, let me assign a value to it. I assign a value to this s, which is called 0x0102. Can you see what it means? How many bytes is this? Classmates, is it two bytes? Okay, then, let's print the value of c, print the value of c, then let me ask first, first ask this, don't read it first, in fact, you should know what it means after you understand these two.

So you said that if it is in low-end byte order, what should C0 be printed as? low end? This is C0, which is not low-end. Where is the low-end address stored? The stored low-end data is 02, so should it be 01? On the other hand, if it is in high-end byte order, is the storage exactly the opposite of this, right? classmates? Okay, you just need to be clear about this. Then un 2 points s, its value is printed out, does it have this effect with the order of storage? Well, let's see if that's the case? Is this the same as this 0x01020304? Is this just a little more? It is different, this is 4 bytes, um, these four bytes, this 01 02 03 04 high-endian and low-endian, the final display result should be different. After you understand this program, you type it again at night. Why do you say that? During this exam, when I just graduated, I took the exam when I was interviewing the company. I did pass this question. Do you understand? I did pass the exam. He definitely doesn't know about this, he can't think of this level, is it clear? He couldn't think of it, I'll test it for you, and I'll show you the result.

 Don’t worry about warning, just look at it, one is 248, and this type is sizeof(long int), you don’t need to worry about this, right? 248, look at this again, we saved 0102, right? Classmates, let him un2.c[0] un2.c[1] 0 which value is printed? Did you see it? 2 right? is it this one? What about 1? Which one was hit? 1 is it this? , obviously this is a high-endian or low-endian? Is it little endian? Then the low-order address stores low-order data, and the high-order address stores high-order data, which is obviously a low-end byte order.

Then look at this U2 point s again, right? s stands for 258, is this value 258? right? classmates? This is 2, right? How many powers of 2 is this? 2 to the 4th power, 2 to the 8th power, should this be the 2-8th power? 2. What is the power of 8 plus 2? Is it 256? What if you say you don't know how to do it? Turn on the calculator, the calculator can calculate, so when you use this calculator, which one should you use? Programmer, then you see this hexadecimal system, click this to see if it is a hexadecimal system, and then you are not equivalent to 102, right? 102, see if the decimal is 258, this is octal, this is binary, do you know how to use this well, then is the following one right? Is the following one similar to that one? Classmates, 01020304, then 1234 is 4321 respectively, isn't it classmates? the other way around? Is this low end? is not it? What is the final value of this 4321? It is the number 1020304, is this number right? Take it out, is it still in hexadecimal?

see? Students, the actual number is 16909160, no problem, right? , I will also send this program to everyone at that time. One is that if you want to understand it, you must see the principle clearly. Why? Let's think about it here, have you ever thought about this problem, why do I print out this value, is it displayed in this way or in that way? why? Because you are not a common body? Students, the union is these two variables, should they occupy the same memory? If it is, you assigned it at the beginning, and then you use it to display, which can be displayed. But if you use it to assign a value at the beginning, and then use it to copy again, will it overwrite its value? yes, is that clear? Students, this must be clear about this, this must be understood.

This byte sequence questions basic concepts. I told you, to sum up a sentence for you, 4-byte order problem. The first one, what is big-endian byte order, and the second is little-endian byte order, right? Let's see if this little endian is also called something? What is also called low endian? What does little endian order mean? What is it? Low addresses store low data, right? Then the high address stores the high bit data, right? Alright, students, what about the big endian? What is it called? What is also called high-end endian, give you another name, or what? Network byte order, understand? Classmates, it is also called network byte order, right? What is that? What is stored in the high-end address? Store the low-order data, and then store the high-order data in the low-end address. It is enough to understand the meaning. You have to understand this program. This is a problem of byte order.

 So after talking about this byte order, let me tell you about this. We must pay attention to it in our network transmission. The high-end byte order is used in network transmission, also called network byte order. So under what circumstances? Only need this? Big endian or little endian, or what about network byte order and little endian byte order? Be sure to pay attention, if it is a string, do you use the switch? In your data type, please pay attention, as long as the data type does not exceed one byte, does char not exceed one byte? Do not use int? Is short used? Is the structure used? If there is any Int short in your structure, as long as it exceeds one byte, should you use it? Of course, will the structure itself exceed one byte? He will also use it. Under normal circumstances, when we make it, we will put everything in the structure? Data type, for example, if yours is an int type, turn it around, turn it around for a short type, and don’t need to change it for a chart, we will use it immediately later, this is an application occasion, so I won’t talk about it.

Ok, let's see what are the related functions? This related function is actually very simple, as you can tell by its name. Okay, let's see if there is an h in this name, is there an n, is there an l, is there an l? Do the students have l? So how do you remember this? Let me tell you how to remember this interface? inside this? H table means the host, host is not the meaning of the host ah? Classmates, n means network, s means short, and l means long. After you figure out these 4, this function can be seen at a glance.

What does the first htonl mean? h Host to means to which side, where is it? Isn't that a long integer to network byte order? Is this often possible when converting to the long type? long it? Classmates, see if the second htons is reversed or not? It is exactly the same as that, but the data type is not changed to short, is it? Is it not the case that the host self-sequence converts the network byte order short integer? Do you see that its parameters are short? This is of type long, right? , you see, this uint32, this is unsigned 32 bits, right? Are you an unsigned 16-bit integer? right? What about this ntohl network? What is the data converted from the network into? Long integer in host byte order? How is this ntohs transformed into network byte order? host byte order short integer

You only need to tell what the four letters mean, can you see it? to does not mean to where, right? Classmates, where is from from? to is to where? Okay, this, then you should know this first, if there is such a thing, we will use it for you when we write code. Ok, let's talk about the other two functions that are used a lot, the ip address conversion function, look at this function called inet_pton, please note that this p refers to the ip address in the form of a dotted decimal string. To is transformed into what? Transformed into what? Network byte order.

 So how many bytes is this integer? Classmates, is it 4 bytes? Students, please note that the ip address is 4 bytes. what? The port is 2 bytes, is the ip address 4 bytes? This function means to convert the dotted decimal ip address in string form into big endian mode. Is big endian mode network byte order? This kind of IP? Then the plastic is also represented by 4 words. We will verify and verify it for you when writing the code. Is it so good? Look at its parameters, the first parameter, af, what does it mean to use AF_INET when using af? Is it the protocol diagram?

Classmates, the first one is AF_INET, then look at this

In fact, it means ipv 4 and ipv 6, right? Classmates, at present, we all use ipv 4, so we fill in AF_INET for this, understand? Fill in the second one of AF_INET, so what parameter do you think this const char * is? The parameter is passed in, and the parameter is transmitted, and it can be seen by looking at it. Why? Because your home is const and cannot be modified, it can only be passed in. The latter is a guess, the void * must be sent out, is it sure to be sent out, this, that is to say, you finally converted the ip address in the form of a string into the ip address in 4-byte network byte order , I will save it for you which one is not in dst, brothers save it in this.

Okay, I gave an example of these few examples, for example, I use this, this is inet_pton, the first parameter is no problem, the second I want to convert this 127.0.0.1 to what? 4-byte network byte order ip address, how to convert it? We can calculate by hand, and we can also calculate by hand. For example, let me tell you one, this is its usage, how is it used? Is the last 127.0.0.1 saved in this address to serv.sin_addr.s_addr, right? Of course, what type of address is this? Are the students in shape? 4-byte integer value, for example, you can also manually convert it to 192.168.232.145, so how can this conversion be performed? Do you separate the four numbers in dotted decimal first, right? 192.168.232.145? Well, first calculate it separately, then convert it into hexadecimal, then this 192 converted into hexadecimal is C0, is it C0? Let me do some research, this is the decimal time system, right? Is 19 C0? um, huh? Another 168 is A8, right? 232? It doesn't matter whether 232 is e 8 or not, and 145, is 145 91? now is not it? Am I equivalent to switching over one by one? After conversion, you can then store it in network byte order.

Then take a look, under normal circumstances, we think 192.168.232.145, who do you say is at the low end? Who is on the high end? Is the right on the low end and the left on the high end, is that right? So the storage is 145 facing forward. Is this 91 placed at the front? Um, and then is it the other way around? Is this the case for 0x91E8A8C0? This is the end. In fact, is it understandable that this number saves that value? Can this be set up by myself? Students can try it, when the time comes, don’t you know if you adjust this function and try it? Yes or no? Okay, let's look at another function, the one just now is a 4-byte integer value converted from dotted decimal to integer.

 Okay, then the other way around, okay? Can a 4-byte integer value be converted into a dotted decimal ip address? Yes, which function is called? Called inet_ntop is not just the other way around? See, is it the same as before, friends, right, so this is actually a skill when I remember it. What does p mean? It's IP, right? IP is not an ip address in dotted decimal string form, right? Is n a 4-byte big-endian network address? OK, so let's see how it works. The first af is obviously the same as that? Right, is it also AF_INET that we are using ipv 4? Looks like the second src. Obviously this must be a parameter? Is it an input parameter? Input parameters, that is to say, which one do you want to convert into what? This kind of ip address in the form of a string, so what is this? The students really think that this should be filled with a 4-byte integer value, isn't that the case? Then convert to what? Convert to an ip address formed by a dotted decimal string.

So in the end, is the ip address of this place not saved in this dst? Then save it in this dotted decimal system, is it convenient for us to see? Is it convenient for you to see this kind of integer? Is it inconvenient? Then there is a length in the back. Whose length does this length refer to, whose length is his length src or his length dst? Remember this length dst, understand? is his length.

So when you are storing, when you allocate memory for this IP value, how long is the array at least? How much, is it three four times three plus 3 points is 15? Is it enough to give him a score of 16? Do you know what it means? It's useless if you divide too much.

Why do you say this, do you see if 192.168.232.145 is fully occupied, the maximum is 25? Every one, right? So three or four multiplied by 12 plus 3 points is 15, plus 1-0 is enough? So in general, 16 is enough when you make it, and there is no need to make 1024 or 256. Is it wasteful and useless, without that meaning? Ok, this function returns successfully, this is a pointer to dst, do you know what it means? Does this pointer point to him? Do you know what it means? Think about it, everyone, what is the purpose of doing this? This pointer points to this, what is the purpose of doing this? what is the benefit? So when you write code, can you write consecutively? do you understand? It can be connected. What about returning after writing failure? and set errorno,


Let me give you an example here, such as the one above, such as this 010aa8C 0, is this a 4-byte integer value? But should not add 0x in front to indicate hexadecimal? Shouldn't it be converted into dotted decimal first, and let you convert it into a dotted decimal format, so is 01 1? So you will first convert 0a to decimal? Is aa a 10? Then A8 is converted into hexadecimal 168? I’ve calculated this thing for you, I think it’s this, right? 168  
C0 is 192, has this been calculated for you just now? Then after you switch back, do you want to reverse it? why? Because what is this? This is high endian, right? Okay, turn it upside down, how to turn it upside down? Is this value not on the far left? Did 192.168.10.1 work out? What do you understand? That is to say, after you forwarded it in the end, shouldn't this C0 run to the far left, isn't it this? Then this is A8, which is 168,
10, isn't it this? It should be calculated like this, can you understand this? Classmates, can you understand? You must do it, and you must understand what is high-end byte order and what is low-end byte order.

If you don’t understand this, you don’t understand this thing, what does it mean? In this number, only two of this number represent a byte. Is this the case? Why? Because it is hexadecimal, hexadecimal is good, this is this one.

So about the high-endian and low-endian and these two functions, I will introduce you to this first, and it doesn’t matter if you don’t know it now, because we will always use it later, right? Once is not enough twice, twice is not enough three times, I don't believe you, you can't remember if you do it ten times or eight times, can you? It's okay, don't worry.

Now let me tell you about a very important structure, which is also a structure that is often used in our API programming. Which one is it called? Called struct sockaddr, this is the structure that stores the ip address, right? If we want to communicate, do we need to know the ip port of the other party? Is there such a structure dedicated to storing this? Let's take a look at this structure, then the structure we use in this interface is the structure of struck sockaddr, then there is another structure corresponding to this structure, which is the struck sockaddr_in, then I would like to ask everyone about this structure , in this structure, is the address type of the first 16 bits just an integer value of two bytes? Brothers, what is the use of an integer value of two bytes? This is very fixed, just use AF_INET, about this I can open one for you, man 7 socket
, is there a socket? Let's see if there is such a structure below, if not, let's search for another one. man bind

 Does such a function have such a structure? This is called sa_family, sa_family, this usage is very fixed, where do you use it? If there is no such thing, just use AF_INET, let’s look at man 7 ip, see if he has written the usage for you, have you seen it? won't it matter? Can it just be copied directly? See if there is a structure behind it? Students, this structure,

 This is another structure.

 What is the relationship between this structure sockaddr_in and this structure sockaddr? Please note that the struck sockaddr structure was used in the early days, but this structure has a disadvantage. What are the disadvantages? It is often inconvenient for us to assign values. Can you take a look at this convenience? This is, well, it’s all AF_INET, right? But if the 14 bytes below let you assign an IP port, is it convenient for you to assign it? convenient? Classmates, it is not convenient, this is not convenient. Of course, if you say you can master memory alignment, you know what memory alignment is, right? So among the 14 bytes, it is actually divided in this way. There are 2 bytes for storage, and what can be stored with 4 bytes for the port number? ip address, the remaining 8 words are also free, and they will not be used if they are free, so if you can master the memory alignment, it doesn't matter if you assign values ​​like this, but in fact we have a better way to use this structure called struct sockaddr_in, then this structure will give us the port number and IP address for you, and you can assign the value directly.

Then when we use it, we will convert it again. Take a look, man 2 bind is in this function, such as the bind function, such as the bind function,

So is there a structure called cost struct sockaddr in this function? , Brothers, is this a pointer? So in other words, under normal circumstances, if we see this kind of function, should we first define a structure of this type? Then just pass the address over after assigning the value, but take a look at this, is it convenient for you to assign a value to this structure? 14 bytes, do you know what to assign? Do you know how to fill it? It is more troublesome, more troublesome, so obviously it is not very convenient.

 

 For this reason, it was improved later. Attention everyone, after another improvement, another structure like this was proposed. This structure is exactly the same size as this structure. Do you know what it means? Well, the size is exactly the same, so this will tell you what port and what ip address to fill, and fill it out for you. When you fill it, you only need to assign values ​​to the structure, right? Is it enough to assign a value to this member of this structure? Just assign the port and ip.

So let's take a look at this structure. There is such a structure called struct sockaddr_in. There are three members in this structure, right? Good looking guys. The first one is called sa_family_t, right? Then it is called sa_family, did this address family: AF_INET tell you, right? Are we using ipv 4, you can use it directly. Look at the second what does sin_port mean?

pot in network best order。

Did I tell you? Is the port number in network byte order? You said you need to transfer this port? Students, let me give you an example. If your port number is 9898, do you need to convert it into network byte order? Yes, have I told you, so under what circumstances do you not need to transfer? Its data type length is 1, no need to transfer, as long as it is higher than 1, it needs to be transferred, so is it different for sort to be higher than 1? In fact, this port is of sort type, is long also higher than 1? right. then char

For example, for example, does my char a100 need to be converted? Please note that it is unnecessary to mention the smallest data type. Is the smallest data type char c? 
 

char is one, it is actually of char type, don’t get it wrong, char does not need to be converted, that is to say, you don’t need to convert a string, but what you want to send, what short type is in it, int Type you just need to turn. Remember this, remember this, it will be reflected when we write code. Then there is another struct in_address sin_addr is Internet address, is it ip address? So is this also a structure? see? Well, well, this structure is defined for you here, and he made it weird, and he also made a structure for him here for one member.

 If I design it, if I design it, I can put this here directly, but there is an advantage to this design, what is the advantage? In terms of expansion, get it? For example, if you have more items below, can you just fill in sockaddr_in here? Does the original thing need not be modified? ,right? Remember this. Okay, then how to set it up, how to use it, how to design it, you can use it however you want, you don’t need to worry about this, so how do you use it when you use it? Classmate, is it the first angle body? For example, is this structure called addr.sin_addr.s_addr? Okay, this structure, then I will tell you about this structure, and let's talk about the latter two later.

 What is this used for local communication with the socket? Structure, don't worry about it for now. Well, this is a struct, it's all there, right? Both. Then I will introduce the API function of this socket to you. First introduce the function, then after introducing the function, let's introduce the development process of the server to you, and then introduce the development process of the client. Then let's write some code and test it. Well, what is one homework tonight? You write a server client, a server server, a client, and then communicate. Okay, you decide the communication protocol between the server and the client yourself. That’s ok, brothers, you decide it yourself. Whether you send a structure, you send a string, and then add a package diagram. That's okay, wait a minute and decide whatever you want, remember? Let's take a look at this and introduce the relevant socket functions to you.

 

 So the first one is this socket, did we say it once in the morning, is this function equivalent to creating a socket? What do you get when you create a socket? Get a file description, right? For this file descriptor, does the kernel correspond to two buffer areas? Well, which two buffers, send buffer and receive buffer? Okay, let's take a look at this structure and this function. Then the first parameter is called domain, which means the protocol version. Obviously we all know whether we often use it now, one is ipv 4? So here you can fill in AF_INET. This way of writing is very fixed, just write it like this.

 OK, what does the second type mean? It is a protocol type, so there are two commonly used ones, one is called soft stream, sock stream, and the other is called sock dgram. Look at this sock stream is streaming, so the protocol used is the TCP protocol. Is it because when he establishes a link, it is a link-oriented safe and reliable byte stream transmission? yes, huh? Then there is another one is that the sock dgram is in the newspaper format, is it the datagram protocol? The protocol used is the UDP protocol, which is unsafe, unreliable, and linkless datagram transmission. This is the case, then there is another one called protocol, and the third one is protocol, which is usually enough for us to pass 0, indicating that the type used is the default protocol. In fact, there are not only two types of protocols, but many others, but we rarely use them. Well, this function succeeds, obviously, is it returning a file description greater than 0? um, huh? fail. Return -1 and set errno

 Then think about it, can we also print out this error for you by using the perror function? Have we used this many times before? It has been used many times, should we not print this in the thread? The value of errno ah? Yes or no? We have said this. Well, when you call the socket function, did we get a file descriptor? The two buffers corresponding to this file descriptor are built for you in the kernel, did you build them? Let me ask you again, can you directly operate the kernel? Can't, can't you? Then in the next series of operations, you are all operating on the file description. If it is a file descriptor, we talked about the file descriptor earlier. I told you, what are the functions that the file description can operate on? read, write, close also apply here.

 Well, this function is finished, so let me briefly mention the red mark. Let me just mention it to you, that is to say, after you created the socket, we got a file description, right? In addition to the corresponding read-write buffer, what else is there in this file description? There are two queues. Why is this queue divided into request link queue and linked queue? Under what circumstances do these two queues usually exist? Please pay attention, is your file description point monitoring file descriptor communication? Communication doesn't use it.

The acresies function we will talk about later, let me tell you, what does it mean to have two queues? That is to say, I will give you an example. Of course, it is not appropriate to give it now, so I will talk about it later, and I will talk about it later. The second function bind, we all know whether this server provides external services, it needs to provide external ports, is it a fixed port? Classmates, please pay attention, each service has a fixed port when it provides services to the outside world, so this group port is generally not allowed to move casually, once it is moved, it means that the client will not be able to connect to it? If you want to move, you have to explain to the outside world, what do you see? Well, if the first function socketfd still displays this, should it be the file descriptor returned by the socket we created earlier? right? Just let it go.

The second is struct sockaddr *addr Is this a pointer, students? Well, this, everyone, pay attention to what is stored in this? IP port after these information. Addrlen is the length of this addr, this length is good, so this structure is not what we just said, does this structure have 3 important members? One is called family, is it possible to use AF_INET this day? Then, if there is a port, I have told you that the port is usually 16 bits, and the length is only two bytes, right? What do you do if the length of two bytes exceeds 1? Do you want to convert? Should the host self-deorder and switch back to the network to go to the short integer? Yes or no? So which function is used for short plastic? Can htons understand? So after the conversion, did your 8888 convert the high-end byte order?

But this kind of words don't change, right? Does 8888 not transform? ,Yes or no? Then you're in perfect symmetry, aren't you? You want something else, right? Regardless of whether the situation is 8888 or 9999, you should transfer or transfer, okay? Well, the latter one, is this IP INADDR_ANY? Students, IP is here to tell you a macro, this macro is called INADDR_ANY.

What does this mean? Indicates that any available IP of this machine is used. Let me ask everyone, is there more than one IP address for these many servers? , you may not have contact with the server, you can plug in a lot of network cards on the server, some have at least 2, some have 3, 4 do you know what it means? He has 4 network cards, so for example, when you provide services to the outside world, all of your 4 network cards can access data, can they all send data? When you use this INADDR_ANY, it means using any available IP.

Understood, of course you don't tell others when you provide services to the outside world? You can use any of these four, right? He, if you have a fixed IP, you can write the fixed IP here. 127.0.0.1 refers to the machine, right? It refers to the meaning of this machine, so if it is not this issue, you write it as it is. Of course, is your address definitely the address of this machine? What does this 127.0.0.1 mean? This local loopback address is often used for testing and providing external services. Is it okay to use this? No, it’s not good to provide external services. Have you tested your own host clearly? This is called the local loopback address.

Ok, so when we use it, do we use inet_pton? Brothers, what does inet_pton mean? Is the point divided into decimal? What is this address translation? High-end mode that translates to network byte order, right? Where is the 4-byte integer stored? Is it &serv.sin_addr.s_addr inside this? Is the ip address of our host this? Is there a small structure under this structure? Are there any members here? Called s_addr.

You can’t remember now, can you clear your notes or check documents? This returns 0 on success, -1 on failure, and sets the third parameter of errno, listen to the bind just now, what does it do? Classmates, bind binds the IP port and the file description. Is it because you have been operating this file descriptor during subsequent operations? Do you know what it means? The operation is the file description, read, write, close, etc., which are all related to the file description.

Well, look, the latter listen is to change the socket from active to passive. Did we talk about this this morning? Under normal circumstances, if you are a client, should you actively connect to the server? Does the server know when you connect him? I don't know, he is passive and you are active. Is it like this? Just like we surf the Internet, isn't it the same? As soon as I hit Baidu, I connected to the car. Well, this is the change of listen from active to passive.

 So what is this fd obviously? Which fd is this? fd? Yes, it is to create the file descriptor returned by the socket, right? There is also a backlog parameter, so what did the handout or the helper say? The maximum number of simultaneous connections. In fact, related tests have been done on this parameter. It is said that this parameter is basically useless and useless, but when you fill it, you can’t fill in 0. Do you know what it means? This is the maximum is 128, the maximum is 128, you fill in a number greater than 0, what does this mean? Let me give you an example, for example, the original intention is this, the backlog is like this, if we say that our server is the biggest, that is, if you fill in 10 for this parameter, the reason is this, 10 connections can be made at one time Client, what should I do after I come again? Need to wait, know what that means? That is, these 10 have already entered the connection queue. Once the connection queue is full, if there is another connection coming next, where can it be placed? Waiting in the connection queue.

So where to put the link successfully? Put it in the linked queue? Did I just tell you that there are two queues? Where is it? I see that students, one is the request link queue, is this equivalent to not establishing a link? Is this link already established clearly? You can understand this, because this parameter is not very meaningful anymore, it doesn’t make much sense, but when you fill it, don’t you fill in 0 to make it clear? You can fill in 5 10 128, you can.

Let's look at accept later, so what does this accept mean? Is to get a link, so where do you get the link from? Everyone should pay attention to get a link from the link queue and take out a link. The implication is that the link may have been established before calling accept. Can you say that? Let's try this. Is the link I didn't call already established? We can write code to test this. OK, so you think we'll be fine with that? Let’s just say that after I call accept, I can get a link, then I can communicate after I get the link, and that’s it.

 Everyone on the server side should pay attention to the fact that there are two types of file descriptors on the server side. What I just mentioned is the second type. Which is the first? Have we already called shocket before? Has a file descriptor been returned? Is it that file descriptor? Is it true that we have tuned listen and set it to a passive listening state? That is called the monitoring file descriptor. Remember, there are two types of file descriptions on the server side, one is the monitoring file description, and the other is the communication file description. When we call accept and return a file description, please note that this file description is used for communication.

What is communication? Is it sending and receiving data? What does the file descriptor refer to? Only those who receive the link, if there is a client to connect, he will receive the link, then the last link, and finally we get a new link, which is to call assert, and the returned value is used to communicate with the client. Ah, listen to it first. We told you well when we wrote the code. The first one is obviously the file description returned by calling the socket. The second parameter, everyone, have you written const here? Did you write const or not? Everyone pay attention to what this one is? The outgoing parameter, the outgoing parameter is not written, and the const is not written, what does this mean? How does my server know who is connected to me? How do you know? It is stored in this structure, who connected it, right? Are there two in this structure? Friends, one is the port of IP? , whoever connects will be recorded in this structure, is it clear? Look at the third parameter, which is a particularly important parameter. Many people are on it, and this is a stepping stone.

This parameter is an incoming parameter, what does incoming refer to? Passing in refers to telling the accept function how much memory this structure occupies and passing it out. After this function is adjusted, is this piece of memory filled? How much is filled? He told you again through this return value, so this parameter is an incoming and outgoing parameter. Remember, this is what you need to pay attention to. This function successfully returns a new file descriptor for communicating with the client. If it fails, it returns -1 and sets errno. Do you understand? This accept is a blocking function, what does that mean? Then if there is no client connection coming, this accept will be blocked until the client contacts him, and he will return immediately, and then return a file description, which is used for communication.

But in fact, when the function kernel calls this function, it takes the link in the request queue to the link queue, and the link queue. Is the relative link established? Hmm, isn't this already built? Well, look at this function called connect, who uses the connect function? I would like to ask everyone, is the client first to contact the server or the server mainly contacts the client, and the client contacts the server. This function is called in the client field. Is it clear? How did the first parameter fd come from? Everyone pay attention to how this fd came from? The file description is to call the socket, nothing else, pay attention.

 There is another type that is returned by calling assert, but as far as the client is concerned, it only has one type of file descriptor, so what? It is used for communication, because it does not monitor, do you know what it means? The client does not listen, this file descriptor is a file descriptor returned by your call operation. Well, the second parameter, guess what it means? Are you connected to each other? Are you connected to the server? So when you connect to the server, do you have to know that the iP port is placed in this, do you see if it has a const? What's the meaning? This is a transmission parameter, look at the third parameter, the third is an addrlen, does the length of this refer to its length? right? You generally say how do we fill in this length? is not it? sizeof(addr) will do it, right? sizeof(addr) he can.

Ok, so after the introduction of these functions, is it the communication? Then on your client, you have established a connection with the server by calling connect, and then you can send data after exiting the client? Send data, receive data, which function to use? Just use read and write. Are you clear, just these two. Did everyone say these two words before? I won't be long-winded here.

 

 Well, what you haven’t seen are these two, one is called recv and the other is called send. Their parameters are basically the same, that is, the last one has an extra parameter. We usually pass 0 for this value. Flags =0. Then when you use it, you can use any of these 4. But I suggest that when you use it, you don't use it in combination, and you don't use read. Then what kind of send do you use, don't use write, you want to use recv, do you know what it means? These two are a pair, are they read and write? Are these two also a pair of recv send? But will crossover work? It's okay, I think the readability is poor, do you know what it means? When you use it, you don't use it in cross-use, so don't use it in cross-use.

Okay, so basically we have introduced this function. Next, I will tell you about the communication process. After this process is finished, we will write one for you and describe it in words. Finally, we will write a code for you. Just test it out. You should first listen to these functions in general. Some students may have listened to them, but the teacher, I didn’t understand them at all, did I? Don't worry, don't worry, it will make you clear in a while, and it will gradually clear you up.

Alright, here we are, we must at least draw this blueprint to the end. Where did you hear this sentence? Haha, it seems that everyone doesn't pay much attention to the big blueprint of this city, some of which are written on this label, and a blueprint is drawn to the end, right? Roll up your sleeves and work hard, is there anything else? right? Has Xinmin said many famous sayings that you can only know whether the shoes are suitable or not, only when you put them on? Okay, let's introduce the function to you first, and then let's give him this flow. Okay, then I will walk you through this process, the whole process for you.

Seeing the picture, students look at this picture, as long as you understand this picture, I think it should not be difficult to write a program. First look at the client, first look at the first step of the client, first of all, do we want to call the socket to create something? Do you get a file descriptor first? Well, to create a socket, after the socket is created, are there two more buffers in the kernel? Well, which two buffer zones? send buffer and receive buffer. Then we operate these two buffers, can we just operate the file descriptor? Is it possible to read and write? OK, so what do you need to know? The buffer that read and write write here is not a read. Is it a read buffer? Well, does write write the write buffer? okay.

 Did the second function connect get a file descriptor after creating the socket? Who is the second step to connect to? Is it connected to the server? Bros? At that time, there was a 3-way handshake process in the connection process. You don’t need to worry about it. Don’t worry about it now. This is a 3-way handshake to establish a connection. Okay, then this can be connected. After the connection is successful, the client and the server will establish a connection. Once the link is established, what's the next step? Are you able to send and receive data? send recv, brothers, send or you can call write and read, write read, this is writing and reading, or send recv, is it clear? OK, send recv.

So in fact, please pay attention, should the send and recv be a loop? this one? Hey, I understand. Of course, you may say that you post once and end immediately. Is it also possible? Right, students, can this piece be recycled? I accept it, I sent one, then the server will reply after receiving it, and I will send him another one, is it sent back and forth, do you know what it means? Well, you don't need it in the end, remember to call close to close it.

Is this the same as when we talked about opening a file earlier? Well, open opens a file and then don't use it, isn't that what close does? This is client-side writing, isn't it relatively simple? It's relatively simple. Let's take a look at the server. The first step of the server is to call socket to create a socket. So what value does this function return? Is it described in the file? right? Students, can I also get two buffers by returning the file description? Get two buffers, of course, this step, it is reasonable to say that there will be two queues in this step, why? Because your file description is not good for monitoring.

 The second step is to call the bind function to bind, so who is bound to whom? Students, bind the IP port with the file description, because have you been operating the file description in the follow-up? You didn't explicitly manipulate the IP address, so it's good to find out. The third step, what does listen do? After monitoring and calling this function, your process and your program are in the monitoring state? We can use the command to see this. So does this list change our file description from active to passive? Is it because the server must be passive, right? Yes, no good. Next, continue. After this, call the accept function. Accept is a blocking function, right? If no client connection arrives, the accept will be blocked all the time. Please note that it will always be blocked, but the connection may have been established before the accept.

 Let me give you an example. For example, before accept, you add a sleep 100, and then your client directly connects to it. In fact, the link before sleep 100 already exists, but you haven’t called accept yet. Take this link out. Is it like this? Well, that accept comes up with a new link, so what is this new link for? What are you doing? It is used to communicate with the client, everyone should pay attention to the one used to communicate with the client.

So after this accept, have we got two types of file descriptors? Is a class returned by this function? Well, yes, what kind of file descriptor is this called? This is called monitoring file descriptor, to understand, concept, to understand, this is called monitoring file description socket (), this communication is called communication file description accept (). Then this monitoring file describes its function, that is, to monitor whether the client has a link coming, and if there is a link coming, can you call accept to receive a new link? Then the function of this accept function is to come up with a new link, this new link is used to communicate with the client, is it clear? Okay, then after getting a file descriptor for communication, can you send and receive data after receiving it? The follow-up is different from here. In the end, there is no need to remember to close. Then the whole process is such a process. It is such a process. Well, friends, this is such a process.

Then let me tell you about this process and write it down for you. Okay, let’s take a look. Let’s start with the server. What is the first step in the server development process?

The first step, brothers, um, is it to create a socket? Is not calling the socket () function? Is it also a function? So what question do I get? returns a file descriptor, right? What is this literary descriptor for? Let me write to you, what does this document describe the service for? Used to listen for client connections. Folks, this one knows, this is one of a kind. Folks, this is a class of file descriptors.

Second, what are you doing? What are you doing? So you have now got a file descriptor, and the next step is to bind it. Well, it is binding, who is bound to whom. will lfd. and what? For example, our file description is called lfd, is that okay? Well, there is nothing worth mentioning about binding lfd, that is, binding the listening file descriptor to whoever binds the IP port, and calling the bind function.

The third step, what to do? What will it be? What did lfd change from? From active to passive to passive, in fact, it is passive monitoring, which function to call? listen

The fourth step, the fourth step, is it the next thing I am now, is my current program already in the monitoring state? Then is it necessary to monitor whether there is a client connection coming? If a client connection arrives, which function will be called to take this connection out? Does accept accept a new link? where? Is it acceptable? What value is returned? get.

A communication file descriptor. I tell you that? Got a file descriptor, right? OK, so what is this file descriptor for? This file descriptor is used to communicate with the client. Am I telling you two things? Students, one is the listening file descriptor, and the other is the communication file descriptor.

What is step 5 doing? Classmates, is it you next in the fifth step? Of course, you can be inside the loop, or not inside the loop. What must it be? What sent and received data? Sending and receiving data, under normal circumstances, our server first receives and then sends, so I ask everyone, can you send and then receive? It’s okay, for example, if your client link is established, you can send it directly to him, but the premise is that the link must be established first, is it clear?

But under normal circumstances, we are the client who sends the server first, will the server get back after receiving it? What are you receiving? This side is generally in a while loop, is it okay not to be in a while loop? You just post one and end it, that’s fine, isn’t it? This depends on the demand, we are in a loop first. OK, what's next? Is it read data? Receive data, read or recv receive, and then send data? So I would like to ask everyone, if the client is only connected to the server, but he can't send data, what will happen to the server? Will it block? Attention everyone, did we tell you about this pipeline when we talked about it earlier? Is the pipeline blocked? Are the read and write ends of the pipeline blocked? If there is no data, there is no data to read, does it keep blocking? We are the same.

Do you remember the socket file description points? Let's talk about this file, what is this read function? I told you when it was blocking and non-blocking. What is reading ordinary files? Non-blocking read pipeline, what are these sockets? Are there any terminal devices that are blocked, is it clear? Ok, sending data is send? Yes, of course it is write, write and what? Or is send not so good? After this is done, remember to close the file description at the end, close a few, students say, one, is this lfd to be closed, then this one new file descriptor, for example, what is this called? Does the cfd have to be turned off? what? Is cfd two? Is it two? Just close it, don’t worry about it, just close it, it’s not that complicated, just call close to close these two file descriptors, right? So this is the development process of the server, can you understand? Classmates, after understanding this, we will write a code to verify whether this is the case.

Ok, let's write one, this is server.c, what does this mean? How is this a server program? Ok, so first add some header files, which ones to add? stdio.h, stdlib.h, string.h types.h, maybe we don’t need it here, but is it okay if you add more? Will it report an error if it is missing? Students, which one else? Attention everyone, there are two more, I will write them for you here, netinet.h

What do you think of this? This can be checked in this way. How to check? man 7 ip These header files, one is called this header file, you must have it, you must have it, maybe I remember it wrong, it is not called this name, here is one, what is the other one called? There is another called this, two are enough, I remember it is arpa/inet.h. Is it this one? There are two of these, these two are enough, if not enough, you have to fill in, which one?

 man 2 socket

This does not matter socket.h. Why? This header file may include netinet/in.h in it. Do you know what it means? Don't just write it in and read it. If it says an error is reported, it doesn't matter if you add this. Ok, let's take a look at Int main. Okay, what do you want to do first? Students, how to create a socket? Does it return a file descriptor? What is it that we call alfd? What is the first parameter of socket? Can't remember to turn over. Oh, it's the first one, this one, domain? Are there many kinds of domain values? There are many kinds, do we generally use this one? This one is called AF_INET. 

What is the second parameter? Is the second type ah? For type, we often use these two, so what is this socket_stream? Are the students using the TCP protocol? This SOCK_DGRAM uses the UDP protocol, right? Here we are, since it is link-oriented, should there be a connect process? It must be a link, so what do we use? socket_stream? Just pass 0 for the third parameter, understand? Is it possible to fail? It is possible, if this is less than 0, where are we going?

 Is it not possible? good,

What is the second step? Is it bound? Brothers, bind. How to write it? Does the binding bind this IP port? The IP port is bound to the file description, so before that, I will give you this function first. Well, in this way, I will also pick up this function for you, and I will put it here for you. I only put this kind of thing once, and I won't put it in later, right? I will add this comment for you,

The second word is not bound, we also add this comment back, man 2 bind. it is this,

 In this case, it is convenient for us to write according to the writing, right? Binding is good, is this bind? Well, is it okay to have a return value? This return value, can you take a look at what is returned by this return value? Does it mean success and failure? Does it return 0 for success and -1 for failure? And set errno. Then you can add a judgment to it.

The first parameter is which socket fd is it? Is it just this lfd? For the second one, do you need the address of a structure of this type to be placed here? So should you define a variable first, and after defining the variable, do you want to assign a value next? For example, called serv, can it? So after the definition, is it going to be initialized next? Here I will introduce a function that you have never used before, called bzero, have you used it? It has only one parameter, I think there is only one? I see man bzero This is the first address of that piece of memory, right? Is this the length? How should this length be filled? Is it sizeof(serv)? Is it okay to fill in this struct sockaddr? They are the same value, right? Okay, let's start assigning value, who will be assigned first? serv., Can't remember how to turn it? , man 7 IP, I remember there is one, is there one here? Is there, is it this? Brothers, please pay attention to when you define, can you define sockaddr in this way? Do you define it like this? Have you said that you should not use sockaddr? Is it more convenient for us to assign values ​​by using sockaddr_in? Looks good, is the first one this sa_family_t 

 

 Does the second one have a port? Classmates, for the port, we need to specify a port here, for example, 4 8 and 4 9 are fine, you can choose this as you like, but you don’t want to use the ports before 1024, is it clear? Those are probably already reserved by the system, aren't they? OK, how does this work? Is it necessary to convert between this high-end byte order and low-end byte order? What should we put? Transform what into what? Is it necessary to convert the byte order of the local host into a short integer of the network byte order, called htons (8888), and you can fill it in directly, for example, can four 8s be used? Well, there is also a serv.sin_addr.s_addr=, what is this equal to?

Let's just use that macro to use any available IP of this machine, of course I only have one. So if this is the case, does it need to be transferred? Also need to turn. So how to turn? Classmates, let's think about how many bytes are in the IP address of our network? There are 4, is it the last one that has been transformed into a long plastic surgery? How do you write this? htonl(INADDR_ANY) right? 

I will add a note to you, indicating that the machine is used. what? Any available IP, let’s use this one first, and we’ll give it to you later, after testing this, I’ll use that one for you. Is it possible to convert this ip address in the form of a string into something? The integer value of the network byte order four sections? I'll use that for you then. Ok, is this assignment done? Classmates, is it just 3, 1, 2, 3, and these 3 have been assigned, so should I start writing here next? How do you write this? serv is not to do a mandatory type conversion? struct sockaddr*? Then you see that this parameter is not a struct sockaddr of this type? You need to perform a cast on it 

Well, there is another parameter behind, what does this parameter mean? This parameter refers to the memory size of this struct sockaddr *addr, how to write it? Is sizeof(serv) used in this way? It's finally done, okay, let's judge that ret is less than 0, perror(bind error

return -1 right? OK, this is over. Ok, this binding is done.

What is the next third step? Is it monitoring? Monitor, which number to call? Is it listen? Shall we finish this after class? Classmates, don't interrupt our program. Okay, can we let everyone rest for a while after class? Okay man 2 listen isn't it? I can write the prototype of this function for you, and of course you will check it yourself when the time comes. OK, take a look at listen.

What is the first one? Is it lfd? Shall I give him a line 128 for the second parameter? The maximum value is 128. Ok, what should I do next after listening? Is it necessary to accept a client connection? Which function to call? man 2 accept OK? accept OK, what is the return value of this accept? Students, what is the return value for? Is it for communication?

int  cfd = accept();

What is the first parameter? Is it lfd? Is the second parameter the client's IP port here? You don't care what to pass, so is the third useless? You don’t care about passing NULL, let’s make a simple one and add this for you later.

Okay, okay, let's print out this CF, what do you think this CF should be? classmates? lfd, what should it be, what is it? I forgot that you just passed the exam. Students, have the first three file descriptors been allocated? So when should the next redistribution start? 3 Because you didn't open the file earlier, did you? Ok, this is accept. After accept, have we got a file description for communication? What's next? Yes or no? Then enter the while loop to read and write data? Classmates, let's talk about this again right away, and I will definitely give you a longer time after class. All right, I will give you back the time it took.

 There is no way to talk about this, we can’t stop in the middle of this, okay? good. Then the next step is to start receiving data, which function to call when receiving data? Reading data is actually reading whose data, my friends, is it sent by the client? Ok, read Here we need to set a buf, int n is equal to 0, char buf[1024], and then initialize it here, memset(buf, 0x00, sizeof(buf)), are you starting to read? n= read What is the first parameter to pass, you should remember cfd, don’t forget it, don’t write it as lfd.

The second parameter, the third parameter, is this different from the file? Print this sentence, is the read function a blocking function? Here, if there is no number, if the client does not send data, this function will always be blocked. Are you clear? Then you have read the data, you have to remember to send it back to the other party, do you want to reply one, we are like this, can we make a big news conversion? Understand, what is the big message conversion for? What is needed here? The cycle factor int i is required to be equal to 0 and equal to 0, buf[i]= toupper(buf[i])

The declaration of this function is here, man toupper, right? Is this function this? What do you mean by looking at this? to buffer to what what is not to what ah? Do I need to add a header file to convert to uppercase or lowercase? It is ctype.h, this needs to be added to ctype.h, if this is not added, an error will be reported. Is it one? Okay, so this is equivalent to a loop here, what are you doing? Is it sending data? Does sending data mean writing data? Call write, the first one, right? cfd, the second buf, and the third n, do you know this scene? Ok, this is finished, return 0 after writing, so did you remember to close the file descriptor before return 0? Close the listening file descriptor. and what? And communicate file descriptors right?

  Ok, it's over, let's compile it and see, it's all done in one step, right? Let's test the test first, and we will test it in the next section. Let's talk about this first.

 Okay, then we finished writing this idiom last class, didn't we? Next, let's test the program we wrote, right? See if there is any loophole, is not it? It's normal to have loopholes, we can just change it, isn't it? Let's see how to measure this? Let me first teach you a small test tool called NC. Now let’s start it for you./server. Are you starting this now? Okay, let me ask you the first question, is this program blocked? Where is it blocked now? where? Is listen a blocking function? I didn't say that, did I? Who is blocked? Is accept here? Students, he is blocked at position 42, is it clear? It's blocked here.

Next, let's use a client tool to test our code is wrong, how to test it? There is a tool called this tool, I call this nc, nc, this is a small test tool, how to test the small test tool? 127.0.0.1, then this can be abbreviated as 127. 1, can it just be written like this? Classmates, do you know which port just by writing like this? I don’t know, I will write a port later. Did we set the port ourselves? Enter the car to see, is there an immediate return?

Is it lfd=3 cfd=4? Is this used to monitor file descriptors? This is the file descriptor for communication, right? Then let's send the data to have a look, right? Hello, has he received it? Students, did not receive it, there are 5, why are 4 displayed here? Is there still a carriage return? This is no problem, did he also send it after the conversion is completed? Really, are you back too? no problem.

 So no matter how you post it, it’s all right, isn’t it all right? Then let's take a look at one and see what happens if I turn off this client? How to close? I just ctrl c is it over? Then it goes into an endless loop, doesn't it? Students, do we need to adjust this program a little here? Take a look at our code, take a look at our code, if you pay attention, if read returns, if the other party closes the link, then the read function is here, we can see how much it returns through this observation, If the other party closes the connection, we should return 0 here, which means that the other party has closed the connection. In this case, does our while (1) loop need to exit? Yes or no? So it should be adjusted here, this is too fast, right? Going too fast can easily freeze your program.

 Judging if n is less than or equal to 0, what does it mean to be equal to 0? Students, did you see that the other party closed the link just now? If it is less than 0, it fails to read. If one fails to read, what is the other? The other party closes the link. In this case, we all need to jump out of the while loop, isn't it? read error or what? Can client close exit? The other party closes the link, client close, you can close these with client close, and then you can type out this value, how much is it? Yes or no? n, then this n should be client close. Is the client closed? Of course, does the client exit mean that the connection is closed? Then, huh? Classmates, break, this is more reasonable, this is more reasonable. Test again, 

 

 No problem, let's end it for him and see if he ends it too, right? read error or client close Correct, does the other party close the link? The other party closes the link, is it necessary for him to block there? Or is it an endless loop? It's not necessary, he can also quit. Then this is the example we learned from you. In this example, the server code should not be too long, and what we wrote is also written according to our process. You can type this process at night. Check it out. Okay, so what shall we say next? Let's talk about the development process of this client.

OK, what do you want to do first? Is the first step to create a socket? So what can you get? What do the students return? A file descriptor, so what is this file description for? Is it for communication? Let me tell you, who is this file descriptor used to communicate with? Server communication.

The second step, with the socket, what do we need to do next? Do you want to connect with someone? It is for everyone to connect to the server.

In the second step, can my client be bound? Can he bind? . This is not to say that he cannot be bound, he can be bound, but his binding is equivalent to unnecessary actions. Why do you say it is unnecessary? Your binding is equivalent to fixing the port of this client. What happens if you don’t bind it? Not binding is actually the method of not binding that we often use. If you don't bind the kernel, you will be randomly assigned a port. If you bind it, it is equivalent to it. Did you decide it to death yourself? What if you set the port and it is occupied? know what it means? So you can leave it to the kernel to handle this kind of thing. The kernel will randomly assign a port for you, so we generally don’t bind it, but the server must be bound. If you don’t bind it, you let the kernel randomly assign it. Even you yourself don't know if this is the case.

How do you provide services to the outside world, understand? Okay, okay, let's see what the second step is? Are you connecting directly to the server? Which function was called? Which function to call when connecting to the server? connect, of course, which one should we adjust in the first step? Is it this socket? Call the connect function to connect to the server.

The third step, if you connect to the server in the second step, what information do you think the client needs? He has to know even who he knows? Said who? How to identify it? Is it IP plus port? IP and port good? The third step, after the connection is completed, is the next step very simple? What are you looking at? The link already exists, is it enough to send and receive data? Is it my classmates? To create a socket like this, I think we should be in decline. Is this client relatively simpler than this server? Is it socket and connect? Is it going to send and receive data next? I asked why everyone has no expression? I thought I wrote it wrong.

What is the third step called? Is he sending data first? Sending data, is sending data equivalent to writing data transfer? Which function to call? write or what? send it? What about after this thing? Which function is called to receive data? read or what? recv is not used in the end? After you're done, you have to close, right? close is fine.

Is there only one close? Is it my classmates? Well, the overall process of this client is like this, let's follow this process and write down the code for everyone. This should be much simpler to write than that, right? Much simpler. good,

 

Then let's continue to write this and I will tell you again. So is the first step of creating a socket the same as that of the server? Well, you can pick up that sentence directly, but this one returns the file descriptor, which is used for communication, the server. Is the server used for monitoring? Add a note to you, what is it used for? Used to communicate with the server, right? Okay, the second one is to connect to the server, so you can’t remember this one, come and see it, isn’t it this one? classmates,

What does the first parameter show? Is it fd? , the second is an address, right? , the third is not the address length? Well, which function do we want this parameter to be exactly the same as? And bind is not the same as bind ah? do you remember? That is binding, this is connection, they are exactly the same. This is the first time I’ve talked about it, so I’ll break it down for you.
 

 Okay, what does this return value say, let me see, if you don’t see it, what does it mean? You see if they said they are together, right? If the connection is successful or the binding is successful, do you return 0 for failure? return -1? It must be errno, if you give it to him, let him judge it

 connect What is the first one? cfd right? The second one, let’s write the second word first, and then assign the value at that time. For example, we call it serv. Of course, does this need to be converted strongly? struct sockaddr *, is the last one the length? sizeof(serv)? So next, should you assign this to this serv? So we need to define a structure called struct sockaddr_in serv; assignment.

What does serv.sin_family equal? AF_INET; What about the second one? serv.sin_por This is the port, who is this port? Students whose port is it? To connect to the server, do you need to know the server port? Did we set up four 8s for this server? Are you still going to use 4 8's at this point? So what are you doing? Is it a conversion? htons first parameter, what? It has only one parameter, so will it be this in the future? Is it IP next? serv.sin_addr.s_addr, can this in_addr be used? Can I use that? It's okay, it's okay to use that, but here I will use another method for you, and the two will diverge for you 

 Now do we need to convert the ip address formed by a string into something? Is it the 4-byte address in network byte order? Well, which function to use? Which function to use? inet_pton (); is not referring to the dotted decimal that ah? Is n network byte order? I can’t remember this parameter, check, is this the man inet_pton?

Well, is the first parameter af? Obviously, should we use AF_INET, AF_INET, is the second parameter an ip address in the form of a dotted decimal string? Is it where we want to save it later, right? Well, for the second one, how do we write it with this? 127.0.0.1, does this refer to the local address? good.

 Is the third parameter just this thing? Isn't he serv.sin_addr.s_addr? Is there an address here? Classmates, has anyone added it? Does it have to be void * right? right? Well, this means that we have already attached the value of this ip address? And also converted into what? Is it a four-byte integer value in network byte order? , I will type this integer value for you, isn’t it like this? Are you going to call him serv.sin_addr.s_addr? Classmates, are you hitting him? Can we print it in hexadecimal? What percentage? P right? is it this one? Well, you pay attention to this. Ok, then connect to judge whether it may fail.

man 2 connect 

What is the return value of connect? Students, success is 0, is success 0? Return -1 on failure? , It’s usually like this, I think it’s right, it’s usually like this. Ok, if it is less than 0, is it considered a failure? perror? OK, is there? Is the success of this function now equivalent to establishing a link with him and this server? After the link is established, can he send and receive data next? Data can be sent, good while. what? Send data, then let me ask everyone to send data, where does it write the data? Where did it go? What buffer is written to the kernel? Is it written buffer? Then I ask everyone, you said this, I call this write function here 

So I mean, did you say that this function directly sends the data to you? Do you have? you figured it out? No, this write function is only responsible for writing to the sending buffer. So as for who actually sent the data, it wasn't you who did it, who did it? Is the kernel clear? You just have to write there. The kernel is responsible for taking the data out of the sending buffer, and then finally calls the driver of the network card to send the data. The device that sends the data must be the network card, is it clear? Is it hardware? write? How to write the first one? Let's do this, when we send, can we read data from this standard input first, can we? What data to read? Read standard input data, which function to call? Students, read it, char buf[256], this can be smaller. Memset, what is n equal to? read, read the data first, and read the data to read this STDIN_FILENO, that is, we input from this terminal, and then send this data to the network?

Ok, then the data is read out, and then where should the data be written? Write to this network, the first cfd, the second buf, and the third n, right? Ok, so after sending it out, do you want to read the data sent by the server? Well, read the data sent by the server, which function to call? Of course, isn't it, let's initialize it first? right? n is equal to 1 read, the first parameter cfd, the second parameter buf, and the third parameter sizeof(buf).

Okay, let's type the read data, printf, then write the line directly, isn't it? Is this equivalent to reading data from standard input and then writing the read data to the network? right? Send it to the server, and after sending it, do I have to wait for the server to send the data back? Is the whole cycle going back and forth? Cycle back and forth.

Okay, let's take a look at the last one, what do you do after exiting the loop? close? which? close the socket? who? cfd? Is this socket a communication socket? Well in the end? return 0, is this your standard procedure? Of course, in this case, should we also add a judgment here, if my server closes this link, is it possible? If n is less than or equal to 0, is it equivalent to this reading failure or something? Or did the other party close the link? read error or server is closed?

 This is n, right? Is it a direct break here? Okay, so on the whole, is our program finished or not? There are only fifty or so lines in total. There are only fifty or so lines in total. Then let's compile and take a look.

Man inet_pton lacks header files, is he using them here? It looks like he can't do without him. Then we just add it every time. Which one is missing? No. 26? 26, I see 26. Is this hand type? Oh, this is wrong, what should this be? Let's mark it as x percent, which is more reasonable. That's the address, right? right? Classmate, do you need to add an enter character when you type the address? What should be added? Add a carriage return, OK, no problem, then let's test whether the server should be started first. Take a good look and I'll start it up. stand up.

 Let’s first check if the value is 127.0.0.1   

   7F plus 1 is 128? What do you say? Is 7F seven 1s? Is 7 1s plus 1 equal to 1, and the following 7 0s are not like this? How many powers of 2? Is 7 power minus one 127? Let me open this calculator for you to have a look. This is this. First of all, let’s observe this address. This is this binary. You have to use hexadecimal, right?

But you can't do that, do you know what it means? What byte order is ours? , Is this network byte order? Network byte order, is it because our machine is in little endian order? Are you going to give it away? Is this 7f in front? 7f then 0.0.1 right? So this 7f is obviously 7F when I play it, right? What is 7F? Is it this? Right? So what is this plus 1 equal to? Add 1, so is it 128, and subtracting one from 128 is 127, that's okay, that's okay.

Okay, this is this, we have shown it to you, and I will let you observe it again. Link. Netstat -anp, please pay attention to check the tcp status, what do you think? This command is called net stat. net means network. Does state mean state? -a, a refers to all, n refers to the digital display. I have this handout, let me tell you about it. You can use it like this when you use it. where? I think, this command, this a means to display all, and n means to display it in numbers. For example, is the pid a number? right? p is to display process information, such as the process name and process pid and so on.

 

 Take a look at this, I'll call you this, do you know what it means? Does grep filter it? grep 8888

Look carefully, look carefully, look at this line, this line, is this the third line of the server?

Is it a server? So is the IP of this server 127.0.0.1? This port is 8888? This side refers to Ben, and refers to the situation on my own side, because if it is connected, is it the two sides? This is 127.0.0.1:8888 on my own side, so this side is the other party's side, who is the other party referring to? Who is the counterparty of the server? is not it? Client, this is the client, the client is 51188, see if this is the client, the second line?

On the client side, should you pay attention to see if the client is 51188 this time, is this a normal response? Well, are these two related? Does each other mean mutual? His counterpart is a's counterpart is b, and b's counterpart is not a? This means that if you look at this one, it is exactly the same, um, are these two just matched? This whole, this whole down? Well, pay attention, is this the socket chip I told you earlier? Did I tell you that a link in a link actually has a socket chip? Is this a socket chip? right? It can be seen from here.

 Well, here, there is a state called ESTABLISHED. What about this, when we talk about the process state later, I will tell you the meaning of this, which means that the link has been established. Is it clear? Friends, express the link, let's see what it means LISTEN? The appearance of this LISTEN must appear on the client side or the server side? It must appear on the server side, so this LISTEN means that your process is already in the listen state, can you understand? Then the process in the listen state can allow the client to connect to him.

Obviously, have we already established a link below? Can you understand? They've already established a link, isn't it this one? Is this a connection right? Then we will show you a demonstration after everyone has seen it. Then my client communicates with the server.

This is the server client, write it down, hello, received it, did he receive it? look normal. Then come another one, is it normal to receive the conversion? Then let's take a look, if I say that the server is turned off, will the client exit, do you understand what it means? Well, did you quit? No, think about it again, why not? No? Where is the blockage? Does our client receive input from standard input? Then is it necessary to write this data to the network after we input the data? Who is it going to be sent to? Is it the server? Where is he stuck? It is blocked on the standard input. If you don’t believe me, I will write it. I will write this once. It will be released immediately. Hello, have you quit?

 Well, that is to say, the second read. When he was reading the Internet, did he see that the link of the other party had been closed? Did he log out immediately, so that he would no longer be blocked? Do you understand? Is this what we do? Then let's observe whether the connection has been established before accept, do you know what it means? It is also very easy to measure. The code, I will adjust it for you a little bit, this is called, then I will do this, you can see that I will do an experiment for him like this.

 How to do the experiment? I am under listen, above accept, I add a sleep to him, 30, do you know what it means? I add a sleep, and then I use the client to connect to it, then this function must still be in sleep 30, then you can see if this link already exists, do you know what it means? Check if this link is already there? gcc -o server

 

 

, is it up, and then I am like this, I use the nc command 127. 0.0.1, and then press Enter 4 times 8, it is obvious that the accept function does not return immediately? This sentence was not typed. Well, then you can observe whether there is a link. Netstat -anp | grep 8888 Is there a link? Have you got it? Classmates, yes, as long as you can see this, it means that the link already exists, is it clear? That is, the link is actually established before accept.

 

 So about this accept function, I have a picture here for you to take a look at. This picture, this one is a bit inelegant, I didn’t want to see this at first, so I’ll look it up for you. This comparison is a bit funny, let me see which one is still there. Why, why did you try to find him but couldn't find him? Look at this picture to understand, it was given to me by a colleague when I was from Shenzhen,

I saw this very much. Some students may not understand the meaning of accep when I talked about it at that time, so why should I say this here? Just now everyone mentioned the sleep 30, did I add a sleep 30 on top of the accep? Did I tell you that the Excel function may have been linked before, so calling the accept function is equivalent to taking out a used and available link? So obviously what does that mean? For example, this is our linked queue. Brothers, there are people here who use the linked queue, that is to say, there is already a link in the queue before accept, so calling the accep function is actually to take out a link from this link. Come on, do you know what it means?

Take out an available link from this linked queue, what does this mean beyond words? classmates? The implication is that before calling acceptp, the link may already exist, right? Students, of course, if I don't add sleep 30 in front, will the accept function return immediately as soon as the client connects? Why? Because it was completed in an instant, do you know what it means? If there is a sleep before accep, then at this time, you should not think that there is no new link generated during sleep 30 before accep, which is unreasonable

 Knowing this, calling accept does not mean creating a new connection. I'll write this for you. I am more aware of this. some details. The first detail, calling the accept function does not mean creating a new link but what students? But what is taken out of the linked queue? A link is available. Have we tested this sentence for you? Because there is already this link before accept, we call accept just to say to take out an available link, not to create a new link, can this sentence be clear? Okay, that's good, so what else do we have about the where function? There is also a small point that has not been measured for you. which point? this point.

Let's see if the client's IP and port are not available to you here? 42 Is the second parameter and the third parameter both NULL? Let's use it for him here, not in my case, so for this reason, should we define a structure to receive information from this client? What structures? Is it struct sockaddr_in client; this is called client, does it still need a length, right? Students, what about the length? Not len, his data type

 man 2  accept

 Is it this data type? socklen_t len

What should its initial value be? Students, is it sizeof(client)? Did I tell you that this parameter is not one? Is it both an incoming and an outgoing? Ok, how to write the first parameter? Do you want to perform mandatory type conversion? , is it a conversion problem? Let's see if this is it? You have to convert this type right? How to write here? Is it client? Is it to take the address? Well, is this both incoming and outgoing? Right students? How do you write this? If it is only incoming, is it necessary to use this address? No need, brothers. Well, is this numeric type just passing the value directly? This is both incoming and outgoing, len is an input and output parameter.

 Ok, then let's get the client address, and then let's print the client's IP and port, okay? , how about the first parameter? In this case, first of all, please pay attention to whether we have obtained an IP now, and what type of IP is it? Classmates, is it a long integer value in network byte order? Should we call a function to convert this value into some string form? Which function to call? inet_ntop is not this? It doesn't matter if you can't remember the parameters, check this slowly.

man  inet _ntop 

the first is af 

 This is the first parameter, what should be filled in?

is not it? When you see this af, you can easily think of this AF_INET, the second is src, is this an incoming or a transmission? Incoming, who is incoming? Is it the value of the 4-byte integer network byte order? Integer value, how to write this? Now you write the address, how should it be? It should be client.sin_addr.s_addr, has the value already been obtained? Yes or no? Is this ip address stored in this s_addr, right? Classmates, you can’t remember this. After you take this code over, write it down carefully, okay?

What is the third parameter? Do I want to save this ip address in which one? Should we define something here? What is not IP ah? How many? Is 16 enough? memset, is it this, take a look? Then we should pass the third parameter as the second parameter, is it enough to write SIP? The last parameter, does the last parameter refer to its length char * dst? Is it sizeof(SIP) or not this? In that case, will our ip address be saved in this sip?

? Then look at its return value, what is the return value of this function? Is it here? Is it back to char *? Does this char * point to him? char * dst? Yes or no? Did I tell you this? Well, okay, how about looking at the IP address? Is the ip address also a network byte order value? Is it network byte order? So what should you convert it into? Host byte order.

 

 You notice that the values ​​you get from the network are all in network byte order. Is this because after you call the accept function, are those IP address end libraries saved for you in this client? The value stored here must be the value of the network byte order, as long as it is this, what is it? This data type, the length greater than 1 is like this, you need to convert, so is this port number also two bytes? Is it short plastic surgery? How does this transform it into something? How to change host byte order? ntohs (client.sin_port) is not the case? Is this port stored in this sin_port, this is a bit long, 

 

Well, is it like this? You should also learn to use these two functions, especially after you call the accept function, then when you get the address information of the client, you need to add this thing, so everyone think about the benefits of this? Our accept function writes the second parameter to it. What is the benefit of obtaining the IP address of the other party? One of the biggest factors is that it can set a blacklist, do you know what it means? Let me give you an example, so my server can allow, prevent certain malicious clients from accessing, right? When I saw it was yours, I immediately rejected it, huh? How to get rid of them? After you accept, do you know who gave you and who connected you? After you make a judgment, you can use this malicious client of this IP as an IP, and you save it in a file, can you search for this file, is this IP in this file? Or is it okay for you to form a list? Anyway, there are many ways to do this, and you can save it in the database. If you check it, this IP will be closed directly in this blacklist. Does he want to send data and can’t send it? Know what it means. Okay, let me test this for you, is it true?

 

 This is already occupied bind error, this is indeed occupied. Has he quit yet? If he didn't quit, can you connect? Brothers, you can't connect, is it because he also quit, right? As soon as the server is closed, does the NC also exit immediately? Is it now that you restart and still can't start? Is it unable to start? bind error This will probably take about 1 minute to wait about 1 minute.

 why? We will say later that this is called address reuse. After the address is reused, it will last for about 1 minute, or about 1 minute. Then there is a function that can solve this problem. Let’s talk about it tomorrow. The function called set sock OPT can Set port multiplexing. Now start it up and see if it is up, is it up? Okay, get up, let's see if we can get the IP of this client, let's get it.

Get up, no response, no, this, he is asleep, is there a sleep? right? Is this a sleep30? At that time? Ok, have you found this address and the port is 51992, so how do you check and verify that this port is correct? no

 

Is the port 51992? Then the printed out is 51992, do you see it? is it this one?

So the client port is not a problem, right? Okay, let's talk about this program, let's talk about this program, so basically we have almost finished talking about it, this sleep 30 is removed, is this sleep 30 added here? The removal of this part is to add a comment for you to get the IP and port of the client. Of course, if you get it, it should be obtained from the second parameter of accept. Is this what we call this function inet_ntop and this function ntohs to print out? Brothers, you have to know how to use this, try again, you have to wait for about 1 minute, maybe less than 1 minute,

When it gets better, try again. Let’s use the client. There is no such thing. This is after the sleep is removed. Does this mean that there will be information immediately? The port is 51994, it looks different every time, and then check if it is 51994 51194, look, is it? It’s okay for me to shake it, so it’s normal to send it out, right? close it? Is not also quit, is not it? Classmates, this.

 

 

 

Guess you like

Origin blog.csdn.net/chezabo6116/article/details/130090037