Created
March 21, 2018 23:18
-
-
Save Zwork101/858c60241435f5ea4c48fff6c3718298 to your computer and use it in GitHub Desktop.
New files
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 threading | |
def sendMsg(): | |
while running: | |
try: | |
message = input("Send: ") | |
sock.send(message.encode()) | |
except: | |
print("Couldn't send message") | |
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
adress = str(input("Please enter adress; ")) | |
port = int(input("Please enter port: ")) | |
try: | |
sock.connect((adress, port)) | |
running = True | |
message = str(input("Please enter your username: ")) | |
try: | |
sock.send(message.encode()) | |
iThread = threading.Thread(target=sendMsg) | |
iThread.daemon = True | |
iThread.start() | |
while running: | |
try: | |
data = sock.recv(1024) | |
print(data.decode()) | |
except ConnectionResetError as e: | |
print(e) | |
running = False | |
except: | |
print("Couldn't send message") | |
except: | |
print("Couldn't connect") |
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 threading | |
import sys | |
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
connections = [] | |
port = int(input("Please select a port: ")) | |
sock.bind(('0.0.0.0', port)) | |
sock.listen(1) | |
print("Server initialized") | |
def send(message, connection): | |
print("Sending '" + message + "'") | |
try: | |
connection.send(message.encode()) | |
except: | |
print("Failed to send.") | |
def handler(c, a): | |
aux = 1 | |
try: | |
data = c.recv(1024) | |
username = data.decode() | |
except ConnectionResetError as e: | |
print(e) | |
aux = 0 | |
connections.remove(c) | |
c.close() | |
while aux == 1: | |
try: | |
data = c.recv(1024) | |
data = data.decode() | |
for connection in connections: | |
message = (username + ': ' + data) | |
send(message, connection) | |
except ConnectionResetError as e: | |
print(e) | |
connections.remove(c) | |
c.close() | |
break | |
while True: | |
c, a = sock.accept() | |
cThread = threading.Thread(target=handler, args=(c, a)) | |
cThread.daemon = True | |
cThread.start() | |
connections.append(c) | |
print(connections) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment