473,804 Members | 3,375 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

threads and sockets

hi!

how can i stop a server socket running in a thread other than the main
thread? if the server socket was a local variable of the function started
by the child thread, would calling join work?

thanks


----------------------------------------------------------------
This message was sent using IMP, the Internet Messaging Program.
Jul 18 '05 #1
3 1714
Ajay wrote:
how can i stop a server socket running in a thread other than the main
thread? if the server socket was a local variable of the function started
by the child thread, would calling join work?


Short answer: you can't, and join won't help. Use select on sockets, with a
timeout, or use twisted.
--
Regards,

Diez B. Roggisch
Jul 18 '05 #2
how can i stop a server socket running in a thread other than the main
thread? if the server socket was a local variable of the function started
by the child thread, would calling join work?


Short answer: you can't, and join won't help. Use select on sockets, with a
timeout, or use twisted.


More specifically:

def serverthread(so ck):
while not quit:
try:
select(sock, sock, sock, 1)
except:
#handle errors, but don't use a bare except in real code
#close sock if necessary

- Josiah

Jul 18 '05 #3
Ajay <ab******@mail. usyd.edu.au> wrote in message news:<ma******* *************** *************** *@python.org>.. .
hi!

how can i stop a server socket running in a thread other than the main
thread? if the server socket was a local variable of the function started
by the child thread, would calling join work?


Here is how you do what this:

import socket, threading

class MyThread(thread ing.Thread):
def __init__(self):
threading.Threa d.__init__(self )
HOST = ''
PORT = 50007
self.s = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
#! blocking operatioms on this socket (accept) will timeout
self.s.settimeo ut(1)
self.s.bind((HO ST, PORT))
self.s.listen(5 )
self._stop = False
def stop(self):
self._stop = True
def run(self):
while 1:
try:
if self._stop:
print "stop requested"
break
conn, addr = self.s.accept()
print "connection from", addr
except socket.timeout:
print "timeout"
except socket.error:
print "socket.err or"
break
self.s.close()

def TimerFucn(t):
print "TimerFucn"
t.stop()
# you can do t.close(). accept() will throw an exception
# in thos case all the stuff with stop() is not needed

listener = MyThread()
t = threading.Timer (10, TimerFucn, (listener,))
t.start()
listener.start( )

while 1:
s = raw_input()
if s[0] == 'q':
break
Jul 18 '05 #4

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

Similar topics

4
7550
by: Julia Goolia | last post by:
hello, i read that it is bad to use threads with tkinter. so my question is how does one create a gui program with sockets? at one point you have to call mainloop() which does not return. then you are not free to do stuff with the sockets. what's up with that? thanks so much for your time!
0
1451
by: Gonçalo Rodrigues | last post by:
Hi, I have a problem with threads and sockets. I'll try to describe the problem in words with pseudo-code. I've been working on a few classes to make it easier to work with threads. This framework, let us call it that, consists in two parts. The first part is just a basic thread class deriving from threading.Thread with a few extra functionality that makes it easier for a thread to spawn a new thread and "father it". Each of its
6
2937
by: Zunbeltz Izaola | last post by:
Hi, I have the following problem. I'm developing a GUI program (wxPython). This program has to comunicate (TCP) whit other program that controls a laboratory machine to do a measurement. I have a dialog box, wiht two buttoms "Start measurement" and "Stop". "Start" executes a function that do the measurement in the following way.
5
4233
by: Russell Warren | last post by:
Does anyone know the scope of the socket.setdefaulttimeout call? Is it a cross-process/system setting or does it stay local in the application in which it is called? I've been testing this and it seems to stay in the application scope, but the paranoid side of me thinks I may be missing something... any confirmation would be helpful.
2
2362
by: Stressed Out Developer | last post by:
We have an application that has a 200 count loop that does the following: ' Each time thru the loop we pass the next IP Address is a range (aka 192.168.4.50 thru 192.168.4.254) Try If mUIO_Threads(i) Is Nothing Then mUIO_Threads(i) = New System.Threading.Thread(AddressOf mUIO_DAQ(i).InitDAQ) mUIO_Threads(i).Name = mUIO_DAQ(i).UIO_IPAddr mUIO_Threads(i).IsBackground = False
2
2187
by: Ronodev.Sen | last post by:
the way my program needs to go is -- 1) open a socket and listen on it 2) moment a client connects to the socket - process some data (by sending it to another machine), get the result and send it back to the SAME socket - the data isnt a large value (measured in bytes rather than MB or GB) i TRIED thinking of this in the Asynchronous way - BeginReceive - then pass to the OnClient Connected handler, which calls a Wait For Data
4
2144
by: nyhetsgrupper | last post by:
Hi, I've written a async server app. This app start by connecting to a client and then send some data (BeginSend). When the data is sent, the server is starting to listen for incomming data. (BeginRecieve). In the receive callback I always call BeginRecieve again to keep listen for more data. The server is continualy listening, and from time to time sending (but if it is sending more data when there is already a listening socket, I don't...
6
6454
by: roblugt | last post by:
I have what I imagine is a well-known .Net networking problem, but even though I've Googled for some time I've not yet come across a thread where this has been fully explained... There is a fairly common idiom (at least in the Java/UNIX/Win32 worlds) where a socket connection is established and the program then utilises two threads: one dedicated to reading from the socket and the other one concurrently writing to the socket - both using...
2
4572
by: jgbid | last post by:
Hi, I'm trying to build an IP Scanner inc c# for a specific port (80) and for specific IP Ranges. For example 24.36.148.1 to 24.36.148.255 My first step was to use TcpClient, but there are nothing to control Timeout Connection... and I wasn't interested to wait 25-30sec for each ip. After some search, it's look like Socket was the solution with
0
1208
by: Qui Sum | last post by:
I write ip/port scanner so users using search on our LAN know if the particular ftp is online or not. The class I write has the following structure: public class IPScanner { public IPScanner() { } private void connect(object data)
0
9704
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9569
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10558
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10318
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10302
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9130
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6844
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4277
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.