-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtcp_client.py
More file actions
80 lines (61 loc) · 2.35 KB
/
tcp_client.py
File metadata and controls
80 lines (61 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
"""
Lab 06 — TCP Client Program
Tasks Covered:
- Task 1: TCP Server and Client communication
- Task 3: Exchange at least five messages
- Task 4: Send Student Name and Registration Number
Bonus:
- Timestamp in messages
- Convert received message to UPPERCASE
- Terminate when user types 'exit'
"""
import socket
import datetime
# ─────────────────────────────────────────
# STUDENT INFO — EDIT THESE
# ─────────────────────────────────────────
STUDENT_NAME = "Niha Hawas"
REG_NUMBER = "2312274"
# ─────────────────────────────────────────
def get_timestamp():
return datetime.datetime.now().strftime("[%H:%M:%S]")
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = "127.0.0.1"
port = 12345
print("=" * 50)
print(" TCP CLIENT STARTED")
print(f" Student : {STUDENT_NAME}")
print(f" Reg No : {REG_NUMBER}")
print("=" * 50)
try:
client_socket.connect((host, port))
print(f"✅ Connected to server at {host}:{port}\n")
# Receive server's intro message
intro = client_socket.recv(1024).decode()
print(f"Server Info: {intro}\n")
msg_count = 0
while True:
message = input("Your Message (type 'exit' to quit): ").strip()
if not message:
continue
# Bonus: Add timestamp + student info to message
ts = get_timestamp()
full_message = f"{ts} [{STUDENT_NAME} | {REG_NUMBER}] Client: {message}"
client_socket.send(full_message.encode())
msg_count += 1
# Bonus: Terminate if client types 'exit'
if message.lower() == "exit":
print("You ended the session.")
break
data = client_socket.recv(1024).decode()
# Bonus: Convert server reply to UPPERCASE for display
print(f"Server Reply [{msg_count}]: {data}")
print(f" ↳ UPPERCASE: {data.upper()}\n")
if "Goodbye" in data:
break
except ConnectionRefusedError:
print("❌ ERROR: Could not connect to server.")
print(" → Make sure tcp_server.py is running FIRST.")
finally:
client_socket.close()
print("\n✅ Client closed. Messages sent: {msg_count}")