Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions scripts/tcp_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import socket
import threading
import sys

#Wait for incoming data from server
#.decode is used to turn the message in bytes to a string
def receive(socket, signal):
while signal:
try:
data = socket.recv(32)
print(str(data.decode("utf-8")))
except:
print("You have been disconnected from the server")
signal = False
break

#Get host and port
host = input("Host: ")
port = int(input("Port: "))

#Attempt connection to server
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((host, port))
except:
print("Could not make a connection to the server")
input("Press enter to quit")
sys.exit(0)

#Create new thread to wait for data
receiveThread = threading.Thread(target = receive, args = (sock, True))
receiveThread.start()

#Send data to server
#str.encode is used to turn the string message into bytes so it can be sent across the network
while True:
message = input()
sock.sendall(str.encode(message))