淺談?dòng)肰B實(shí)現(xiàn)聊天討論室和點(diǎn)對(duì)點(diǎn)會(huì)話論文
淺談?dòng)肰B實(shí)現(xiàn)聊天討論室和點(diǎn)對(duì)點(diǎn)會(huì)話論文
Visual Basic是一種由 公司開發(fā)的結(jié)構(gòu)化的、模塊化的、面向?qū)ο蟮?、包含協(xié)助開發(fā)環(huán)境的事件驅(qū)動(dòng)為機(jī)制的可視化程序設(shè)計(jì)語(yǔ)言。從任何標(biāo)準(zhǔn)來說,VB都是世界上使用人數(shù)最多的語(yǔ)言——不管是盛贊VB的開發(fā)者還是抱怨VB的開發(fā)者的數(shù)量。它源自于BASIC編程語(yǔ)言。VB擁有圖形用戶界面(GUI)和快速應(yīng)用程序開發(fā)(RAD)系統(tǒng),可以輕易的使用DAO、RDO、ADO連接數(shù)據(jù)庫(kù),或者輕松的創(chuàng)建ActiveX控件。程序員可以輕松的使用VB提供的組件快速建立一個(gè)應(yīng)用程序。以下是學(xué)習(xí)啦小編今天為大家精心準(zhǔn)備的:淺談?dòng)肰B實(shí)現(xiàn)聊天討論室和點(diǎn)對(duì)點(diǎn)會(huì)話相關(guān)論文。內(nèi)容僅供參考,歡迎閱讀!
淺談?dòng)肰B實(shí)現(xiàn)聊天討論室和點(diǎn)對(duì)點(diǎn)會(huì)話全文如下:
在一個(gè)單位內(nèi)部或通過廣域協(xié)議(如X.25)互聯(lián)的行業(yè)內(nèi)部都有幾十或上萬臺(tái)計(jì)算機(jī)互聯(lián),用Intranet雖然可以建立聊天室,但實(shí)現(xiàn)點(diǎn)對(duì)點(diǎn)實(shí)時(shí)對(duì)話卻比較困難。本人用Winsock和VB自制了一套聊天室和對(duì)話系統(tǒng),特拿來供同行們參考。
一·Winsock的主要屬性、事件和方法
Winsock是不可見控件,控件文件名是MSWINSCK.OCX,全稱為Mcirosoft winsock control,使用時(shí)要將此控件調(diào)入工具箱。
1·屬性:①Protocol=0 //使用TCP協(xié)議;
?、赗emoteHost //準(zhǔn)備連接遠(yuǎn)程機(jī)的IP地址
?、跼emotePort //連接遠(yuǎn)程機(jī)的IP端口號(hào) (1024—65535之間)
?、躄ocalPort //本地機(jī)監(jiān)聽I(yíng)P端口號(hào)必須與呼叫機(jī)端口號(hào)相同
2·方法:①connect //申請(qǐng)連接遠(yuǎn)程機(jī)
?、趌isten //設(shè)置監(jiān)聽
③accept //建立實(shí)際連接
?、躶enddata //發(fā)送數(shù)據(jù)
?、輌etdata //接收數(shù)據(jù)
?、辌lose //關(guān)閉連接
3·事件:①connectionrequest //一方請(qǐng)求連接時(shí)另一方產(chǎn)生
②connect //一方機(jī)接受連接時(shí)另一方產(chǎn)生
?、踓lose //一方機(jī)關(guān)閉連接時(shí)另一方產(chǎn)生
④dataArrival //一方發(fā)送數(shù)據(jù)另一方產(chǎn)生
?、輊rror //請(qǐng)求連接失敗時(shí)產(chǎn)生
二·制作方法
?、?在一工程中添加兩個(gè)表單form1(模擬客戶端)、form2(模擬服務(wù)器端)。
form1中裝入控件:
控件名
主要屬性
用 途
VB.Form form1
caption=”雷萌聊天室”
controlbox=0 ‘False
模擬客戶機(jī)表單
VB.Textbox text1
multiline=-1 ‘True
scrollbars=3 ‘Bath
用于輸入發(fā)往聊天室的信息
VB.Textbox text2
locked=-1 ‘True
multiline=-1 ‘True
scrollbars=3 ‘Bath
顯示從聊天室發(fā)來的信息
VB.Combobox combo1
text=”10.84.234.11” ‘任定默認(rèn)地址
放入常用的地址
VB.Commandbutton comm1
caption=”退出”
最小化form1
VB.Commandbutton comm2
caption=”連接”
請(qǐng)求與輸入的地址連接
VB.Commandbutton send
caption=”發(fā)送”
發(fā)送Text1中的內(nèi)容
VB.Label label1
caption=“請(qǐng)?jiān)诖溯斎氚l(fā)表的信息”
Text1的框標(biāo)
VB.Label label2
caption=“聊天室或?qū)Ψ降男畔?rdquo;
Text2的框標(biāo)
VB.Label label3
caption=”等待連接”
顯示連接狀態(tài)信息
VB.Label label4
caption=”聊天室或?qū)Ψ降刂?rdquo;
用于指示Combo1
VB.Label label5
caption=”操作:選地址連接,連接成功看到聊天室內(nèi)容后再輸信息發(fā)送”
操作說明
VB.Timer timer1
interval=6000; enabled=false
防止連接超時(shí)
MSWinsocklib.winsock a
用于數(shù)據(jù)傳輸
form2中裝入控件:
控件名
主要屬性
用 途
VB.Form form2
caption=”接收信息”
controlbox=0 ‘False
模擬客戶機(jī)表單
VB.Commandbutton command1
caption=”返回”
隱含F(xiàn)orm2窗口
VB.Commandbutton command2
caption=”對(duì)話”
點(diǎn)對(duì)點(diǎn)會(huì)話時(shí)用此直接啟動(dòng)Form1
VB.Textbox text1
locked=-1 ‘True
multiline=-1 ‘True
scrollbars=3 ‘Bath
存放聊天或?qū)υ拑?nèi)容
VB.Label label1
caption=”接收的信息”
Text1的框標(biāo)
MSWinsocklib.Winsock a
用于監(jiān)聽
MSWinsocklib.Winsock b
用于傳送聊天信息
⑵ 在Form1的各控件事件中加入如下代碼:
Dim flag As Boolean 注釋:連接狀態(tài)變量
Private Sub a_Connect()
flag = True
End Sub
Private Sub a_DataArrival(ByVal bytesTotal As Long)
Dim i As String
a.GetData i
Label3.Caption = "連接成功!"
Comm2.MousePointer = 0
Form1.MousePointer = 0
Timer1.Enabled = False
If i = Chr(0) Then
Text2.Text = "你是今天第一個(gè)進(jìn)入本聊天室的客戶。" + Chr(13) + Chr(10)
Else
Text2.Text = Text2.Text + i
End If
Text2.SelStart = Len(Text2.Text)
Send.MousePointer = 0
Combo1.Enabled = False
Comm2.Caption = "斷開連接"
Text1.SetFocus
End Sub
Private Sub a_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
flag = False
Timer1.Enabled = False
Comm2.MousePointer = 0
Form1.MousePointer = 0
MsgBox "網(wǎng)絡(luò)連接失敗 !"
Label3.Caption = "等待連接"
Combo1.Enabled = True
Combo1.SetFocus
a.Close
Comm2.Caption = "連接"
End Sub
Private Sub Comm1_Click()
a.Close 注釋:關(guān)閉連接
Form1.WindowState = 1
End Sub
Private Sub Comm2_Click()
If Comm2.Caption = "斷開連接" Then
a.Close
Comm2.Caption = "連接"
Label3.Caption = "等待連接"
Combo1.Enabled = True
Timer1.Enabled = False
Comm2.MousePointer = 0
Form1.MousePointer = 0
Else
Text2.Text = ""
Label3.Caption = "正在連接.."
Comm2.MousePointer = 11
Form1.MousePointer = 11
Timer1.Enabled = True
flag = False
a.Protocol = sckTCPProtocol
a.RemoteHost = Combo1.Text
a.RemotePort = 3000
a.Connect
End If
End Sub
Private Sub Form_DblClick()
If MsgBox("關(guān)閉本聊天室! 確認(rèn)嗎?", 36, "退出系統(tǒng)") = 6 Then
End
Else
Form1.WindowState = 1
End If
End Sub
Private Sub Form_Load()
If App.PrevInstance Then
MsgBox "本系統(tǒng)已經(jīng)加載,請(qǐng)看任務(wù)攔!", 48, "提示"
End
End If
flag = False
Load Form2 ‘讀入form2進(jìn)入監(jiān)聽
End Sub
Private Sub Send_Click()
Dim S As String
On Error GoTo ffff ‘防止鏈路中斷
Send.MousePointer = 11
If Right(Text1.Text, 1) <> Chr(10) Then
S = Text1.Text + Chr(13) + Chr(10)
Else
S = Text1.Text
End If
If flag Then
a.SendData S
End If
Exit Sub
ffff:
MsgBox "連接中斷!", 48, "提示"
a.Close
Send.MousePointer = 0
Comm2.Caption = "連接"
Label3.Caption = "等待連接"
Combo1.Enabled = True
Comm2.MousePointer = 0
Form1.MousePointer = 0
Exit Sub
End Sub
Private Sub Timer1_Timer()
flag = False
Timer1.Enabled = False
Comm2.MousePointer = 0
Form1.MousePointer = 0
MsgBox "網(wǎng)絡(luò)連接失敗(超時(shí)) !"
Label3.Caption = "等待連接"
Combo1.Enabled = True
Combo1.SetFocus
a.Close
Comm2.Caption = "連接"
End Sub
?、?在Form2的各控件事件中加入如下代碼:
Const maxn = 200 ‘最大同時(shí)連接本機(jī)的客戶數(shù)
Dim user(maxn) As Boolean
Private Sub Command1_Click()
Form2.Hide
End Sub
Private Sub Command2_Click()
Load Form1
Form1.Show
End Sub
Private Sub Form_Load()
Dim str1 As String
Form2.Caption = "雷萌通信軟件"
注釋:winsock控件 a 作為服務(wù)器程序監(jiān)聽
a.LocalPort = 3000
a.Listen
End Sub
Private Sub a_ConnectionRequest(ByVal requestID As Long)
Dim i As Long
For i = 1 To maxn ‘當(dāng)一客戶請(qǐng)求時(shí)給啟動(dòng)一Winsock控件標(biāo)志號(hào)
If Not user(i) Then
user(i) = True
Exit For
End If
Next i
If i > maxn Then
Exit Sub
End If
Load b(i) ‘當(dāng)一客戶請(qǐng)求時(shí)啟動(dòng)一Winsock控件
b(i).Accept requestID 注釋:實(shí)際建立連接
If Text1.Text = "" Then 注釋:發(fā)送數(shù)據(jù)
b(i).SendData Chr(0)
Else
b(i).SendData Text1.Text
End If
Form2.Show
End Sub
Private Sub s_Close(Index As Integer)
b(Index).Close 注釋:關(guān)閉連接
Unload b(Index) 注釋:卸載 一個(gè)WinSock 控件
user(Index) = False
End Sub
Private Sub b_DataArrival(Index As Integer, ByVal bytesTotal As Long)
Dim str As String
Dim i As Long
b(Index).GetData str
Text1.Text = Text1.Text + str
For i = 1 To maxn
If user(i) Then
b(i).SendData str
End If
Next i
End Sub
三·運(yùn)行
本程序在VB6.0中編譯通過,運(yùn)行后最小化到任務(wù)欄上,也可以用API的Shell_Notifyicon 函數(shù)做入右下角的指示器欄中常駐內(nèi)存。你可以在網(wǎng)絡(luò)中用一個(gè)固定的機(jī)器地址作為聊天討論室,其他用戶都選該機(jī)地址連接進(jìn)入該室聊天或討論。各用戶也可選各自熟悉的地址進(jìn)行連接對(duì)話,雙擊form1空白處從內(nèi)存中撤出系統(tǒng)。根據(jù)同樣的原理可以制作電子郵件系統(tǒng)。