Created
April 5, 2020 00:06
-
-
Save kaanberke/2edbb7059142e57ef5ecd9055ecb8fb8 to your computer and use it in GitHub Desktop.
ChatApplicationSystemSocket
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import socket | |
| import select | |
| import errno | |
| import sys | |
| HEADER_LENGTH = 10 | |
| IP = '127.0.0.1' | |
| PORT = 3000 | |
| username = input('Username: ').encode('utf-8') | |
| username_header = f'{len(username):<{HEADER_LENGTH}}'.encode('utf-8') | |
| client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
| client_socket.connect((IP, PORT)) | |
| client_socket.setblocking(False) # Receive won't be blocking.. | |
| client_socket.send(username_header + username) | |
| while True: | |
| message = input(f'{username} > ') | |
| message | |
| if message: | |
| message = message.encode('utf-8') | |
| message_header = f'{len(message):<{HEADER_LENGTH}}'.encode('utf-8') | |
| client_socket.send(message_header + message) | |
| try: | |
| while True: | |
| username_header = client_socket.recv(HEADER_LENGTH) | |
| if not len(username_header): | |
| print('Connection closed by the server') | |
| sys.exit() | |
| username_length = int(username_header.decode('utf-8')) | |
| username = client_socket.recv(username_length).decode('utf-8') | |
| message_header = client_socket.recv(HEADER_LENGTH) | |
| message_length = int(message_header.decode('utf-8')) | |
| message = client_socket.recv(message_length).decode('utf-8') | |
| print(f'{username} > {message}') | |
| except IOError as e: | |
| if e.errno != errno.EAGAIN and e.errno != errno.EWOULDBLOCK: | |
| print('Reading error:', str(e)) | |
| sys.exit() | |
| continue | |
| except Exception as e: | |
| print('Normal error: ', str(e)) | |
| sys.exit() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import socket | |
| import select | |
| HEADER_LENGTH = 10 | |
| IP = '127.0.0.1' | |
| PORT = 3000 | |
| server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
| server_socket.setsockopt( | |
| socket.SOL_SOCKET, # Thing you want to set | |
| socket.SO_REUSEADDR, # What you want to set | |
| 1 # Set the thing | |
| ) | |
| server_socket.bind((IP, PORT)) | |
| server_socket.listen() | |
| sockets_list = [server_socket] | |
| clients = {} | |
| def receive_message(client_socket): | |
| try: | |
| message_header = client_socket.recv(HEADER_LENGTH) | |
| if not len(message_header): | |
| return False | |
| message_length = int(message_header.decode('utf-8')) | |
| return { | |
| 'header': message_header, | |
| 'data': client_socket.recv(message_length) | |
| } | |
| except: | |
| return False | |
| while True: | |
| read_sockets, _, exception_sockets = select.select(sockets_list, [], sockets_list) | |
| for notified_socket in read_sockets: | |
| if notified_socket == server_socket: | |
| client_socket, client_address = server_socket.accept() | |
| user = receive_message(client_socket) | |
| if user is False: | |
| continue | |
| sockets_list.append(client_socket) | |
| clients[client_socket] = user | |
| print(f'Accepted new connection from {client_address[0]}:{client_address[1]} username:{user["data"].decode("utf-8")}') | |
| else: | |
| message = receive_message(notified_socket) | |
| if message is False: | |
| print(f'Closed connection from {clients[notified_socket]["data"].decode("utf-8")}') | |
| sockets_list.remove(notified_socket) | |
| del client_socket[notified_socket] | |
| continue | |
| user = clients[notified_socket] | |
| print(f'Received message from {user["data"].decode("utf-8")}: {message["data"].decode("utf-8")}') | |
| for client_socket in clients: | |
| if client_socket != notified_socket: | |
| client_socket.send(user["header"] + user["data"] + message["header"] + message["data"]) | |
| for notified_socket in exception_sockets: | |
| sockets_list.remove(notified_socket) | |
| del client_socket[notified_socket] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment