python - Why client socket connection is not closed after all data received from server? -


i'm learning socket programming in python,

server code:

import socket  srvsock = socket.socket(socket.af_inet, socket.sock_stream) srvsock.bind(('', 23000)) srvsock.listen(5)  while true:     clisock, (rem_host, rem_port) = srvsock.accept()     print "conection established host %s , port %s" % (rem_host, rem_port)     while true:         strg = clisock.recv(20)         if not strg:             print 'conection closed'             clisock.close()             break         clisock.send(strg) 

client code:

import socket  clisock = socket.socket(socket.af_inet, socket.sock_stream) clisock.connect(('', 23000)) clisock.send("hello world rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr dsadsadsa tttttt\n") while true:     data = clisock.recv(20)     print type(data)     if not data:         clisock.close()         break     print data 

i'm sending data stream client server , @ same time receiving data server, after successful data transmission, server not closing client connection. did miss thing ?

the issue caused because server keeps reading data client until reads no data. happens when connected client closes connection. until then, server socket block (i.e. temporarily suspect operations) until client sends more data.

bottom line: either client or server has indicate no longer intends send data on connection.

you can fix client adding line

clisock.shutdown(socket.shut_wr) 

before for loop in client. indicates no more data sent.


Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -