-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.py
56 lines (47 loc) · 1.37 KB
/
client.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
53
54
55
56
# -*- coding: utf-8 -*-
import socket
from Tkinter import *
import os
reload(sys)
sys.setdefaultencoding('utf-8')
tk=Tk()
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
s.bind(('0.0.0.0',11719))#11719
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST,1)
text=StringVar()
name=StringVar()
name.set('Client')
text.set('')
tk.title('Chat')
tk.geometry('400x300')
log = Text(tk)
nick = Entry(tk, textvariable=name)
msg = Entry(tk, textvariable=text)
msg.pack(side='bottom', fill='x', expand='true')
nick.pack(side='bottom', fill='x', expand='true')
log.pack(side='top', fill='both',expand='true')
def loopproc():
log.see(END)
s.setblocking(False)
try:
message = s.recv(128)
log.insert(END,message+'\n')
except:
tk.after(1,loopproc)
return
tk.after(1,loopproc)
return
def sendproc(event):
if(text.get()==':exit'):
text.set(' logged out====')
sock.sendto ('====User '+name.get()+text.get(),('255.255.255.255',11719))
exit(0)
sock.sendto (name.get()+':'+text.get(),('255.255.255.255',11719))
text.set('')
msg.bind('<Return>',sendproc)
msg.focus_set()
tk.after(1,loopproc)
tk.mainloop()