On Wed, Apr 17, 2019 at 3:20 AM Amir Farhang <[email protected]>
wrote:

> i am trying to make a server and client which sends a file from client to
> server and the server saves it to hard then the server asks for another
> file and if the answer of client is yes then the client sends the second
> file then the server again saves it and if the client answer is no server
> close the socket when i run this code the first file is sent
> and received successfully but after that both of the server and the client
> freeze and nothing happens what is wrong with it and how can i fix it?
>
> my server code:
>
> import socket
>
> host = 'localhost'
>
> port = 4444
>
> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>
> s.bind((host, port))
>
> s.listen(5)
> (client, (ip, port))=s.accept()
>
> while True:
>
>     data = "".join(iter(lambda: client.recv(1), "\n"))
> with open('filehere.txt', 'w') as file:
>
>
>
>     for item in data:
>
>         file.write("%s" % item)
>
>     if not data: break
>
>
> client.send("is there any other file?")
>
> d = client.recv(2048)
> if d == "yes":
>
>     while True:
>
>         data = "".join(iter(lambda: client.recv(1), "\n")
>
>         with open('filehere1.txt', 'w') as file:
>
>              for item in data:
>
>                  file.write("%s" % item)
>
>         if not data: break
>
>         s.close()
> else:
>
>     s.close()
>
> my client code:
>
> import socket
>
> host = 'locahost'
>
> port = 4444
>
> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>
> s.connect((host, port))
>
>
> f = open('myfile.txt', 'rb')
>
> l = f.read()
> while True:
>
>     for line in l:
>
>         s.send(line)
>
>     break
>
> f.close()
>
> d = s.recv(2048)
>
> a = raw_input(d)
>
> if a == "yes":
>
>     s.send("yes")
>
>     f = open('myfile1', 'rb')
>
>     l = f.read()
>
>     while True:
>
>         for line in l:
>
>             s.send(line)
>         break
>
>     f.close()
> else:
>
>     s.close
>
>
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 [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/python_inside_maya/344e3eb1-b169-4916-80c6-3be93cea39c2%40googlegroups.com
> <https://groups.google.com/d/msgid/python_inside_maya/344e3eb1-b169-4916-80c6-3be93cea39c2%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA3iVMd55jwzyY4e2phcGbbfbcoaS9Xhux%2BM2CaP-HK_MA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to