Python socket send EOF

后端 未结 3 1807
滥情空心
滥情空心 2021-02-05 14:58

I have a simple file transfer socket program where one socket sends file data and another socket receives the data and writes to a file

I need to send an acknowledgme

3条回答
  •  情歌与酒
    2021-02-05 15:26

    Design a protocol (an agreement between client and server) on how to send messages. One simple way is "the first byte is the length of the message, followed by the message". Rough example:

    Client

    Python 2.6.5 (r265:79096, Mar 19 2010, 21:48:26) [MSC v.1500 32 bit (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> from socket import *
    >>> s=socket()
    >>> s.connect(('localhost',5000))
    >>> f=s.makefile()
    >>> f.write('\x04abcd')
    >>> f.flush()
    

    Server

    Python 2.6.5 (r265:79096, Mar 19 2010, 21:48:26) [MSC v.1500 32 bit (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> from socket import *
    >>> s=socket()
    >>> s.bind(('localhost',5000))
    >>> s.listen(1)
    >>> c,a=s.accept()
    >>> f=c.makefile()
    >>> length=ord(f.read(1))
    >>> f.read(length)
    'abcd'
    

提交回复
热议问题