LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-06-2020, 06:37 AM   #1
cmgeo
Member
 
Registered: Oct 2011
Location: India
Distribution: Win 10, Debian and ubuntoooo
Posts: 58

Rep: Reputation: Disabled
how to end looping socket server (python script)


hello,

this is my partial server script:
Code:
def proceessConnection(conn, addr):
	global __continueProgamme
	with conn:
		#print('Connected by', addr)
		while True and __continueProgamme == 1:
			data = conn.recv(64)
			print(type(data), data)
			if not data:
				break
			
			if data == b'EXIT':
				#print('settng to exit')
				__continueProgamme = 0
				exit(0)
				break
			elif len(data.decode('utf-8').split(".")) == 4:
				conn.sendall(bytearray(whichCountry((data.decode('utf-8'))),'utf-8'))
			else:
				conn.sendall(data)
		#print("Ending connection",addr)

def getSocket():
	try:
		s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
		print(type(s), s)
		s.bind(('127.0.0.1', 50000))
		return s
	except Exception as e:
		#print("Waiting for socket")
		time.sleep(1)
		return getSocket()

s=getSocket()


__continueProgamme = 1

while True:
	#print("Ready to Accept")
	if __continueProgamme != 1:
		break
	
	s.listen()
	conn, addr = s.accept()
	childT = threading.Thread(target=proceessConnection, args=(conn, addr))
	childT.start()
	#print('finished threading start')
	if __continueProgamme != 1:
		break
	#print("not set to exit")
and this is my client script
Code:
def sendToServer(msg):
	s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	s.connect(("127.0.0.1", 50000))
	s.sendall(msg)
	data = s.recv(1024)
	print(type(data), data)
	print('Received', repr(data))


sendToServer(b'Hello, world')
sendToServer(b'31.13.127.8')
sendToServer(b'EXIT')
sendToServer(b'EXIT')
my problem is that i wish to stop the server on first exit but it does not and only does so on second attempt where it returns exception ConnectionResetError.

i want to smoothly exit the server on first exit.

how do i achieve that.

Thanks for looking into it.

CMG
 
Old 08-06-2020, 07:35 AM   #2
SoftSprocket
Member
 
Registered: Nov 2014
Posts: 399

Rep: Reputation: Disabled
When the thread exits that is processing the first connection the server is blocked in the accept call. When the second connection occurs a new thread is created but the server then sees that the loop variable is now false and exits - breaking the second connection.

If you want to wait for a thread to exit before blocking on an accept call (i.e. only accept one connection at a time) you can use thread.join. If you don't want to block on accept you can set the serve socket non-blocking and then use the select module.
 
1 members found this post helpful.
Old 08-06-2020, 08:28 AM   #3
cmgeo
Member
 
Registered: Oct 2011
Location: India
Distribution: Win 10, Debian and ubuntoooo
Posts: 58

Original Poster
Rep: Reputation: Disabled
Thank you, it solves my problem.

CMG
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Modify UDP receive socket buffer size for an open socket (not at system level, but socket level) barz_83_LQ Linux - Networking 2 11-27-2017 07:56 PM
Connecting client socket to server socket only once in socket programming srinietrx Programming 5 08-20-2017 11:53 AM
LXer: Python Python Python (aka Python 3) LXer Syndicated Linux News 0 08-05-2009 08:30 PM
Python: how to import a python file that doesn't end in .py? BrianK Programming 5 04-15-2008 12:35 PM
python cgi script and premature end of script headers Neruocomp Programming 1 07-28-2005 11:43 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration