3

I am making a basic chat application. while making, I am encountering a problem when using input function in server and client to send messages between the two. I have made two functions for sending and receiving the messages and used two Threads in order to handle these functions becuase I wanted to send and recieve messages at the same time.I have set the "bye" keyword to terminate the connection in both the functions that is when sending to the client and when receiving from client.

My problem is complex, so I will explain by an Example of this below code.

when I send the "bye" message from the client to server. The client script checks the message and then sends it and then it closes the connection in the send function of client script and then the main problem arises. It recieves the message in the recieve funtion in the below server file of t2 Thread and then the connection is closed for both server and client but t1 Thread in below code of server invokes and expects an input. So how do I solve this.

and also the client script returns an exception because recieve Thread invokes and uses the client object to recieve the message from server but how it will recieve it because the connection is closed. so, this is the problem arising in the server file and also the opposite in client file.

This is the code of the server file.

import threading
from threading import *
import time
import socket
server_socket= socket.socket()
ip= socket.gethostbyname(socket.gethostname())
port= 1234
server_socket.bind((ip,port))
server_socket.listen()
print("\n server is listining to their clients!!! ")
client_socket, client_add= server_socket.accept()
print(f"Got connected to {client_add}") 
def send():
    b= lambda x: "\n".join([x]*g)
    while stop_thread.is_set():
        try:
            sms= input("server--> ")
            
            inputs= sms.split()
            getdata= inputs[0]
            if len(inputs)>1:
                g= int(inputs[1])
                sms= b(getdata)
            client_socket.send(sms.encode())
        except ValueError:
            client_socket.send(sms.encode())
        except Exception as e:
            print(e)
    
        if sms.lower()=="bye":
            print("\n Socket was closed by you!!")
            client_socket.close()
            server_socket.close()
            stop_thread.clear()
            break
            
    
def recieve():

    while stop_thread.is_set():
        msg= client_socket.recv(1024)
        if msg.decode().lower().strip()=="bye":
            print("\nserver was closed by Client!! ")
            client_socket.close()
            server_socket.close()
            stop_thread.clear()
            break
            
        else:
            print("\n\n>>Client --> ", msg.decode())
            
            
  

stop_thread= Event() 
t1= Thread(target=send )
t2= Thread(target=recieve)
stop_thread.set()



t1.start()
t2.start()


I know this is because of the GIL(Global interpretor lock) which halts the entire program until the user presses a key. I have also tried some python libraries including queue, pynput, and many more. I want that when I type "bye" it should not stop at the input function and just close all the connections and also at the recieve funtion in the other file.

  • There is no really clean way to abort an `input`. See e. g. https://stackoverflow.com/questions/72323177/how-to-force-input-to-stop-and-continue-the-code-flow – Michael Butscher Jul 24 '23 at 11:29

0 Answers0