Advanced Programming tcp, udp

1. The four layers: Application Layer tcp / ip transport network link layer 
2. seven: session, presentation, application, transport, network, data link, physical layer.
Theoretically layer 7 is, in fact, four
3-port, is to distinguish between processes. The only distinguishing mark process, pid is the process on a computer, on more than one computer port is
4.80 port is http, 21 ports are on to the ftp, 0-25535, well-known
1024-65535 dynamic port
5. command, netstat -an

four .ip address

five .socket
tcp slow, do not lose data
tcp fast, easy to lose data

udp binding
simplex: receive only
half-duplex: intercom
full duplex: phone

retransmission data
udpSocket.sendto ( 'Hello' .encode ( "utf-8") , ( '192.168.0.107', 8080))

the received data
can receive a maximum of 1024 bytes, two data foregoing assignment is received on behalf of the two values, the need to assign the front 2 variables
a, B = udpSocket.recvfrom (1024)

# binding information, only bind native, sender does not need to bind, can be random, the recipient needs to bind, ip address may be empty
udpSocket. bind (( "", 8080) )

operation:
write a program, a send and receive data. Unlimited accept the
East Columbia quotations, ask yourself questions

received data packets in return gives go get empty
echo empty bag

Research globle global variable reference

work 2. Analog QQ chat program, multi-threaded test

* Import socket from 
# to create a socket
UDPSocket = socket (AF_INET, SOCK_DGRAM)
# binding information, can only bind the local, the sender does not need to bind, can be random, the recipient needs to bind
udpSocket.bind ( ( "", 8080))

# #ip determine who the recipient address is
# udpSocket.sendto (B "haha", ( '192.168.0.107', 8080))
# sending Chinese, add encode (after the character string "utf- . 8 ")
# udpSocket.sendto ( 'Hello' .encode (" utf-8 " ), ( '192.168.0.107', 8080))
udpSocket.sendto ( 'Hello' .encode (" utf-8 " ), ( '192.168.0.107', 8080))

# udp using transmission data information once a connection is established for each hair, ip and port number
# using sockets, each port number is not the same, can not be used to python3 string, only byte type
# udpSocket.sendto (B "haha", ( '192.168.119.115', 8080))

# closed, up to close to 1024 bytes, two data assignment in front, is representative of the received two values, the assignment requires two foregoing variables
A, B = udpSocket.recvfrom (1024)
Print (a.decode ())

from socket import *

def main():
udpSocket = socket(AF_INET, SOCK_DGRAM)

udpSocket.bind(("", 6789))

#收,打印
while True:
recvInfor = udpSocket.recvfrom(1024)
print("[%s]:%s"%(str(recvInfor[1]), recvInfor[0].decode("gb2312")))


if __name__ == "__main__":
main()




Guess you like

Origin www.cnblogs.com/wangjunxi/p/11832208.html