服务端:severchart.py
#-*- coding: utf-8 -*-
import wx
import socket
import sys
import thread
class ServerFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self,None,-1,'server window',size = (535,480))
panel = wx.Panel(self,-1)
self.DisplayText = wx.TextCtrl(panel,-1,'',size=(320,260),style=wx.TE_MULTILINE)
self.InputText = wx.TextCtrl(panel,-1,'',pos = (0,280),size=(320,120),style=wx.TE_MULTILINE)
#jpg = wx.Image("D:/workspace/log/src/serverframe1.jpg", wx.BITMAP_TYPE_JPEG).ConvertToBitmap()
#wx.BitmapButton(panel, -1, jpg, pos=(320, 0))
self.sendButton = wx.Button(panel,-1,'send',pos=(240,410))
self.Bind(wx.EVT_BUTTON,self.OnSendClink,self.sendButton)
self.sendButton.SetDefault()
self.port = 50007
self.host = ''
self.s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
self.s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.s.bind((self.host,self.port))
self.s.listen(5)
def OnSendClink(self, event):
self.send_data = self.InputText.GetValue().encode('utf-8')
self.InputText.SetValue('')
print self.send_data
print self.send_data
print type(self.send_data)
try:
self.tcpClientSock.send(self.send_data)
self.DisplayText.AppendText('You said: \n ')
self.DisplayText.AppendText(self.send_data.decode('utf-8').encode('gb2312'))
self.DisplayText.AppendText('\n')
except socket.error, e:
print(e)
self.c.close()
def serverRe(self):
while True:
print ('waiting for connection,port = %d' % self.port)
self.tcpClientSock,self.addr=self.s.accept()
print ('connect from',self.addr)
while True:
try:
self.data=self.tcpClientSock.recv(1024)
print type(self.data)
if not self.data:
break
print self.data
self.DisplayText.AppendText('%s said: \n ' % self.addr[0] )
self.DisplayText.AppendText(self.data.decode('utf-8').encode('gb2312'))
self.DisplayText.AppendText('\n')
except socket.error, e:
print(e)
self.tcpClientSock.close()
self.s.close()
def mutiThread(self):
thread.start_new(self.serverRe, ())
if __name__ == '__main__':
app=wx.PySimpleApp()
frame=ServerFrame()
frame.Show()
frame.mutiThread()
app.MainLoop()
客户端:clientchat.py
#-*- coding: utf-8 -*-
import wx
import socket
import sys
import thread
class ClientFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self,None,-1,'client window',size = (535,480))
panel = wx.Panel(self,-1)
self.DisplayText = wx.TextCtrl(panel,-1,'',size=(320,260),style=wx.TE_MULTILINE)
self.InputText = wx.TextCtrl(panel,-1,'',pos = (0,280),size=(320,120),style=wx.TE_MULTILINE)
#jpg = wx.Image("D:/workspace/log/src/serverframe1.jpg", wx.BITMAP_TYPE_JPEG).ConvertToBitmap()
#wx.BitmapButton(panel, -1, jpg, pos=(320, 0))
self.sendButton = wx.Button(panel,-1,'send',pos=(240,410))
self.Bind(wx.EVT_BUTTON,self.OnSendClink,self.sendButton)
self.sendButton.SetDefault()
self.port = 50007
self.host = '127.0.0.1'
self.addr = (self.host,self.port)
self.c = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
self.c.connect(self.addr)
def OnSendClink(self, event):
self.send_data = self.InputText.GetValue().encode('utf-8')
self.InputText.SetValue('')
#print type(self.send_data)
try:
self.c.send(self.send_data)
self.DisplayText.AppendText('You said: \n ')
self.DisplayText.AppendText(self.send_data.decode('utf-8').encode('gb2312'))
self.DisplayText.AppendText('\n')
except socket.error, e:
print(e)
self.c.close()
def clientRe(self):
while True:
try:
self.data=self.c.recv(1024)
if not self.data:
break
self.DisplayText.AppendText('server said: \n ')
self.DisplayText.AppendText(self.data.decode('utf-8').encode('gb2312'))
self.DisplayText.AppendText('\n')
except socket.error, e:
print(e)
self.c.close()
def mutiThread(self):
thread.start_new(self.clientRe, ())
if __name__ == '__main__':
app=wx.PySimpleApp()
frame=ClientFrame()
frame.Show()
frame.mutiThread()
app.MainLoop()
运行结果: