-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat_server.py
29 lines (29 loc) · 915 Bytes
/
chat_server.py
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
import socket, sys
name = "Sayeed"
#Get the hostname, IP Address from socket and set Port
soc = socket.socket()
host_name = socket.gethostname()
ip = socket.gethostbyname(host_name)
port = 12345
soc.bind((host_name, port))
soc.listen(1) #Try to locate using socket
client_socket, client_addr = soc.accept()
print("connection established!")
#get a connection from client side
client_name = client_socket.recv(1024)
client_name = client_name.decode()
print(client_name + ' has connected.')
print('Press [bye] to leave the chat room')
client_socket.send(name.encode())
while True:
message = input('Me > ')
if message == '[bye]':
message = 'disconnecting...'
client_socket.send(message.encode())
print("\ndisconnected!")
soc.close()
break
client_socket.send(message.encode())
message = client_socket.recv(1024)
message = message.decode()
print(client_name, '>', message)