mirror of
https://github.com/Xevion/tcp-chat.git
synced 2025-12-14 16:13:12 -06:00
repo init
This commit is contained in:
65
server.py
Normal file
65
server.py
Normal file
@@ -0,0 +1,65 @@
|
||||
import socket
|
||||
import threading
|
||||
|
||||
# Connection Data
|
||||
host = '127.0.0.1'
|
||||
port = 55555
|
||||
|
||||
# Starting Server
|
||||
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
server.bind((host, port))
|
||||
server.listen()
|
||||
|
||||
# Lists For Clients and Their Nicknames
|
||||
clients = []
|
||||
nicknames = []
|
||||
|
||||
|
||||
# Sending Messages To All Connected Clients
|
||||
def broadcast(message):
|
||||
for client in clients:
|
||||
client.send(message)
|
||||
|
||||
|
||||
# Handling Messages From Clients
|
||||
def handle(client):
|
||||
while True:
|
||||
try:
|
||||
# Broadcasting Messages
|
||||
message = client.recv(1024)
|
||||
broadcast(message)
|
||||
except:
|
||||
# Removing And Closing Clients
|
||||
index = clients.index(client)
|
||||
clients.remove(client)
|
||||
client.close()
|
||||
nickname = nicknames[index]
|
||||
broadcast('{} left!'.format(nickname).encode('ascii'))
|
||||
nicknames.remove(nickname)
|
||||
break
|
||||
|
||||
|
||||
# Receiving / Listening Function
|
||||
def receive():
|
||||
while True:
|
||||
# Accept Connection
|
||||
client, address = server.accept()
|
||||
print("Connected with {}".format(str(address)))
|
||||
|
||||
# Request And Store Nickname
|
||||
client.send('NICK'.encode('ascii'))
|
||||
nickname = client.recv(1024).decode('ascii')
|
||||
nicknames.append(nickname)
|
||||
clients.append(client)
|
||||
|
||||
# Print And Broadcast Nickname
|
||||
print("Nickname is {}".format(nickname))
|
||||
broadcast("{} joined!".format(nickname).encode('ascii'))
|
||||
client.send('Connected to server!'.encode('ascii'))
|
||||
|
||||
# Start Handling Thread For Client
|
||||
thread = threading.Thread(target=handle, args=(client,))
|
||||
thread.start()
|
||||
|
||||
|
||||
receive()
|
||||
Reference in New Issue
Block a user