In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >
Share
Shulou(Shulou.com)06/01 Report--
1. What is a computer network?
Multiple computers communicate-> computer network.
2. The complexity of computer communication
(1) the complexity of transmitting information (type, content)
(2) the amount of information
(3), transmission distance (interference.)
(4) the security of information
(5) the integrity and closeness of the computer system.
We should not only ensure the closeness of the computer, but also achieve the communication of the computer.
3. Ip address
(1) the IP address is limited, so we need a way to reuse the IP address.
(2) the reuse of IP address leads to the complexity of data transmission (ATM, store-and-forward mechanism; routing mechanism).
(3) the IP address is too abstract and inconvenient to use, so the human culture escape of IP address is given: domain name.
(4) the domain name can only represent one IP network address, so it can only represent a node entity on the network.
(5) in fact, when you visit a node, you essentially use an IP address, so you need to convert the domain name to an IP address (DNS...).
4. Classification of IP addresses
(1) the IPv4 address is 4 bytes, with. Partition; the IPv6 address is 16 bytes
Rule: in IP address division, generally do not take all-0 / all-1
IP is generally divided into five categories
A, B, C, D, E, the commonly used IP addresses are Class A, Class B, and Class C.
Class An IP: the first byte starts with 0 0000 00000 murmurs-> 0111 1111 0000127
Class B IP: the first byte starts with 10 1000 00000 murmurs-> 1011 1111 128 million 191
Class C IP: the first byte starts with 110 1100 00000 murmurs-> 1101 1111 192 words 223
(2). Subnet mask: set the network number to 1 and the host number to 0 (set the bits of each byte)
Example: class C IP address, 3-byte network number and 1-byte host number
1111 1111 1111 0000 0000
255. 255. 255. 0
(3) Subnetting: the subnet mask of class C address may not always be 255.255.255.0.
This is also to see if there is subnetting under Class C IP.
If there is a subnetting, the last byte, that is, the host number, may be 2 segments (01ax 10) and 4 segments (00max 01max 10lap 11).
Example: 192.168.3.11xx xxxx 1111 1111 1111 1100 0000
The corresponding subnet mask is 255.255.255.192.
(4), IOS and TCP/IP
Model analysis
(5), port number
Port: the number that uniquely identifies the application
When we send and receive data through QQ, Wechat and mailbox, it does not lead to the chaotic reception of the data. How do we do that? How did you find it one by one?
A certain application on the computer is identified by the port number, that is, to find the corresponding number.
When we send data, we first look for the physical computer through IP, and then look for the corresponding application according to port.
(6), TCP and UDP
UDP is a part of TCP/IP system.
Both TCP and UDP are transport layer protocols.
TCP protocol:
I >, connection-oriented transport protocol, reliable, synchronous
Ii >, the characteristics of connection-oriented network transmission: a, one side needs to actively establish a connection, and the other party receives the connection request; b, the data transmission can be carried out only after the connection has been established; c, when the data transmission is completed, the connection needs to be released, and the two ends of the connection jointly decide whether the connection is maintained.
UDP protocol:
Connectionless: that is, when data transmission is carried out, there is no need to create a connection in advance.
B, unreliable: there is no way to know whether the data sent will reach the destination or when.
C, asynchronous:
(7) TCP's three-way handshake and four waves
TCP- > at least 3 handshakes (last time to prevent mispress, twice, it may be deadlock);: phone call model
Model analysis
TCP- > 4 waving. Model: boyfriend and girlfriend breakup model
Through IP, only physical connectivity can be guaranteed. As for the form of sending and receiving data, it does not belong to it.
127.0.0.1: the local echo address can be used as a test machine, and it can be connected to ping without installing a network card.
5. Programming implementation of TCP.
Basic socket programming for TCP is the following steps:
(1) Model analysis
(2), code implementation
Utili.h
# include#include#include#include#include#include#define SERVER_PORT 9090#define SERVER_IP "127.0.0.1" # define LISTEN_QUEUE 5#define BUF_SIZE 255
Server-side code:
# include "utili.h" / / TCPint main (void) {int sockSer = socket (AF_INET, SOCK_STREAM, 0); if (sockSer = =-1) {perror ("socket"); return-1;} struct sockaddr_in addrSer, addrCli; addrSer.sin_family = AF_INET; addrSer.sin_port = htons (SERVER_PORT); addrSer.sin_addr.s_addr = inet_addr (SERVER_IP); int yes = 1 Setsockopt (sockSer, SOL_SOCKET, SO_REUSEADDR, & yes, sizeof (int)); / / reuse of addresses and ports socklen_t len = sizeof (struct sockaddr); int res = bind (sockSer, (struct sockaddr *) & addrSer, len); if (res = =-1) {perror ("bind"); close (sockSer); return-1;} res = listen (sockSer, LISTEN_QUEUE) If (res = =-1) {perror ("listen"); close (sockSer); return-1;} int sockConn; char sendbuf [BUF _ SIZE]; char recvbuf [BUF _ SIZE]; while (1) {sockConn = accept (sockSer, (struct sockaddr *) & addrCli, & len); if (sockConn = =-1) {continue } else {printf ("Server Accept Client Connect OK\ n");} printf ("Ser: >"); scanf ("% s", sendbuf); if (strncmp (sendbuf, "quit", 4) = = 0) break; send (sockConn, sendbuf, strlen (sendbuf) + 1,0); recv (sockConn, recvbuf, BUF_SIZE, 0) Printf ("Cli: >% s\ n", recvbuf);} close (sockSer); return 0;}
Client code:
# include "utili.h" / / TCPint main (void) {int sockCli = socket (AF_INET, SOCK_STREAM, 0); struct sockaddr_in addrSer; addrSer.sin_family = AF_INET; addrSer.sin_port = htons (SERVER_PORT); addrSer.sin_addr.s_addr = inet_addr (SERVER_IP); struct sockaddr_in addrCli; addrCli.sin_family = AF_INET; addrCli.sin_port = htons (7070) AddrCli.sin_addr.s_addr = inet_addr ("192.168.1.155"); int yes = 1; setsockopt (sockCli, SOL_SOCKET, SO_REUSEADDR, & yes, sizeof (int)); / / reuse of addresses and ports socklen_t len = sizeof (struct sockaddr); int res = bind (sockCli, (struct sockaddr *) & addrCli, len); if (res = =-1) {perror ("bind") Close (sockCli); return-1;} res = connect (sockCli, (struct sockaddr*) & addrSer, len); if (res =-1) {perror ("connect"); close (sockCli); return-1;} else {printf ("Client Connect Server ok\ n");} char sendbuf [BUF _ SIZE]; char recvbuf [BUF _ SIZE] While (1) {connect (sockCli, (struct sockaddr*) & addrSer, len); recv (sockCli, recvbuf, BUF_SIZE, 0); printf ("Ser: >% s\ n", recvbuf); printf ("Cli: >"); scanf ("% s", sendbuf); if (strncmp (sendbuf, "quit", 4) = = 0) break Send (sockCli, sendbuf, strlen (sendbuf) + 1,0);} close (sockCli); return 0;}
(3) running result
6. Programming implementation of UDP.
Basic socket programming for UDP is the following steps:
(1) Model analysis
(2), code implementation
Utili.h
# include#include#include#include#include#include#define SERVER_PORT 9090#define SERVER_IP "127.0.0.1" # define LISTEN_QUEUE 5#define BUFFER_SIZE 255
Server-side code:
# include "utili.h" int main () {int sockSer = socket (AF_INET, SOCK_DGRAM, 0); if (sockSer = =-1) {perror ("socket"); return-1;} struct sockaddr_in addrSer, addrCli; addrSer.sin_family = AF_INET; addrSer.sin_port = htons (SERVER_PORT); addrSer.sin_addr.s_addr = inet_addr (SERVER_IP) Socklen_t len = sizeof (struct sockaddr); int res = bind (sockSer, (struct sockaddr*) & addrSer, len); if (res = =-1) {perror ("bind"); close (sockSer); return-1;} char sendbuf [buff _ SIZE]; char recvbuf [buff _ SIZE] While (1) {recvfrom (sockSer, recvbuf, BUFFER_SIZE, 0, (struct sockaddr*) & addrCli, & len); printf ("Cli: >% s\ n", recvbuf); printf ("Ser: >"); scanf ("% s", sendbuf); if (strncmp (sendbuf, "quit", 4) = = 0) {break } sendto (sockSer, sendbuf, strlen (sendbuf) + 1,0, (struct sockaddr*) & addrCli, len);} close (sockSer); return 0;}
Client code:
# include "utili.h" int main () {int sockCli = socket (AF_INET, SOCK_DGRAM, 0); if (sockCli = =-1) {perror ("socket"); return-1;} struct sockaddr_in addrSer; addrSer.sin_family = AF_INET; addrSer.sin_port = htons (SERVER_PORT); addrSer.sin_addr.s_addr = inet_addr (SERVER_IP); char Sendbuf [buff _ SIZE] Char recvbuf [buff _ SIZE]; socklen_t len = sizeof (struct sockaddr); while (1) {printf ("Cli: >"); scanf ("% s", sendbuf); if (strncmp (sendbuf, "quit", 4) = = 0) {break;} sendto (sockCli, sendbuf, strlen (sendbuf) + 1, 0, (struct sockaddr*) & addrSer, len) Recvfrom (sockCli, recvbuf, BUFFER_SIZE, 0, (struct sockaddr*) & addrSer, & len); printf ("Ser: >% s\ n", recvbuf);} close (sockCli); return 0;}
(3) running result
Server screenshot
Client screenshot
The socket on the server side is global, does not communicate with any client, and each new client is assigned a new socket to communicate.
LISTEN_QUEUE: the size of the waiting queue (maximum number of queues)
UDP: you have to know where the server is first.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.