socket + file i/o question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John

    #1

    socket + file i/o question

    I am sending a file on a tcp socket using the following code


    while 1:
    buf = os.read(fd, 4096)
    if not buf: break
    print total, len(buf)
    conn.send(buf)

    The recieving code looks like

    while 1:
    if recvbytes == filesize:
    print 'Transfer done. Size = %d' % recvbytes
    break
    buf = s.recv(4096)
    if not buf:
    print 'EOF received'
    raise Exception()
    print recvbytes, len(buf)
    os.write(fd, buf)
    recvbytes = recvbytes + len(buf)


    My problem is that the first time the client uploads a file
    to the server, the code works. But then when the server wants
    to download a file to the client, the same code breaks down!

    The problem seems to be the socket. The send part sends exactly the
    amount of data it reads from the file. But the recv part gets one
    more byte than the size of the file?? It seems to me that this
    extra one byte is coming inside the send/recv calls. Anyone has
    any comments on where this extra one byte is coming from?

    Thanks a lot for your help,
    --j

  • John

    #2
    Re: socket + file i/o question

    Here is what the send and recieved number of bytes show up as:

    Filesize being sent = 507450
    Server sending file to client...
    (total size sent , buffer size)
    ....
    491520 4096
    495616 4096
    499712 4096
    503808 3642
    ../server.py: (107, 'Transport endpoint is not connected')

    On the client side, the bytes recieved shows one extra byte!
    (Bytes recieved, buffer size)
    ....
    504256 1400
    504256 1400
    505656 1400
    505656 1400
    507056 1400
    507056 395
    507451 395
    EOF received
    ../client.py: An unknown error occurred.

    Note that on the client there was an extra byte??
    507451 ??

    Hope this helps in explaining my question better,
    --j

    Comment

    • John

      #3
      Re: socket + file i/o question

      I found the problem. There was a recv that was not from an open
      socket...

      Sorry abt the trouble,
      --j

      Comment

      Working...