Jump to content

Python help please :)

Go to solution Solved by NotBen,

I believe the server is sending messages but they are not being received and so it waits before doing anything else.

To fix this you should probably be receiving the messages sent even if nothing is done with them.

 

def Chat():
    NickName = input("Please enter a nickname: ->")
    message=""
    while message!= NickName+",end":
        
        s.recv(128)
        message =NickName + ","+ str(input("Send: ->"))
        s.sendall(message.encode())

    s.close()
    input("\n\n press enter to close")
    print("successfully disconnected from the server")

 

Hi , any help anyone could offer would be greatly appreciated, basically im trying to write a server which allows multipul clients to connect and send messages to the server. so far i have a login working and the server allows multipul connections. however when i call the "Chat() funtion it only seems to allow each user to send one message. Yet it allows one client to send loads of messages without crashing, the error i get is :

"ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine" 

ive looked online and it says the antivirus/firewall could be causing problems butt ive tried disabling both yet i still get the same problem.

 

to me it seems like a problem with either the function of the multithreading. Any help would be massivly appreciated and the code is included below. 

 

Thanks in advance

 

Sever code



import socket
import hashlib
import threading
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('127.0.0.1', 4000))

def Client_Connection():
    YES = False
    while YES == False:
        Login = c.recv(1024).decode()
        FileName = "Login.txt"
        
        print(Login)
        

        if Login == "c1c224b03cd9bc7b6a86d77f5dace40191766c485cd55dc48caf9ac873335d6fc1c224b03cd9bc7b6a86d77f5dace40191766c485cd55dc48caf9ac873335d6f":
            YES=True
            print("OI")
            AddUser()
        
            
        else:
            file = open(FileName,"r")
            for line in file:
                print(line)
            
                if line  == Login + "\n":
                    YES=True
                    message = "Welcome"
                    c.send(message.encode())
                    Chat()
                else:
                    print("NO")

            c.send("NO".encode())
            file.close


    
def AddUser():
    FileName = "Login.txt"
    file = open("Login.txt" , "a")
    
    RecievedCreds = c.recv(1024).decode()
    print(RecievedCreds)
    file.write(RecievedCreds +"\n")
    file.close
    c.send("New Login Created\nRestarting server Please reconnect".encode())
    c.close()
   
        
    
def Chat():
    MessageContents = ""
    while MessageContents != "end":    
        message = "Welcome"
        c.send(message.encode())
        MessageData = c.recv(1024).decode()#max number of bytes recv
        SplitMessage = MessageData.split(",")
        Sender,MessageContents = SplitMessage[0],SplitMessage[1]
        
        print(Sender,":", MessageContents)

    Disconnect()
def Disconnect():

    c.close()
    input("\n\n press enter to close")
        

        
        

def Setup ():
            
    print("Listening: ")
    while 1:
        s.listen(10) #listen to 1 connection at a time
        global c
        c, addr = s.accept()
        print ("Connection from: ", addr)
        
        Client = threading.Thread(target= Client_Connection)
        Client.start()


Setup()








Client Code

import socket
import hashlib
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

def connect():
    
    message=""
    print("Waiting for connection")
    s.connect(('127.0.0.1', 4000))
    Login()
    
def Login():
    Username = input("Please Enter Your Username: ")
    Password = input("Please Enter Your Password: ")    

    SendPass = hashlib.sha256(Password.encode()).hexdigest()
    SendUser = hashlib.sha256(Username.encode()).hexdigest()

    Credentials = SendUser + SendPass
    s.send(Credentials.encode())
    print(Credentials)
    if Credentials == "c1c224b03cd9bc7b6a86d77f5dace40191766c485cd55dc48caf9ac873335d6fc1c224b03cd9bc7b6a86d77f5dace40191766c485cd55dc48caf9ac873335d6f":
        Admin()
    else:
            
        data = s.recv(1024).decode()
        if data =="Welcome":
            Chat()
        else:   
            Login()


def Admin():
    NewUsername = input("Please enter the new Username: ")
    NewPassword = input("Please enter the new Password: ")

    SendNewPass = hashlib.sha256(NewPassword.encode()).hexdigest()
    SendNewUser = hashlib.sha256(NewUsername.encode()).hexdigest()

    NewCreds = SendNewUser + SendNewPass
    print(NewCreds)

    s.send(NewCreds.encode())

    Return = s.recv(1024).decode()
    print(Return)

    


        
def Chat():
    NickName = input("Please enter a nickname: ->")
    message=""
    while message!= NickName+",end":
        
        message =NickName + ","+ str(input("Send: ->"))
        s.sendall(message.encode())

    s.close()
    input("\n\n press enter to close")
    print("successfully disconnected from the server")
connect()

 

Link to comment
https://linustechtips.com/topic/1058090-python-help-please/
Share on other sites

Link to post
Share on other sites

I believe the server is sending messages but they are not being received and so it waits before doing anything else.

To fix this you should probably be receiving the messages sent even if nothing is done with them.

 

def Chat():
    NickName = input("Please enter a nickname: ->")
    message=""
    while message!= NickName+",end":
        
        s.recv(128)
        message =NickName + ","+ str(input("Send: ->"))
        s.sendall(message.encode())

    s.close()
    input("\n\n press enter to close")
    print("successfully disconnected from the server")

 

Link to comment
https://linustechtips.com/topic/1058090-python-help-please/#findComment-12514191
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×