-
Notifications
You must be signed in to change notification settings - Fork 1
/
client_ipv6.py
52 lines (42 loc) · 1.27 KB
/
client_ipv6.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import socket
import select
import sys
if(len(sys.argv) <3) :
print 'Format : python client.py hostname port'
sys.exit()
host = sys.argv[1]
port = int(sys.argv[2])
socketclient = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
# Connection establishment with the server
try : socketclient.connect((host, port))
except :
print 'Unable to connect'
sys.exit()
print "You have joined the chat room! Start messaging to know others!\n"
print "Type 'disconnect' to leave chatroom"
sys.stdout.write('Me: ')
sys.stdout.flush()
while True:
messagesocket = [sys.stdin, socketclient]
# Getting readable sockets and reading them with select
readsock, writesock, errsock = select.select(messagesocket , [], [])
for sock in readsock:
#Message received from server
if sock is not socketclient:
#when client wants to send a message
msg = sys.stdin.readline()
socketclient.send(msg)
sys.stdout.write('Me: ')
sys.stdout.flush()
else :
#data recevied from the RECV_BUFFER in the server
data = sock.recv(4096)
if not data :
#if client is disconnected from the server
print '\nDisconnected from chat server'
sys.exit()
else :
#receive message from other clients
sys.stdout.write(data)
sys.stdout.write('Me: ')
sys.stdout.flush()