473,498 Members | 1,648 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Socket flushing

1 New Member
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(socket.SOL_SOCKET, socket.SO_REUSEADDR,1)

Thanks

Amit
Apr 3 '08 #1
1 37150
Subsciber123
87 New Member
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:
Expand|Select|Wrap|Line Numbers
  1. import socket
  2.  
  3. BUFF_SIZE=1024
  4. REQUEST_SIZE=1024
  5.  
  6. class my_sock(socket.socket):
  7.     def __init__(self,*args,**kwds):
  8.         self.buffer=""
  9.         socket.socket.__init__(self,*args,**kwds)
  10.     def send(self,data):
  11.         self.buffer+=data
  12.         while BUFF_SIZE<len(self.buffer): # I am biased against > symbols
  13.             socket.socket.send(self,self.buffer[:BUFF_SIZE])
  14.             self.buffer=self.buffer[BUFF_SIZE:]
  15.     def flush(self):
  16.         self.buffer+=(BUFFER_SIZE-len(self.buffer)%BUFFER_SIZE)*"\x00"
  17.         self.send("")
  18.  
  19. sock=my_sock()
  20. sock.bind(("",80)) # (address, port)
  21. sock.listen(1)
  22. while True:
  23.     s=sock.accept()
  24.     try:
  25.         data=s.recv(REQUEST_SIZE)
  26.         if data.startswith("This is an evil request for spam and eggs."):
  27.             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.")
  28.             s.flush()
  29.     finally: # just in case something goes wrong, don't leave the socket open
  30.         s.close()
  31.  
I haven't tested it, so I can't guarantee that it works, but that should be a start.
Apr 11 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
1680
by: John Abel | last post by:
I'm hoping this is something simple, and someone can point me in the right direction here. I have a class based on SocketServer (ThreadingTCPServer), and I've used makefile on the socket so I use...
1
2500
by: Manfred | last post by:
Hello, I am working on an interactive server/client setup, not using INET::Sockets. Everything is gravy except for a little leftover stuff being shown. I type a command w/ args, CLIENT join()s the...
4
41752
by: Stephan Steiner | last post by:
Hi I have a networking application that periodically needs to go into sleep mode (from an application point of view, I'm simply suspending the receiver thread until it's time to start listening...
2
1635
by: O.J. Tibi | last post by:
Hi all, I suspect I'm having problems with socket functions within my PHP application running on Red Hat 2.4.21-4.EL #1/PHP 4.3.2/Apache 2.0.46. Below is a sample code listing: ...
3
4259
by: dansolo | last post by:
Hi to all, I would like to flush the datas in a tcp socket connection, however in the package socket I haven't found any function to do this work. I was thinking of opening and closing the socket...
1
2948
by: Chris | last post by:
Hi, I am using PHP persistent sockets and have a problem with them. The problem is that PHP only sends the data when the socket is closed. In the code example below the counterparty process...
3
9691
by: madsornomads | last post by:
Hi all, I have a problem with reading from a Java server after I have written to it - it just hangs. It works fine if I just write to the server and not try to write. I have read the HOWTO on...
1
1825
by: zbrozlo | last post by:
Hi, I have strange problem with my perl program. My program consists of two parts, first written in python and second in perl. Perl part is a server, but also uses two sockets for "talking" with gui...
4
5480
by: The Doctor | last post by:
Hey people, I have two applications: the server, which creates a server socket, waits for a real-time signal, and if it receives one, it creates a client socket. The client, which will...
0
7126
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7168
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7210
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6891
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
4595
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3096
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3087
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
659
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
293
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.