udp socket usage of concurrency and parallelism

1.udp agreement

  Is defined: a User Datagram Protocol UDP, osi reference model is a connectionless transport-layer protocol, does not provide a simple and reliable information transfer service
  purposes: not required for reliability, packet order is not required, a small amount of data transmitted each time transmission, and strive to fast

2. udp communication process

  It is very similar to the walkie-talkie: just send regardless of whether the other received do not even care about each other is not.

          Buy pagers              

          Fixed-speaking channel

          (Saving listen and accept)

          Received signal

          Send signal

          Intercom and buy

          (Province connect)

          Send the specified frequency

          send Message

          Receive information

  It differs from the tcp: udp because unreliable transport, without establishing a connection, not stick package, a single data packet can not be too  

  Note: udp socket have to manually specify the

# ================================================= ================ client
from socket import  *

client = socket(AF_INET,SOCK_DGRAM)
client.sendto("%Y-%m-%d".encode("utf-8"),("127.0.0.1",1688))
data,addr = client.recvfrom(1472)
print(data)
# ================================================= ================ server
from socket import  *
import time

server = socket(AF_INET,SOCK_DGRAM)
server.bind(("127.0.0.1",1688))

while True:
data,addr = server.recvfrom(1472)
res = time.strftime(data.decode("utf-8"),time.localtime())
server.sendto(res.encode("utf-8"),addr
 
#模拟聊天
===================================================================服务器 from socket import * server = socket(AF_INET,SOCK_DGRAM) server.bind(("192.168.13.93",1688)) clients = {} while True: try: data,addr = server.recvfrom(1472) if addr[0] not in clients: clients[addr[0]] = addr try: print(" % Said S:% S " % (addr [ 0 ], data.decode ( " UTF-. 8 " ))) the except: Print ( " encoding problem ah ..... " ) # forwarding messages through all clients to them for k, v in clients.items (): server.sendto (the Data, v) the except Exception AS E: # Print (E) Pass
#=================================================================客户端1
from
socket import * client = socket(AF_INET,SOCK_DGRAM) while True: # msg = input("msg:").strip() client.sendto("client2".encode("utf-8"),("127.0.0.1",1688)) data,addr = client.recvfrom(1472) print(data)
#=================================================================客户端2
from
socket import * client = socket(AF_INET,SOCK_DGRAM) while True: # msg = input("msg:").strip() client.sendto("client1".encode("utf-8"),("192.168.13.93",1688)) data,addr = client.recvfrom(1472) print(data)

 

 

 

4.DNS server:

  Definition: Domain Name Server, a server-side cs structure, essentially a database that stores the correspondence between the domain name and ip

  Purpose: the domain name into an ip address to link server, you must know the ip

  Origin: Due to individual ip hard to remember, so we put ip and domain bound to a

  dns using Udp agreement, because of the small data transmission, but a high-speed requirements dns server to service many computer

The concept of multi-process

  Process: refers to a program is running, it comes from the operating system, no operating system will not process

  Operating System: is a set of software for controlling the hardware, hiding the ugly details of the hardware, the hardware disorderly competition and become the order

      The difference between ordinary operating system software is the operating system are protected by hardware, the user can not be modified, he is a large, long-lived program: And you do not have a common software can write your own or for a

  Multi-channel technology: In order to improve the utilization of the computer, with it you can handle multiple tasks at the same time

  Spatial multiplexing: the memory is divided into different regions, each charged with a different program

  Time multiplexing: When a program is executed io operation, can be switched to execute another program, but have to save the state before the switching, in order to resume execution. A process takes too long will cpu switch, or by the operating system away execute permission of cpu

  NOTE: Not necessarily improve multi-channel efficiency, eg if multiple tasks are pure calculation, then the switch but will reduce the efficiency of only encounter in order to improve operational efficiency while io

 

 

 

 

Guess you like

Origin www.cnblogs.com/wyf20190411-/p/10951628.html