473,385 Members | 1,753 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

shuttong down socket from another thread

hi!

i have a main application which is run through a GUI. the GUI has a
function 'start server' which starts a server in another thread and a stop
function which should stop this server.
the code is

from Tkinter import *
import tkMessageBox
import tkFileDialog
import socket
import threading
from Queue import Queue

class MainApp:
def __init__(self, master):
self.frame = Frame(master)
self.frame.pack()
self.th = None
...

def startServer(self):
self.th = threading.Thread(target=self.doStartServer)
self.th.start()

def doStartServer(self):
print "starting threaded"
UDPserversocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
UDPserversocket.bind((socket.gethostname(), LOCAL_SERVER_PORT))
print "binded to address: ", socket.gethostname()
while 1:
data, address = UDPserversocket.recvfrom(1024)
print data

root = Tk()
app = MainApp(root)
root.title("X Privacy Agent")
root.protocol("WM_DELETE_WINDOW", lambda: sys.exit(0))
root.mainloop()

i tried to do the following in stopServer
def stopServer(self):
print "stopping"
if self.th is not None and self.th.isAlive():
#may not be a clean shut down
self.th.join(5)
print "stopped"

first of all, i would like to have a clean termination of the server. so
that if its handling any data etc, it should finish handling it and then
close. secondly the above code doesn't seem to work. after running the
server and then stopping it, when i click on the close window icon, the
application simply crashes.
i could add the variable to the startServer's while loop and then change
the variable in stopServer, but that would work only after the server has
received one datagram (since it will be blocked in the recvfrom call).

any suggestions of how i should go about allowing the user to
1. stop and restart the server
2. and close the application at any time.

thanks

cheers

----------------------------------------------------------------
This message was sent using IMP, the Internet Messaging Program.
Jul 18 '05 #1
0 1531

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

Similar topics

8
by: simon place | last post by:
Spent some very frustrating hours recoding to find a way of closing a server socket, i'd not thought it would be any problem, however, after complete failure and as a last resort, i looked at the...
3
by: Jos | last post by:
Hello. I'm using the asyncore and _chat modules to create a network server. I also have, running in a separate thread(s), a "producer" which needs to "push" data onto the network connection(s)....
4
by: Stephan Steiner | last post by:
Hi I'm having some weird threading issues.. almost at random, if I dare change a line of my code, the shutdown sequence gets messed up. I'm using a thread to receive data from the network, that...
4
by: Matthew Groch | last post by:
Hi all, I've got a server that handles a relatively high number of concurrent transactions (on the magnitude of 1000's per second). Client applications establish socket connections with the...
2
by: Dirk Reske | last post by:
Hello, I have following thread, that reads data from a socket. private void RecThread() { int Len = 0; byte input = new byte; while((Len = socket.Receive(input)) > 0) //*1
7
by: Droopy | last post by:
Hi, I don't understand why sometimes, a loop running in a thread takes much more time than usual. I have a thread that must call each 20 ms a C++ unmanaged function. I made a C++ managed...
4
by: DaveR | last post by:
I have a background thread to accept and process TCP/IP connections. When the application shuts down I want to gracefully terminate it. The thread will block in NetworkStream.Read() waiting for...
5
by: zxo102 | last post by:
Hi, I am doing a small project using socket server and thread in python. This is first time for me to use socket and thread things. Here is my case. I have 20 socket clients. Each client send a...
18
by: =?Utf-8?B?VGhlU2lsdmVySGFtbWVy?= | last post by:
Because C# has no native SSH class, I am using SharpSSH. Sometimes, for reasons I do not know, a Connect call will totally lock up the thread and never return. I am sure it has something to do...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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...

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.