First, understanding the TCP foundation
1. Addressing
TCP to recognize remote machines, each machine based on a TCP/IP network has a unique IP address. (IP)
TCP needs to know which program to run on the remote machine communicates, and each program uses a unique port number. (port)
Therefore, the endpoint for each TCP connection is uniquely identified by an IP address and a port number. (Ip:port)
Although the IP and port number, TCP can work very well, but it is difficult to remember a string of numbers, for this reason there is a DNS.
Now want to connect with the remote machine, you can request to connect the machine IP address corresponding to the DNS (such as: www.baidu.com),
DNS provides an IP address that you can then establish a connection to.
2. Reliability
TCP is a reliable protocol that, unless there is a problem with the entire network, the data will be properly transmitted to the other end intact.
This reliability is achieved through the following rules:
(1) In order to prevent data from being destroyed during transmission, each message contains a checksum code. This checksum is a code that guarantees that the packet has not been changed during transmission. When the packet arrives at the destination, the receiver compares the checksum with the data in the received message. If the check code is incorrect, the packet is omitted.
(2) In order to prevent packet loss, TCP asks the receiving party to give feedback on each packet received. If the recipient does not provide feedback, the sender will automatically resend it. Because the system will automatically handle this problem, the developer of the program does not have to know the problem at all. TCP will always try to send packets until the receiver receives them, or it will determine that the network connection is broken and return an error message in the program.
(3) To prevent packet duplication or sequential errors, TCP sends an ordinal number for each packet sent. The receiver checks the sequence number, ensures that the packet is received, and merges all the packets in order. At the same time, if the receiving party sees an ordinal number that has already been seen, the packet is discarded.
Ii. using client/server mode
Under the client/server architecture, the server listens for requests from clients and, upon request, establishes a connection to process them.
The client always starts the application to the end of the connection, and the server waits for the client to connect to the end.
1. Server-side port number
There is a list of officially assigned ports maintained by the International Internet Address Distribution Board on www.iana.org.
If you write a server whose service is not in this list, you should choose a port number that is larger than 1024 and is not occupied on the machine.
This avoids conflicts with other services and can have a maximum port number of 65535.
2. Client port number
Typically, the port number of the client is not very important. Typically, the client will pick a port number randomly by the operating system.
The client's system picks a port number that is guaranteed not to be used, known as "short-lived".
Iii. Understanding the basis of UDP
UDP, which is used to send very short messages to other systems from one system.
It provides only one guarantee: The data you receive is complete.
It does not guarantee that the data is actually received, nor does it guarantee that the data will be accepted only once, and that the order of information received is not consistent with the sending time.
However, as long as there is no attack by the attacker bypassing security, the data received over UDP will usually be complete.
The advantage is that because there are no guarantees above, TCP is less expensive than TCP, it takes time to establish and close a connection, and UDP has no concept of connection, so there is no problem of spending time establishing and shutting down the connection. (such as the DNS system).
Use reference:
Tcp:
# You need a reliable data transfer to make sure that you arrive at your destination intact.
# Your protocol requires more than one request and server answer
# you want to send more data
# A brief delay in initial connection is tolerated
Udp:
# You don't care if the packets arrive or don't care if the packets arrive in the right order, or you can detect them yourself and fix them yourself.
# Your agreement includes only basic requests and answers
# You need to establish a network session
# Only a small portion of the data is transmitted. UDP is limited to a packet of not more than 64KB of data, usually people only use UDP to transmit data below 1KB
Four, the bottom interface
1. Basic client Operations
Import Socket,sys
Port = 70
Host = ' quux.org '
filename = '/'
s = Socket.socket (socket.af_inet,socket. SOCK_STREAM)
Try
S.connect ((Host,port))
Except Socket.gaierror, E:
print "Error connecting to server:%s"%e
Sys.exit (1)
S.sendall (filename + ' \ r \ n ')
While 1:
BUF = S.RECV (2048)
If not Len (BUF):
Break
Sys.stdout.write (BUF)
1.1. File class objects
Import Socket,sys
Port = 70
Host = ' quux.org '
filename = '/'
s = Socket.socket (socket.af_inet,socket. SOCK_STREAM)
Try
S.connect ((Host,port))
Except Socket.gaierror, E:
print "Error connecting to server:%s"%e
Sys.exit (1)
FD = S.makefile (' rw ', 0)
Fd.write (filename + "\ r \ n")
For line in Fd.readlines ():
Sys.stdout.write (line)
The file class object is generated by the makefile () function, which has two optional parameters: Operation file class mode and cache mode.
Operation file class mode, read-only, write-only, read and write.
The cache mode is primarily used for disk files, but for interactive network programs It may hinder the operation of the program, so it is best to close it by setting it to zero.
2. Basic server Operations
Import socket
Host = ' '
Port = 51423
s = Socket.socket (socket.af_inet,socket. SOCK_STREAM)
S.setsockopt (socket. Sol_socket,socket. so_reuseaddr,1)
S.bind ((Host,port))
S.listen (1)
Print "Server is running on port%d; Press CTRL-C to terminate. " %port
While 1:
CLIENTSOCK,CLIENTADDR = S.accept ()
Clientfile = Clientsock.makefile (' rw ', 0)
Clientfile.write ("Welcome," + str (clientaddr) + ' \ n ')
Clientfile.write (' Please enter a string: ')
line = Clientfile.readline (). Strip ()
Clientfile.write (' entered%d characters.\n '%len (line))
Clientfile.close ()
Clientsock.close ()
###### Results ######
Welcome, (' 127.0.0.1 ', 62460)
Please enter a string:123
You entered 3 characters.
The connection to the host is lost.
C:\users\administrator>
Python Network Programming 01