2. What is socket?
You often hear people talk about "socket". Maybe you still don't know its exact meaning. Now let me tell you: it uses the standard Unix file descriptor (filedescriptor) to communicate with other programs.
What?
You may have heard some Unix hackers say, "Yeah, everything in Unix is a file !" The guy may be talking about the fact that when a Unix program executes any form of I/O, the program reads or writes a file descriptor. A file descriptor is only an integer associated with the opened file. However (note later), this file may be a network connection, FIFO, pipeline, terminal, file on disk or something else. Everything in Unix is a file! Therefore, when you want to communicate with other programs on the Internet, you will use the file descriptor. You must understand what you just said. Now you may come up with the idea: "Where can I get the file descriptor for network communication ?", You can use the system to call socket (), which returns the socket descriptor (socket descriptor), and then you can use it to send () and recv () call.
"... ", You may have a lot of doubts." If it is a file descriptor, why don't we generally call read () and write () for socket communication?" The simple answer is: "You can use it !". The answer is: "You can, but using send () and recv () gives you better control over data transmission ."
There is a situation where there are many sockets in our world. There are DARPA Internet addresses (Internet sockets), local Node path names (Unix sockets), and CCITT X.25 addresses (you can ignore X.25 sockets completely ). Maybe there are others on your Unix machine. Here we will only talk about the first type: Internet socket.
2.1 two types of Internet socket
What does it mean? Are there two types of Internet sockets? Yes. No, I am lying. But I don't want to scare you. Here we only talk about two types. In addition to this, the "Raw Sockets" I intend to introduce is also very powerful and worth looking.
So what are the two types? One is "Stream Sockets" and the other is "DatagramSockets" (data packet format ). We will also use "SOCK_STREAM" and "SOCK_DGRAM" when talking about them later ". A datagram socket is also called a "connectionless socket" (you can use connect () if you really want to connect ().) Stream sockets are reliable two-way communication data streams. If you output "1, 2" to the socket in order, they will arrive at the other side in order "1, 2. They are passed without errors and have their own error control, which is not discussed here.
What is streaming socket? You may have heard of telnet, isn't it? It uses streaming sockets. You need to input the characters in order, isn't it? Similarly, the HTTP protocol used by the WWW browser also uses them to download the page. In fact, when you telnet to a WWW site through port 80 and enter "GET pagename", you can also get html content. Why can stream sockets achieve high-quality data transmission? This is because it uses The Transmission Control Protocol, also known as TCP (see The RFC-793 for details .) TCP controls your data to arrive in order and there is no error. You may have heard "TCP" because you have heard "TCP/IP ". The IP here refers to Internet Protocol (refer to RFC-791 .) IP addresses only process Internet routes.
What about the datagram socket? Why is it connectionless? Why is it unreliable? The fact is that if you send a datagram, it may arrive, and its order may be reversed. If the package arrives, there is no error in the package. The datagram also uses an IP address for routing, but does not use TCP. It uses the User data Datagram Protocol, also known as UDP (see RFC-768 .)
Why are they connectionless? It is mainly because it does not maintain a connection like a streaming socket. You only need to create a package, construct an IP address header with the target information, and then send it out. No connection is required. They are generally used for transfer package-package information. Simple Applications include tftp and bootp.
You may wonder, "How can I work normally if data is lost ?" My friend, each program has its own protocol on UDP. For example, for each packet sent by the tftp protocol, the recipient must send it back and say "I have received it !" (A "correct command response" is also called an "ACK" package ). If the sender does not receive a response within a certain period of time (for example, 5 seconds), it will resend until it gets an ACK. This ACK process is very important when implementing the SOCK_DGRAM application.
From the column xiaobin_HLJ80