Feb-23-2018, 04:48 PM
Hello,
I am writing a program with a simple UDP connection. The program starts and when it receives data from the server, it turns off. I need to run it in a loop. I do not know how to check if the thread is ended up to run it again.
The While loop does not work because the server must return data.
Main program:
Server:
I am writing a program with a simple UDP connection. The program starts and when it receives data from the server, it turns off. I need to run it in a loop. I do not know how to check if the thread is ended up to run it again.
The While loop does not work because the server must return data.
Main program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
import serwer, time from threading import Thread threads = [] #t = Thread(target=serwer.server) #threads.append(t) #t.start() class ThreadWithReturnValue(Thread): def __init__( self , group = None , target = None , name = None , args = (), kwargs = {}, Verbose = None ): Thread.__init__( self , group, target, name, args, kwargs, Verbose) self ._return = None def run( self ): if self ._Thread__target is not None : self ._return = self ._Thread__target( * self ._Thread__args, * * self ._Thread__kwargs) def join( self ): Thread.join( self ) return self ._return twrv = ThreadWithReturnValue(target = serwer.server) def main(): twrv.start() print twrv.join() if __name__ = = '__main__' : main() |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
try : import socket, traceback, sqlite3 except ImportError: print "Blad importu" AddrIn = 2323 AddrOut = 2222 def czas(): import time return str (time.strftime( "%H:%M" )) def data(): import time return str (time.strftime( "%d-%m-%Y" )) def server(): message, address = s.recvfrom(AddrIn) print data() + " " + czas() + " " + "Polaczenie %s: %s" % (address, message) if message[ 0 ] = = 'k' : print 'costam' transmisja(message, address) return message def transmisja(messag, adres): if (messag = = 'temp' ): s.sendto( 'temp2345' , adres) if (messag = = 'wilg' ): s.sendto( 'wilg1111' , adres) if (messag = = 'woda' ): s.sendto( 'woda2222' , adres) if (messag = = 'slon' ): s.sendto( 'slon3333' , adres) if (messag = = 'zasi' ): s.sendto( 'zasi4444' , adres) #-------------START--------------------------- s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1 ) s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1 ) s.bind(('', AddrOut)) #while True: # server() |