0

s is a standard tcp socket,this is the server:

s = socket.socket()
s.connect(("127.0.0.1", 5203))
with open("example.txt" "wb+") as f:
exqwrt = f.read(1024)
while (exqwrt):
    s.send(exqwrt)
    exqwrt = f.read(1024)

code on client is, s is a standard tcp socket as well:

s = socket.socket()
s.bind(("127.0.0.1", 5203))
s.listen(5)
conn, addr = s.accept()
openedFile = open("examplet.txt", "wb+")
while True:
    exwqrt = conn.recv(1024)
    openedFile.write(exqwrt)
    if not exqwrt:
        break

The problem is that file gets sent pretty fast.But it is written on examplet.txt pretty slowly in like 5 minutes.

jianu81
  • 105
  • 1
  • 2
  • 7
  • You're not flushing the contents to the file so I assume it could be stuck in the buffer. – roganjosh Aug 31 '18 at 17:47
  • Try closing the 3 sockets when you are done with them. Does that make it faster? – tdelaney Aug 31 '18 at 17:51
  • @roganjosh I don't know what that means – jianu81 Aug 31 '18 at 17:55
  • @tdelaney will try it – jianu81 Aug 31 '18 at 17:55
  • @jianu81 data isn't written instantly to a file that you haven't closed. It's buffered up and written in blocks. Since you don't call `.close()` on the file, it's possible that the data is being queued for writing and it takes 5 minutes for enough data to be received to actually do a write to the file. – roganjosh Aug 31 '18 at 17:57
  • I ran your script successfully. It had several errors hinting that the failing script is something different. Have you run this code exactly and does it fail? – tdelaney Aug 31 '18 at 18:00
  • @roganjosh you're a lifesaver! – jianu81 Aug 31 '18 at 18:33
  • 2
    Possible duplicate of [How often does python flush to a file?](https://stackoverflow.com/questions/3167494/how-often-does-python-flush-to-a-file) – roganjosh Aug 31 '18 at 18:50
  • 1
    First note that duplicates are not bad things. I had to take a stab on my initial answer. I think that link includes all the info needed to answer this. It's not necessarily closing the file to flush the buffer, which might not be ideal for a client; the other ways are covered there. – roganjosh Aug 31 '18 at 18:51

0 Answers0