Socket local emulation server + client (ii)

Source: Internet
Author: User

Set up two py files, open two CMD interface, and then communicate with each other. The server side uses the multi-process, continuously processing the data received from the client, and the client sends the data to the client through a list.

(Each connection must create a new thread (or process) to handle, otherwise, a single thread cannot accept connections from other clients during the process of processing the connection.) In this case, the multi-process does not actually play a role.

The code and explanations are as follows:

#socket server-side programsImportSocketImport Time fromMultiprocessingImportProcess#multi-process and multithreading-like usagedefTcplink (SOCK1,ADDR1):#Create a new function to receive information from the client    Print('Accept New Connection ...') Sock1.send (b'welcom')#If the connection builds results, the server sends a welcome to the client     while1:#the server continues to receive data sent by the client until the server receives exit, and the exit is not receiving data .data = SOCK1.RECV (1024)#Receive DataTime.sleep (1)#delay 1s        " "data is sent to the server by the client, and the server sends Hello,%data to the client, all to Utf-8 encoding ." "Sock1.send (('hello,%s'%data.decode ('Utf-8'). Encode ('Utf-8'))        if  notDataorData.decode ('Utf-8') =='Exit':             Breaksock1.close ()#Connection Close    Print('Connection form%s is close ....'%addr1)if __name__=="__main__": S= Socket.socket (socket.af_inet, socket. SOCK_STREAM)#set up a socket    " "The listener port 127.0.0.1 indicates that both the client and the server must be native to run. We want to bind the listening address and port. The server may have more than one network card, can be bound to the IP address of a network card, you can also bind to all network addresses with 0.0.0.0, you can also use 127.0.0.1 to bind to the native address. 127.0.0.1 is a special IP address that represents the native address, and if bound to this address, the client must be running at the same time in order to connect, that is, the external computer cannot connect. The port number needs to be specified beforehand. Because we write this service is not standard service, so with 9999 this port number. Note that a port number less than 1024 must have administrator privileges to bind:" "S.bind (('127.0.0.1', 9999)) S.listen (1)#maximum number of simultaneous receive connections    Print('waiting for connecting ...')     while1:#the information sent by the server through a loop that is received by the client .SOCK,ADDR = S.accept ()#s.accept returns two datat = Process (target=tcplink,args= (SOCK,ADDR))#MultithreadingT.start ()#Turn on multithreading
#Socket client program#with chenhang001.pyImportSockets= Socket.socket (socket.af_inet,socket. SOCK_STREAM)#Create a new socketS.connect (('127.0.0.1', 9999))#establish the connection, why is the double parenthesisPrint(S.recv (1024x768). Decode ('Utf-8'))#print the Received data#send data, define a list, fordata111inch[b'Chenhang'B'Wangyadan'B'Wuhankejidaxue'B'Wuchangfenxiao']: S.send (data111)Print(S.recv (1024x768). Decode ('Utf-8'))#For A in range:#S.send (b (a))S.send (b'Exit')#send exit character, server quits receiving data

Socket local emulation server + client (ii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.