There are a few different problems with this design, but I will focus first on addressing your specific question as to why both the server and client hang after the first file transfer. Looking at the way you are reading from the client socket, you iterate on bytes until the first newline character and then write that to the file. That might function as expected if your source file has only one line. But if your client is sending a file with 2+ lines, your server will move on to a after the first file line to call send() to ask a question, while your client is still trying to send the rest of its first file. Now both the client and server are blocked on send operations and no one is reading. There is also a bit of an issue with the "yes" reply in one case, but no "no" reply to close the server in the other.
In general there are problems with receiving 1 byte at a time, looking for a newline char. Its very inefficient, especially if your file is large. You also have no idea when the file is done if it contains multiple lines, which is exactly what your client is sending (iterating on lines and sending). Also, it can further be quite inefficient to read the entire file into memory and then write it back out to a file, while you could just be looping over, say, 1024 bytes at a time, and writing each chunk to a file.
Low-level socket communication requires adherence to some form of an establish protocol: you say something a certain amount of times until a certain condition, and I say something a certain amount of times. And everything must be well defined, otherwise you end up in situations where both sides aren't in agreement about who is transmitting or receiving at a given moment. RPC frameworks (and HTTP) layer on top of raw sockets to provide you with a protocol for the communication. Some calls will be defined as one-way (no reply) and other may be request-reply. And this enforces the communication order.
What I recommend is to look at some existing online examples of sending files using raw sockets in Python. You might want to approach this using structured messages, so that you can more easily know when a file is really complete. Imagine your server just being in an endless while loop, reading messages with a defined structure and doing something different depending on the type of each:
{"type":"echo", "msg":"Say hello back"}
# server echos msg back to client and client expects to recv
For file transfer, some protocols do this by first sending a header saying something like
{"type":"file", "size":5000, "name":"foo.txt"}
# server knows to read chunks (1024 or 2048, ...) until it has collected "size"
Those are just some possible examples. I am certain you will find complete usable examples online.
--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/344e3eb1-b169-4916-80c6-3be93cea39c2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.