Socket flushing

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • amitk12
    New Member
    • Apr 2008
    • 1

    Socket flushing

    I am new to python, and I am trying to send a message over a TCP socket to another component (client). I have to send the message in chunks of 1024 bytes in response to client REQ.

    The problem is that when first chunk of 1024 bytes is received by the client, it contains a character string appended at the end from the Initial Message that the client had sent as REQ.

    What can I do to flush the socket connection before delivering the messsage

    my setsockopt(sock et.SOL_SOCKET, socket.SO_REUSE ADDR,1)

    Thanks

    Amit
  • Subsciber123
    New Member
    • Nov 2006
    • 87

    #2
    I'm not really sure what your problem is here, because I have never had it. If you post code, that would be most helpful. Anyway, for sockets, I would do something like this:
    [CODE=python]import socket

    BUFF_SIZE=1024
    REQUEST_SIZE=10 24

    class my_sock(socket. socket):
    def __init__(self,* args,**kwds):
    self.buffer=""
    socket.socket._ _init__(self,*a rgs,**kwds)
    def send(self,data) :
    self.buffer+=da ta
    while BUFF_SIZE<len(s elf.buffer): # I am biased against > symbols
    socket.socket.s end(self,self.b uffer[:BUFF_SIZE])
    self.buffer=sel f.buffer[BUFF_SIZE:]
    def flush(self):
    self.buffer+=(B UFFER_SIZE-len(self.buffer )%BUFFER_SIZE)* "\x00"
    self.send("")

    sock=my_sock()
    sock.bind(("",8 0)) # (address, port)
    sock.listen(1)
    while True:
    s=sock.accept()
    try:
    data=s.recv(REQ UEST_SIZE)
    if data.startswith ("This is an evil request for spam and eggs."):
    s.send("No, it isn't. Spam isn't on the menu. Unless, of course, you get it with spam, spam, spam, spam, spam, spam, baked beans, spam, spam, spam, and spam.")
    s.flush()
    finally: # just in case something goes wrong, don't leave the socket open
    s.close()
    [/CODE]
    I haven't tested it, so I can't guarantee that it works, but that should be a start.

    Comment

    Working...