Chapter 3 - Network Programming Fundamentals

  1, the socket: socket file based on one kind, AF_UNIX, (from a socket main function early in the Unix system developed, mainly for the same computer, a plurality of direct communication programs)

        Unix System central idea: everything is a file

  2, based on the type of network sockets, AF_INET

  3,TYPE:

    tcp protocol: SOCK_STREAM

UDP protocol: SOCK_DGRAM

4, tcp: reliable, connection-oriented, oriented transport mode byte stream

5, udp: unreliable without connection-oriented, packet-oriented data transmission, but quickly
6, the gateway: similar to the inlet and outlet of a local area network 
7, segments: IP address range within a LAN
8, Subnet Mask: & ip address subnet mask until the segment
9, osi five models:
an application layer : http, https, ftp
transporting layer: tcp / udp four switches, four router
network layer: layer protocol router IP switch
network link layer: ARP protocol bridging Ethernet switch card
physical layer: an optical fiber cable to transmit electrical signals hub
a computer on the same program even a py: open a file 
b, two computer how to communicate: a cable connected to
c, a plurality of computer communications:
EX: 1 PC (source) to find computer 2 (target)
computer 1 first transmits a request frame (including my ip address, mac address, ip addresses are looking for targets),
switch broadcasts this information to other host
target host receives the message, finds himself comparison is to be looking for a target, return switch, the own ip address, mac address
, ip address is returned to the host xxxxxx is
returned to the host switch form a single wave

knowledge:
1. mac address: physical address is a globally unique, similar to the ID card
2, ip address: is a four site decimal, which identifies the computer locations in the network
protocol acquisition target by ip address destination mac address of: 3, arp protocol
4, the port: the operating system for each application on the host computer are randomly assigned an port
TCP protocol programming process: 
the server Client
instantiate an object instance of the object
of the IP address and port
monitor
receives the connection server client
transceiver transceiver
OFF OFF
switches and routers difference?
The main function is to organize the local area network switch, the switch parses after internal processing, the information point, multi-dot form, and transmits to the fixed end of the
main functions of the router: for inter-network for data transmission, selecting an optimal path router
EX:
  if you shall more than one computer connected to a network cable, you can use the switch
  if you have only one external network IP, but you have a lot of computers require Internet access, you can use the router
 
# 案例1 通信
import socket
import time
sk = socket.socket()  # 买了一个手机
sk.bind(('192.168.1.107',18080))    # 买了一张手机卡
sk.listen()     # 开机

sock,addr = sk.accept() # 等待朋友连接
msg_recv = sock.recv(1024)  # 接收朋友传来的bytes信息
sock.send(b'library')
print(msg_recv.decode('utf-8')) # 进行解码
time.sleep(20)
sock.close()    # 关闭电话
sk.close()      # 关机
 

 

 
import socket

class MySocket(socket.socket):      # 继承socket文件中的socket类
    def __init__(self,encoding='utf-8'):    # 给它一个默认的utf-8编码格式
        self.encoding = encoding    # 将utf-8编码格式封装给encoding
        super(MySocket, self).__init__(type=socket.SOCK_DGRAM)  # 执行父类中的__init__方法,将默认tcp协议改成udp协议

    def my_sendto(self,msg,addr):   # 定义一个发消息类
        return self.sendto(msg.encode(self.encoding),addr)  # 调用父类中的sendto方法

    def my_recvfrom(self,num):  # 定义一个接收方法
        msg_r,addr = self.recvfrom(num) # 调用父类中的recvfrom方法
        return msg_r.decode(self.encoding),addr
 

 

 

 






Guess you like

Origin www.cnblogs.com/jiujiang/p/11449598.html