------------------------------Internet Protocol and protocol family------------------------------
TCP/IP Protocol Introduction: Computer network protocol, different types of different manufacturers between the terminal to communicate, standardize the agreement
。
Internet Protocol Family: There are probably hundreds of protocols for the TCP/IP protocol family, in which the TCP protocol, IP protocol usage comparison
More so everyone by default becomes TCP/IP
IP Address: Network address (IP address and subnet mask are obtained with operation) and host address
Class A IP address: consists of a 1-byte network address and a 3-byte host address, the highest bit of the network address must be "0"
(Address range 1.0.0.1-126.255.255.254)
Class B IP Address: consists of a 2-byte network address and a 2-byte host address, the highest bit of the network address must be
"10" (128.1.0.1-191.255.255.254)
Class C IP Address: consists of a 3-byte network address and a 1-byte host address, the highest bit of the network address must
Is "110" (range 192.0.1.1-223.255.255.254)
------------------------------Socket Introduction------------------------------
#定义: A socket, or socket, is a way of communicating between processes, a primary
To be different is:
#import socket: Apply Socket Package
#socket. Socket (Address Family,type): Creates a socket that returns the socket descriptor
Address Family: You can choose af_inet (for inter-process communication between Internet) or
Af_unix (for the same machine interprocess communication), the actual work commonly used af_inet
Type: Socket type, can be socket_stream (stream socket, mainly used for TCP protocol
) or Socket_dgram (datagram sockets, mainly for UDP protocol)
------------------------------UDP Protocol------------------------------
UPD protocol: Fast propagation of data, but not too secure. 19 World 70 's
#udpSocket = socket (af_inet, SOCK_DGRAM): Create socket
#sendData = raw_input ("Please enter data to send:"): Getting data from the keyboard
#sendAddr = (' 192.168.1.103 ', 8080): Prepare the address of the receiving party
#bindAddr = (', 7788) # IP address and port number, IP is generally not written, representing any IP of this machine
#udpSocket. Bind (BINDADDR) # binds local information, if a network program is not bound, the system
are randomly assigned
#udpSocket. SendTo (SendData, SENDADDR): Send data to the specified computer
#recvData =udpsocket.recvfrom (1024): Receive data, 1024 indicates the maximum number of bytes received at this time
#udpSocket. Close (): Close socket
------------------------------Write a case------------------------------
1 #Coding=utf-82 3 #referencing the corresponding package4 ImportSocket5 6 ImportThreading7 8 ImportSYS9 Ten #global variable Socket OneGlobal_soc=Socket.socket (Socket.af_inet,socket. SOCK_DGRAM) A - #Threads: Information acceptance - classMythreadjieshou (Threading. Thread): the #overriding Constructors - def __init__(self,port): -Threading. Thread.__init__(self) - #Process Port +self.port=Int (port) - + #overriding the Run method A defRun (self): at #Socket global variable - GlobalGlobal_soc - - #Create IP and port numbers -Self.locker= ("", Self.port) - in Global_soc.bind (Self.locker) - to whileTrue: +Self.result=global_soc.recvfrom (1024) - Print("\ n") the Print(Self.result) * $ global_soc.close ()Panax Notoginseng - #Threads: Sending information the classMythreadfasong (Threading. Thread): + #overriding the Run method A defRun (self): the #global variable Socket + GlobalGlobal_soc - $ #Create the destination port number for the Send $Addr= ("192.168.13.33", 8080) - - whileTrue: theStrcount=raw_input ("Please enter:") - Wuyi #Sending of information the global_soc.sendto (STRCOUNT,ADDR) - Wu global_soc.close () - About $ if __name__=="__main__": - #create the corresponding thread -Jieshou=mythreadjieshou (sys.argv[1]) -fasong=Mythreadfasong () A + #Process Open the Jieshou.start () -Fasong.start ()
Network protocol UDP