Skip to content

Commit c0cef7b

Browse files
authored
[Feature] #16 from i-OmSharma/Om2
#4 CLI chat tool Implemented
2 parents a529092 + fe5391a commit c0cef7b

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

chat_tool/clinet.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import socket
2+
3+
# Create a socket object
4+
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
5+
6+
# Get local machine name
7+
host = socket.gethostname()
8+
9+
# Define the port on which you want to connect
10+
port = 12345
11+
12+
# Connect to the server
13+
client_socket.connect((host, port))
14+
15+
while True:
16+
message = input('Enter your message: ')
17+
client_socket.send(message.encode('utf-8'))
18+
data = client_socket.recv(1024).decode('utf-8')
19+
print('Received:', data)
20+
21+
# Close the socket
22+
client_socket.close()

chat_tool/server.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import socket
2+
3+
# Create a socket object
4+
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
5+
6+
# Get local machine name
7+
host = socket.gethostname()
8+
9+
# Define the port on which you want to connect
10+
port = 12345
11+
12+
# Bind the socket to the address and port
13+
server_socket.bind((host, port))
14+
15+
# Now wait for a client to connect
16+
server_socket.listen(1)
17+
18+
print(f'Waiting for connections on {host}:{port}...')
19+
20+
# Accept the connection
21+
client_socket, addr = server_socket.accept()
22+
23+
print('Got connection from', addr)
24+
25+
while True:
26+
data = client_socket.recv(1024).decode('utf-8')
27+
print('Received:', data)
28+
message = input('Enter your message: ')
29+
client_socket.send(message.encode('utf-8'))
30+
31+
# Close the socket
32+
client_socket.close()

0 commit comments

Comments
 (0)