用VB實現聊天討論室和點對點會話

來源:互聯網
上載者:User

在一個單位內部或通過廣域協議(如X.25)互聯的行業內部都有幾十或上萬台電腦互聯,用Intranet雖然可以建立聊天室,但實現點對點即時對話卻比較困難。本人用Winsock和VB自製了一套聊天室和對話系統,特拿來供同行們參考。

一·Winsock的主要屬性、事件和方法

Winsock是不可見控制項,控制項檔案名稱是MSWINSCK.OCX,全稱為Mcirosoft winsock control,使用時要將此控制項調入工具箱。

1·屬性:①Protocol=0 //使用TCP協議;

②RemoteHost //準備串連遠程機的IP地址

③RemotePort //串連遠程機的IP連接埠號碼 (1024—65535之間)

④LocalPort //本地機監聽IP連接埠號碼必須與呼叫機連接埠號碼相同

2·方法:①connect //申請串連遠程機

②listen //設定監聽

③accept //建立實際串連

④senddata //發送資料

⑤getdata //接收資料

⑥close //關閉串連

3·事件:①connectionrequest //一方請求串連時另一方產生

②connect //一方機接受串連時另一方產生

③close //一方機關閉串連時另一方產生

④dataArrival //一方發送資料另一方產生

⑤error //請求串連失敗時產生

二·製作方法

⑴ 在一工程中添加兩個表單form1(類比用戶端)、form2(類比伺服器端)。

form1中裝入控制項:

控制項名
主要屬性
用 途

VB.Form form1
caption=”雷萌聊天室”

controlbox=0 ‘False
類比客戶機表單

VB.Textbox text1
multiline=-1 ‘True

scrollbars=3 ‘Bath
用於輸入發往聊天室的資訊

VB.Textbox text2
locked=-1 ‘True

multiline=-1 ‘True

scrollbars=3 ‘Bath
顯示從聊天室發來的資訊

VB.Combobox combo1
text=”10.84.234.11” ‘任定預設地址
放入常用的地址

VB.Commandbutton comm1 
caption=”退出”
最小化form1

VB.Commandbutton comm2 
caption=”串連”
請求與輸入的地址串連

VB.Commandbutton send 
caption=”發送”
發送Text1中的內容

VB.Label label1
caption=“請在此輸入發表的資訊”
Text1的框標

VB.Label label2
caption=“聊天室或對方的資訊”
Text2的框標

VB.Label label3
caption=”等待串連”
顯示串連狀態資訊

VB.Label label4
caption=”聊天室或對方地址”
用於指示Combo1

VB.Label label5
caption=”操作:選地址串連,串連成功看到聊天室內容後再輸資訊發送”
操作說明

VB.Timer timer1
interval=6000; enabled=false
防止連線逾時

MSWinsocklib.winsock a

用於資料轉送

form2中裝入控制項:

控制項名
主要屬性
用 途

VB.Form form2
caption=”接收資訊”

controlbox=0 ‘False
類比客戶機表單

VB.Commandbutton command1
caption=”返回”
隱含Form2視窗

VB.Commandbutton command2
caption=”對話”
點對點會話時用此直接啟動Form1

VB.Textbox text1
locked=-1 ‘True

multiline=-1 ‘True

scrollbars=3 ‘Bath
存放聊天或對話內容

VB.Label label1
caption=”接收的資訊”
Text1的框標

MSWinsocklib.Winsock a

用於監聽

MSWinsocklib.Winsock b

用於傳送聊天資訊

⑵ 在Form1的各控制項事件中加入如下代碼:

Dim flag As Boolean 注釋:串連狀態變數

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 = "你是今天第一個進入本聊天室的客戶。" + 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 "網路連接失敗 !"

Label3.Caption = "等待串連"

Combo1.Enabled = True

Combo1.SetFocus

a.Close

Comm2.Caption = "串連" 
 

Private Sub Comm1_Click()

a.Close 注釋:關閉串連

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("關閉本聊天室! 確認嗎?", 36, "退出系統") = 6 Then

End

Else

Form1.WindowState = 1

End If

End Sub

Private Sub Form_Load()

If App.PrevInstance Then

MsgBox "本系統已經載入,請看任務攔!", 48, "提示"

End

End If

flag = False

Load Form2 ‘讀入form2進入監聽

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 "網路連接失敗(逾時) !"

Label3.Caption = "等待串連"

Combo1.Enabled = True

Combo1.SetFocus

a.Close

Comm2.Caption = "串連"

End Sub

⑶ 在Form2的各控制項事件中加入如下代碼:

Const maxn = 200 ‘最大同時串連原生客戶數

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 作為伺服器程式監聽

a.LocalPort = 3000

a.Listen

End Sub

Private Sub a_ConnectionRequest(ByVal requestID As Long)

Dim i As Long

For i = 1 To maxn ‘當一客戶請求時給啟動一Winsock控制項標誌號

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) ‘當一客戶請求時啟動一Winsock控制項

b(i).Accept requestID 注釋:實際建立串連

If Text1.Text = "" Then 注釋:發送資料

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 注釋:關閉串連

Unload b(Index) 注釋:卸載 一個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

三·運行

本程式在VB6.0中編譯通過,運行後最小化到工作列上,也可以用API的Shell_Notifyicon 函數做入右下角的指標欄中常駐記憶體。你可以在網路中用一個固定的機器地址作為聊天討論室,其他使用者都選該機地址串連進入該室聊天或討論。各使用者也可選各自熟悉的地址進行串連對話,雙擊form1空白處從記憶體中撤出系統。根據同樣的原理可以製作電子郵件系統。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.