Skip to content

Commit

Permalink
Merge pull request #1 from sundayScoop/add-threading
Browse files Browse the repository at this point in the history
Added threading
  • Loading branch information
sundayScoop authored Mar 10, 2022
2 parents 11cf844 + b4cd85d commit 817bea4
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 31 deletions.
44 changes: 29 additions & 15 deletions client.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,45 @@
'''
EthanGo v1.0
EthanGo v1.1
Super simple socket program. No security. No privacy.
Do not run for prolonged periods of time.
Issues:
- Each sender must take turns sending messages
- Client will crash if server not up
- Simple terminal chat is not cool
Made for educational purposes only.
Written by Julio Medeiros
10/03/2022
'''

import socket
import threading

version = 1.0
version = 1.1
print("Version: ", version, "\n")

HOST = "127.0.0.1" #input("Server's IP adress: ") # The server's hostname or IP address
PORT = 14099 # The port used by the server

def receive(conn):
while True:
data = conn.recv(1024) ## Receive messages from client
if not data: ## If data is null, connection broken > END
break
print("\nThem> ", data.decode("utf-8") ) ## If data exists, decode and display
quit_flag = True

def send(conn):
while True:
msg = bytes(input("Me> "), "utf-8") ## Collect msg to send from server in bytes
conn.sendall(msg) ## Send msg to client

quit_flag = False

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
while True:
msg = bytes(input("Me> "), "utf-8") ## Get msg to send from user and convert into bytes
s.sendall(msg) ## Send message to server
data = s.recv(1024) ## Receive any messages sent from server
print("Them> ", data.decode("utf-8") ) ## Decode messages sent from server

print(f"Received {data!r}")
r_thread = threading.Thread(target=receive, args=(s,), daemon=True)
r_thread.start()

s_thread = threading.Thread(target=send, args=(s,), daemon=True)
s_thread.start()

while not quit_flag:
a=0
quit()
45 changes: 29 additions & 16 deletions server.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,48 @@
'''
EthanGo v1.0
EthanGo v1.1
Super simple socket program. No security. No privacy.
Do not run for prolonged periods of time.
Issues:
- Each sender must take turns sending messages
- Client will crash if server not up
- Simple terminal chat is not cool
Made for educational purposes only.
Written by Julio Medeiros
10/03/2022
'''

import socket
import threading

version = 1.0
version = 1.1
print("Version: ", version, "\n")

HOST = "127.0.0.1" # Standard loopback interface address (localhost)
PORT = 14099 # High port to evade scans

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen()
conn, addr = s.accept()
with conn:
print(f"Connected by {addr}")
def receive(conn):
while True:
data = conn.recv(1024) ## Receive messages from client
if not data: ## If data is null, connection broken > END
break
print("Them> ", data.decode("utf-8") ) ## If data exists, decode and display
print("\nThem> ", data.decode("utf-8") ) ## If data exists, decode and display
quit_flag = True

def send(conn):
while True:
msg = bytes(input("Me> "), "utf-8") ## Collect msg to send from server in bytes
conn.sendall(msg) ## Send msg to client

quit_flag = False

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen()
conn, addr = s.accept()

r_thread = threading.Thread(target=receive, args=(conn,), daemon=True)
r_thread.start()

s_thread = threading.Thread(target=send, args=(conn,), daemon=True)
s_thread.start()


while not quit_flag:
a=0
quit()

0 comments on commit 817bea4

Please sign in to comment.