-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from sundayScoop/add-threading
Added threading
- Loading branch information
Showing
2 changed files
with
58 additions
and
31 deletions.
There are no files selected for viewing
This file contains 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
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() |
This file contains 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
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() |