聊天室服务器客户机的编写
创建一个标准EXE工程,在控件工具栏中点击鼠标右键,选择部件,在控件选项卡中选中Microsoft Winsock Control 6.0就行了。接下来在新建的窗体上添加一个Winsock和一个Command控件,其参数分别设置如下:Winsock的caption属性设为sock1,protocol属性设为0-TCP,Command1的caption属性设为“关闭(&C)”,调整好各个控件和窗体的大小,界面如^17020503a^1。
#1 服务器程序代码如下??
?牐燨ption Explicit
?牐燚im m As Integer '作信息寄存器记数用
?牐燚im tex(200) As String '信息寄存器
?牐燚im n As Integer 'winsock用
?牐牐Ч乇辗衿?
?牐燩rivate Sub Command1_Click()?煟?
?牐燛nd
?牐燛nd Sub
?牐牐Ы雇痹诵辛酱纬绦?
?牐燩rivate Sub Form_Initialize()?煟?
?牐營f App.PrevInstance Then
?牐燤sgBox "程序已经运行!", vbOKOnly, "提示"
?牐燛nd
?牐燛nd If
?牐燛nd Sub
?牐燩rivate Sub Form_Load()?煟?
?牐爊 = 0
?牐爉 = 0
?牐爏ock1(0).LocalPort = 1024
?牐爏ock1(0).Listen
?牐燛nd Sub
?牐燩rivate Sub sock1_Close(Index As Integer)??
?牐爏ock1(Index).Close
?牐燛nd Sub
?牐燩rivate Sub sock1_ConnectionRequest(Index As Integer, ByVal requestID As Long)
?牐爊 = n + 1
?牐燣oad sock1(n)??
?牐爏ock1(n).Accept requestID
?牐燛nd Sub
?牐燩rivate Sub sock1_DataArrival(Index As Integer, ByVal requestID As Long)
?牐燚im rec As String
?牐燚im i As Integer
?牐牐Ы邮苁?
?牐爏ock1(Index).GetData rec, vbString
?牐營f rec = "刷新数据" Then
?牐燛lse '存储数据
?牐爐ex(m) = rec
?牐爉 = m + 1
?牐燛nd If
?牐牐ё⑹?
?牐燜or i = m - 1 To 199
?牐爏ock1(Index).SendData tex(i) + Chr(13) + Chr(10)??
?牐燦ext
?牐燜or i = 0 To m
?牐爏ock1(Index).SendData tex(i) + Chr(13) + Chr(10)?牐?
?牐燦ext
?牐牐ё远境畔?
?牐營f m = 200 Then m = 0
?牐燛nd Sub
#1 客户端程序??
?牐犘陆ㄒ桓霰曜糆XE工程,在新建的窗体上添加如下控件:winsock(sock2)、command1(退出)、label1(接受窗)、label2(发送窗)、label3、text1、text2。其中winsock的protocol属性设为0-TCP,label3的caption属性设为"",autosize设为True,text1的Multiline属性设为True,ScrollBars属性设为3-both,text1、text2的text属性设为""。调整好各控件大小,界面如^17020503b^2。
#1?牐牽突Ф顺绦虼肴缦娄?
?牐燨ption Explicit
?牐燚im ip As String
?牐牐Ъ锹挤衿鞯腎P地址
?牐燚im nam As String '记录本人姓名
?牐牐Ы雇痹诵辛酱纬绦?
?牐燩rivate Sub Form_Initialize?煟?
?牐營f App.PrevInstance Then
?牐燤sgBox "程序已经运行!", vbOKOnly, "提示"
?牐燛nd
?牐燛nd If
?牐燛nd Sub
?牐牐С绦虺跏蓟?
?牐燩rivate Sub Form_Load()?煟?
?牐爄p = InputBox("输入服务器的IP地址", "登录框")??
?牐爊am = InputBox("输入您的姓名", "登录框")??
?牐爏ock2.RemoteHost = ip
?牐爏ock2.RemotePort = 1024
?牐爏ock2.Connect
?牐燣abel3.Caption = nam
?牐牐Ы彰允境隼?
?牐燛nd Sub
?牐牐Ы褂?"X"按钮退出
?牐燩rivate Sub Form_Unload(Cancel As Integer)??
?牐燙ancel = True
?牐燛nd Sub
?牐燩rivate Sub command1_Click()?煟?
?牐爏ock2.SendData nam + "退出聊天室!"
?牐燛nd
?牐燛nd Sub
?牐燩rivate Sub sock2_Close()?煟?
?牐爏ock2.Close
?牐燛nd Sub '连接服务器
?牐燩rivate Sub sock2_Connect()?煟?
?牐爏ock2.SendData nam + "进入聊天室!"
?牐燛nd Sub
?牐牐?数据接受并显示
?牐燩rivate Sub sock2_DataArrival(ByVal bytesTotal As Long)??
?牐燚im rec As String
?牐爏ock2.GetData rec, vbString
?牐燭ext1.Text = rec
?牐燭ext1.SelStart = Len(Text1.Text)??
?牐燛nd Sub '发送窗
?牐燩rivate Sub Text2_KeyPress(KeyAscii As Integer)??
?牐燚im msg As String
?牐營f KeyAscii = 13 Then
?牐營f Text2.Text = "" Then
?牐燤sgBox "说话内容不能为空", vbExclamation + vbOKOnly, "提示"
?牐燛lse
?牐爉sg = nam + ": " + Text2.Text
?牐燭ext2.Text = ""
?牐爏ock2.SendData msg
?牐燛nd If
?牐燛nd If
?牐燛nd Sub
?牐犞链耍龀绦蛞丫瓿桑共煌晟疲蛭荒苁凳毕允久扛鋈说姆⒀浴R胧凳毕允荆捅匦胱鲆恍└亩7椒ㄊ?:在客户端窗体上添加一个时钟控件,其Interval属性设为1000(即1秒),Enabled属性设为False。
#1 在代码窗中添加如下代码:
?牐犜趂orm_load过程的最后添加一行代码:Timer1.Enabled = True
?牐牐Фㄊ彼⑿?
?牐燩rivate Sub Timer1_Timer?煟?
?牐爏ock2.SendData "刷新数据"
?牐燛nd Sub
?牐犗旅嬖僭诜衿鞒绦蛑凶鲆韵赂亩涸趕ock1_DataArrival过程中将
?牐爐ex(m) = rec
?牐爉 = m + 1
?牐牳奈?
?牐營f rec = "刷新数据" Then
?牐燛lse
?牐爐ex(m) = rec
?牐爉 = m + 1
?牐燛nd If
?牐犝庋飧隽奶焓页绦蚓途弑噶耸凳绷奶斓墓δ堋A硗獯蠹一箍梢栽诖嘶∩咸砑悠渌δ埽绺拿⑺搅摹⒖璺考涞取#?