day 31 network programming, simple communication server, a simple messaging client


Network Programming
C / S and B / S structure
c: client s: server client, server, EG computer to upload qq, micro-channel.
B: Browser S:. Server browser server, EG Jingdong, Lynx
bs architecture essentials cs also

learning network programming in order to write a software architecture cs
c end network ------- ----- s end


s end:
1 fixed ip
2 has been stable in operation, and supports concurrent

# network: the network connection media + network protocol (OSI seven)

# network protocols: OSI seven layer protocol: correspondence table will pass network number was

# five protocols: mesh count thereof should pass

physical layer : 010101 electrical signal
data link layer : the physical layer of electrically signal packets, each set of data called a message / data frames, each data frame is divided into: a header and a data head data of two parts
- each data packet, a header and a data portion by
- head: fixed 18 bytes, 6: sender address / 6: receiver address / 6: data type
-mac address: sender, receiver address, that address mac
- each card has a unique address mac: 12 hexadecimal number (the first six is the vendor number, line number is the last six)
- broadcasting: communication within the same LAN, there will be extensive Broadcast storm
Network Layer :
-ip: ipv4: 32-bit binary represented: dotted decimal notation from 0.0.0.0 to 255.255.255.255, the range is limited, not shown all the network devices, so there IPv6
- Subnet Mask: through the sub ip mask, and ip is determined whether the two in the same network segment, the ip address and subnet mask do a bitwise
ip address: 172.16.10.1: 10101100.00010000.00001010.000000001
subnet mask: 255.255.255.0: 11111111.11111111.11111111.000000000
bitwise aND: 172.16.10.0 10101100.00010000.00001010.000000000
-172.16.10.10 / 24-ip address and subnet mask contains
-ip there with mac conversion relationship
- host 172.16.10.10/24 access 172.16.10.11/24
- ARP protocol: broadcast transmitted as packets, the destination hosts mac address
-mac address learning: mac address mapping table and the ip address
- the first one will be added to the received data { '172.16.10.11 "in ip / mac mapping table: } ddsadfgegsdgsdg
- any kind of protocol has its header and content
transport layer:
TCP protocol:
- three-way handshake, four wave (focus, Figure), how the connection is established, how to send data to ensure reliable, disconnect how to disconnect, there is a state in the process of establishing a connection
- If you see a large number of services syn_rcvd state
-dos and ddos attacks: denial of service attacks, distributed denial of service attacks
- port number: port range 0-65535,0-1023 to occupy the port system
-udp protocol:
- send no need to respond, so the data can not rely on (see video)
- port
- + IP subnet mask uniquely determined by a device
- via IP subnet mask + CD + port number uniquely identify a software
- semi connection pool

application layer :

Internet process analysis:
- input browser www.baidu .com
- dns server will take into ip address via dNS
- ip + port number to the address transmission request, the server will have access to Baidu


socket: an abstraction layer between the application layer and the transport layer, it is the TCP / IP abstract layer complex operations into several simple interface supply has been achieved with the process of communication in the network layer calls

write a cs architecture through the socket in python




Simple communication server
# Importing a socket module 
Import socket

# think of buying a mobile phone call: socket.SOCK_STREAM represent establish tcp connection, udp connection socket.SOCK_DGRAM
# bought a mobile phone
the SOC = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
# telephone card inserted : binding ip address passed tuple: (ip, port number)
soc.bind (( '192.168.11.146', 8080)) # If you write native ip, LAN can access the external
# soc.bind (( '127.0.0.1 ', 8080)) # write 127.0.0.1, if only access their own

# boot, listen , this is a semi-5 connection pool size
soc.listen (5)
# wait for someone to give me a call
Print (' xxxx ')
conn, soc.accept = addr ()
Print ( 'YYYY')
Print (addr)
# Conn passage is
# 1024 bytes received
Data = conn.recv (1024)
Print ( 'I received client's hair', data)
# Transmit data, format the data bytes must be
conn.send
conn.send (b'xxxxx ')
# Hang up the phone
conn.Close ()
# destroy mobile phone
soc.close ()


simple communication client
Import socket
# create a socket object
the SOC = socket.socket ()
# connection server
soc.connect (( '192.168.11.146', 8080))
# send
soc.send (b'xxx ')
Data = soc.recv (1024)
Print (' I get back to the server ', Data)
# close the connection
soc.close ()
 
 

Guess you like

Origin www.cnblogs.com/wwei4332/p/11466297.html