zoukankan      html  css  js  c++  java
  • python socket: sending and receiving 16 bytes Stack Overflow

    python socket: sending and receiving 16 bytes - Stack Overflow

        python socket: sending and receiving 16 bytes
        up vote 3 down vote favorite
        3
           

        See edits below.

        I have two programs that communicate through sockets. I'm trying to send a block of data from one to the other. This has been working with some test data, but is failing with others.

        s.sendall('%16d' % len(data))
        s.sendall(data)
        print(len(data))

        sends to

        size = int(s.recv(16))
        recvd = ''
        while size > len(recvd):
            data = s.recv(1024)
            if not data:
                break
            recvd += data
        print(size, len(recvd))

        At one end:

        s = socket.socket()
        s.connect((server_ip, port))

        and the other:

        c = socket.socket()
        c.bind(('', port))
        c.listen(1)
        s,a = c.accept()

        In my latest test, I sent a 7973903 byte block and the receiver reports size as 7973930.

        Why is the data block received off by 27 bytes?

        Any other issues?

        Python 2.7 or 2.5.4 if that matters.

        EDIT: Aha - I'm probably reading past the end of the send buffer. If remaining bytes is less than 1024, I should only read the number of remaining bytes. Is there a standard technique for this sort of data transfer? I have the feeling I'm reinventing the wheel.

        EDIT2: I'm screwing up by reading the next file in the series. I'm sending file1 and the last block is 997 bytes. Then I send file2, so the recv(1024) at the end of file1 reads the first 27 bytes of file2.

        I'll start another question on how to do this better.

        Thanks everyone. Asking and reading comments helped me focus.
  • 相关阅读:
    帮助C#菜鸟进入SQL/XML开发
    汉字转换为拼音的函数
    水晶报表的使用技巧
    用DataSet操作XML
    frame,iframe,frameset 的区别(来源网络)
    oracle 数据库锁表解决方法
    c#日期类型的使用 (转)
    深入了解ViewState 深入了解ViewState
    js中top、parent、frame
    SQL中 inner join、 left join 、right join、 outer join之间的区别(来自百度自用)
  • 原文地址:https://www.cnblogs.com/lexus/p/2847020.html
Copyright © 2011-2022 走看看