1 | #!/usr/bin/python
|
---|
2 | #############################################################################
|
---|
3 | # The chat client
|
---|
4 | #############################################################################
|
---|
5 |
|
---|
6 | """
|
---|
7 | Simple chat client for the chat server. Defines
|
---|
8 | a simple protocol to be used with chatserver.
|
---|
9 |
|
---|
10 | """
|
---|
11 |
|
---|
12 | import socket
|
---|
13 | import sys
|
---|
14 | import select
|
---|
15 | import readline
|
---|
16 | import threading
|
---|
17 | import time
|
---|
18 | from communication import send, receive
|
---|
19 |
|
---|
20 | BUFSIZ = 1024
|
---|
21 |
|
---|
22 |
|
---|
23 | class MyUserInput(threading.Thread):
|
---|
24 | UserInput=''
|
---|
25 | ReceivedData=''
|
---|
26 | def run(self):
|
---|
27 | while (self.UserInput!="exit" and self.UserInput!="EXIT" and self.ReceivedData!="exit" and self.ReceivedData!="EXIT"):
|
---|
28 | dummy=raw_input("CLIENT >>>")
|
---|
29 | if(self.UserInput!="exit" and self.UserInput!="EXIT" and self.ReceivedData!="exit" and self.ReceivedData!="EXIT"):
|
---|
30 | self.UserInput=dummy
|
---|
31 | else:
|
---|
32 | break
|
---|
33 | time.sleep(0.5)
|
---|
34 |
|
---|
35 | class ChatClient(object):
|
---|
36 | """ A simple command line chat client using select """
|
---|
37 |
|
---|
38 | def __init__(self, name, host='127.0.0.1', port=3490):
|
---|
39 | self.myUserInput=MyUserInput()
|
---|
40 | self.myUserInput.daemon=True
|
---|
41 | self.myUserInput.start()
|
---|
42 | self.name = name
|
---|
43 | # Quit flag
|
---|
44 | self.flag = False
|
---|
45 | self.port = int(port)
|
---|
46 | self.host = host
|
---|
47 | # Initial prompt
|
---|
48 | self.prompt='[' + '@'.join((name, socket.gethostname().split('.')[0])) + ']> '
|
---|
49 | # Connect to server at port
|
---|
50 | try:
|
---|
51 | self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
---|
52 | self.sock.connect((host, self.port))
|
---|
53 | print 'Connected to chat server@%d' % self.port
|
---|
54 | # Send my name...
|
---|
55 | send(self.sock,'NAME: ' + self.name)
|
---|
56 | data = receive(self.sock)
|
---|
57 | # Contains client address, set it
|
---|
58 | addr = data.split('CLIENT: ')[1]
|
---|
59 | self.prompt = '[' + '@'.join((self.name, addr)) + ']> '
|
---|
60 | except socket.error, e:
|
---|
61 | print 'Could not connect to chat server @%d' % self.port
|
---|
62 | sys.exit(1)
|
---|
63 |
|
---|
64 | def cmdloop(self):
|
---|
65 | oldUserInput=''
|
---|
66 | while not self.flag:
|
---|
67 | try:
|
---|
68 | #sys.stdout.write(self.prompt)
|
---|
69 | #sys.stdout.flush()
|
---|
70 |
|
---|
71 | # Wait for input from stdin & socket
|
---|
72 | inputready, outputready,exceptrdy = select.select([self.sock], [],[])
|
---|
73 |
|
---|
74 | for i in inputready:
|
---|
75 | #if i == 0:
|
---|
76 | # data = sys.stdin.readline().strip()
|
---|
77 | # if data: send(self.sock, data)
|
---|
78 | if i == self.sock:
|
---|
79 | data = receive(self.sock)
|
---|
80 | if not data:
|
---|
81 | print 'Shutting down.'
|
---|
82 | self.flag = True
|
---|
83 | break
|
---|
84 | else:
|
---|
85 | sys.stdout.write(data + '\n')
|
---|
86 | sys.stdout.flush()
|
---|
87 | if(oldUserInput!=self.myUserInput.UserInput):
|
---|
88 | oldUserInput=self.myUserInput.UserInput
|
---|
89 | send(self.sock,self.myUserInput.UserInput)
|
---|
90 |
|
---|
91 | except KeyboardInterrupt:
|
---|
92 | print 'Interrupted.'
|
---|
93 | self.sock.close()
|
---|
94 | break
|
---|
95 |
|
---|
96 |
|
---|
97 | if __name__ == "__main__":
|
---|
98 | import sys
|
---|
99 |
|
---|
100 | if len(sys.argv)<3:
|
---|
101 | sys.exit('Usage: %s chatid host portno' % sys.argv[0])
|
---|
102 |
|
---|
103 | client = ChatClient(sys.argv[1],sys.argv[2], int(sys.argv[3]))
|
---|
104 | client.cmdloop()
|
---|
105 |
|
---|