some issue about python socket -
I write an example program about dragon socket, but there was some question when executing the example program.
Please review the following code and tell me why this problem happened? Thanks!
Issue details: The server socket was blocked, and just print the following information:
Listen to tcpServer: 127.0.0.1:9003 ('127.0.0.1', 9003) 127.0.0.1:60541 Connect The customer socket was still blocked, and did not print any information
Code: Server-side socket: < / Strong> import socket HOST = '127.0.0.1' port = 9003 buffer = 4096 SOK = socket socket (socket.AFNAIAT, socket.SOCKRRM) sock.bind (HOST, PORT) ) Sock.listen (0) print ('tcpServer Listen:% s:% s \ n \ r'% (HOST, PORT)) print sock Getockname () while true: client_sock, client_addr = sock accept () print ('% s:% s connect'% client_addr) rf = client_sock.makefile ('rb', 4) wf = client_sock.makefile ('wb' , 4) print rf.readlines () rf.close () Client_sock.close () wf.write ("from server") wf.flush () wf.close () sock.close () and client-side socket code: import socket HOST = '127.0.0.1' port = 9003 buffer = 4096 sock = socket socket (socket. AF_I Net, Socket SOCK_STREAM) sock.connect (HOST, PORT) rf = sock.makefile ('rb', 4) wf = sock.makefile ('wb', 4) wf.write ('hello, tcpServer!') Wf.flush () Wf.close () recv = rf.readline () rf.close () print ('[TCP server has said]:% s'% recv) sock.close () < / Div>
The server depends on getting the file signal at the end, but you never send one. Try calling your customer:
... wf.write ('hello, tp server!') Wf.close () sock.shutdown (socket.SHUT_WR) ...
Comments
Post a Comment