472,993 Members | 3,137 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,993 software developers and data experts.

One more socket programming question

I'm now experimenting with the SocketServer class. Originally I
subclassed the StreamRequestHandler to make my own custom handler, but a
result of this seems to be that the client socket closes after it has
been used, instead of staying open.

Just as a test, I decided to use BaseRequestHandler instead, because I
know its methods aren't implemented. So this is what I have:

-----
import SocketServer

host = ''
port = 51234
address = (host, port)
buffer_size = 1024

class MyRequestHandler(SocketServer.BaseRequestHandler):
def handle(self):
print '...connected from:', self.client_address
data = self.request.recv(buffer_size)
self.request.send('%s %s' % ('You typed:', data))

socket_server = SocketServer.TCPServer(address, MyRequestHandler)
print 'waiting for connection...'
socket_server.serve_forever()
------

------
from socket import *

host = 'localhost'
port = 51234
address = (host, port)
buffer_size = 1024

client_socket = socket(AF_INET, SOCK_STREAM)
client_socket.connect(address)

while True:
data = raw_input('')
if not data:
break
client_socket.send(data)
data = client_socket.recv(buffer_size)
print data

client_socket.close()
------

But this only seems to work one time, and then subsequent attempts
return nothing, and then the client program seems to crash (or just
close on its own).

What's happening here?
Jun 27 '08 #1
2 990
John Salerno <jo******@gmailNOSPAM.comwrote:
>
I'm now experimenting with the SocketServer class. Originally I
subclassed the StreamRequestHandler to make my own custom handler, but a
result of this seems to be that the client socket closes after it has
been used, instead of staying open.
Right. "handle" is not called for one REQUEST at a time, it's called for
one CONNECTION at a time. If you need a connection to be persistent, then
your handle() function needs to sit in a loop making recv calls until you
detect that the conversation is complete.
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Jun 27 '08 #2
"Tim Roberts" <ti**@probo.comwrote in message
news:sj********************************@4ax.com...
John Salerno <jo******@gmailNOSPAM.comwrote:
>>
I'm now experimenting with the SocketServer class. Originally I
subclassed the StreamRequestHandler to make my own custom handler, but a
result of this seems to be that the client socket closes after it has
been used, instead of staying open.

Right. "handle" is not called for one REQUEST at a time, it's called for
one CONNECTION at a time. If you need a connection to be persistent, then
your handle() function needs to sit in a loop making recv calls until you
detect that the conversation is complete.
Ah, so I need to rewrite my handle method. I was thinking that the
specialized setup() and/or finish() calls from StreamRequestHandler were the
reason that the connection was closed after one use (which is why I tried
BaseRequestHandler instead).

Thanks.
Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jean-Philippe Guyon | last post by:
Hello, I am trying to compile a class that uses socket using the Visual C++ ..NET compiler. I get the following error: ------ Build started: Project: infCommon, Configuration: Release Win32...
2
by: dream machine | last post by:
Hi all , with BegeinReceive I can build async method of Socket Class that Receive the data from the Socket Client . My question is , if I have this code that create 3 Receive Async Call : ...
1
by: John Sheppard | last post by:
Thanks to everyone that responded to my previous Socket Programming question. Now I have run into some behavior that I don't quite understand. Programming environment. VS.NET 2003, C#, Windows...
5
by: mscirri | last post by:
The code below is what I am using to asynchronously get data from a PocketPC device. The data comes in fine in blocks of 1024 bytes but even when I send no data from the PocketPC constant blocks of...
5
by: Justin Creasy | last post by:
If this is the wrong group for this posting please let me know and I'll move it. I have an application that has an ArrayList of sockets to clients. For standard one-to-one messages my...
2
by: djc | last post by:
I read a network programming book (based on framework 1.1) which indicated that you should 'never' use the RecieveTimeout or the SendTimeout 'socket options' on TCP sockets or you may loose data. I...
14
by: =?Utf-8?B?TWlrZVo=?= | last post by:
I have a sync socket application. The client is blocked with Socket.Receive(...) in a thread, another thread calls Socket.Close(). This unblock the blocked thread. But the socket server is still...
3
by: Stuart | last post by:
I am in the process of teaching myself socket programming. I am "playing around" with some simple echo server-client programs for m the book TCP/IP Sockets in C. The Server program is: ...
1
by: =?Utf-8?B?UmFqbmk=?= | last post by:
Dear Sir/Mam, I have written a server code using the Windows Socket API's. Wherein I have created the socket and bound it to a particular IP address and port number. Later I have made the socket...
4
by: danomano | last post by:
The code below works fine. As of now I just set the buffer length real big and all is good. However, I hate the impreciseness of that. So my question in general is how do I know what length to...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

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.