Skip to content

Commit b56b236

Browse files
committed
Adding the files
1 parent 4c07036 commit b56b236

File tree

4 files changed

+83
-0
lines changed

4 files changed

+83
-0
lines changed

chat_client.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import socket, sys
2+
#Get the hostname, IP Address from socket and set Port
3+
soc = socket.socket()
4+
shost = socket.gethostname()
5+
ip = socket.gethostbyname(shost)
6+
#get information to connect with the server
7+
port = 12345
8+
print('trying to connect to the user...\n')
9+
soc.connect((socket.gethostname(), port))
10+
print(f"connected to {shost}...\n")
11+
name = input('enter your name: ')
12+
print('\n')
13+
soc.send(name.encode())
14+
server_name = soc.recv(1024)
15+
server_name = server_name.decode()
16+
print('Enter [bye] to exit.')
17+
while True:
18+
message = soc.recv(1024)
19+
message = message.decode()
20+
print(server_name, ">", message)
21+
message = input(str("Me > "))
22+
if message == "[bye]":
23+
message = "Left the Chat room"
24+
soc.send(message.encode())
25+
print("\ndisconnecting...\ndisconnected")
26+
soc.close()
27+
break
28+
soc.send(message.encode())

chat_server.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import socket, sys
2+
name = "Sayeed"
3+
#Get the hostname, IP Address from socket and set Port
4+
soc = socket.socket()
5+
host_name = socket.gethostname()
6+
ip = socket.gethostbyname(host_name)
7+
port = 12345
8+
soc.bind((host_name, port))
9+
soc.listen(1) #Try to locate using socket
10+
client_socket, client_addr = soc.accept()
11+
print("connection established!")
12+
#get a connection from client side
13+
client_name = client_socket.recv(1024)
14+
client_name = client_name.decode()
15+
print(client_name + ' has connected.')
16+
print('Press [bye] to leave the chat room')
17+
client_socket.send(name.encode())
18+
while True:
19+
message = input('Me > ')
20+
if message == '[bye]':
21+
message = 'disconnecting...'
22+
client_socket.send(message.encode())
23+
print("\ndisconnected!")
24+
soc.close()
25+
break
26+
client_socket.send(message.encode())
27+
message = client_socket.recv(1024)
28+
message = message.decode()
29+
print(client_name, '>', message)

time_client.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import socket
2+
3+
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
4+
client_socket.connect((socket.gethostname(), 5001))
5+
6+
print("requesting the time server for local time...")
7+
msg = client_socket.recv(1024)
8+
print("local time >> ", msg.decode(),"\n")
9+

time_server.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import socket
2+
import time
3+
t = time.localtime()
4+
current_time = time.strftime("%H:%M:%S", t)
5+
current_time = str(current_time)
6+
msg = current_time
7+
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
8+
server_socket.bind((socket.gethostname(), 5001))
9+
server_socket.listen(5)
10+
11+
while True:
12+
client_socket, client_address = server_socket.accept()
13+
print(f"connection from {client_address} has been established!")
14+
client_socket.send(msg.encode())
15+
print(f"disconnecting with {client_address}...\ndisconnected!\n")
16+
client_socket.close()
17+

0 commit comments

Comments
 (0)