本程式轉貼自Visual Basic遊戲設計實務 位元文化 編著
若單機作業可將IP設成127.0.0.1
Chat.frm內容
語法:
VERSION 5.00
Object = "{248DD890-BB45-11CF-9ABC-0080C7E7B78D}#1.0#0"; "MSWINSCK.OCX"
Begin VB.Form frmChat
BackColor = &H00C0C0C0&
BorderStyle = 1 '單線固定
Caption = "一對一聊天室"
ClientHeight = 4770
ClientLeft = 45
ClientTop = 330
ClientWidth = 7185
LinkTopic = "Form1"
MaxButton = 0 'False
MinButton = 0 'False
ScaleHeight = 4770
ScaleWidth = 7185
StartUpPosition = 3 '系統預設值
Begin VB.CommandButton cmdClear
Caption = "清除"
Height = 375
Left = 6240
TabIndex = 4
Top = 3600
Width = 735
End
Begin VB.CommandButton cmdSend
Caption = "送出"
Height = 375
Left = 6240
TabIndex = 2
Top = 4080
Width = 735
End
Begin VB.TextBox txtMsg
BeginProperty Font
Name = "新細明體"
Size = 12
Charset = 136
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 375
Left = 360
TabIndex = 1
Top = 4080
Width = 5655
End
Begin VB.ListBox lstMsg
BeginProperty Font
Name = "新細明體"
Size = 9.75
Charset = 136
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 2985
Left = 360
TabIndex = 0
Top = 240
Width = 6495
End
Begin MSWinsockLib.Winsock Winsock1
Left = 0
Top = 0
_ExtentX = 741
_ExtentY = 741
_Version = 393216
End
Begin VB.Label lblName
AutoSize = -1 'True
BackColor = &H00C0C0C0&
Caption = "Name"
BeginProperty Font
Name = "新細明體"
Size = 12
Charset = 136
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 240
Left = 360
TabIndex = 3
Top = 3720
Width = 555
End
End
Attribute VB_Name = "frmChat"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Public sServerIP As String '對方主機的IP
Public sName As String '使用者暱稱
'********** 按下清除鍵 **********
Private Sub cmdClear_Click()
lstMsg.Clear
End Sub
'********** 送出訊息 **********
Private Sub cmdSend_Click()
Dim sSend As String
sSend = lblName.Caption & txtMsg.Text
lstMsg.AddItem sSend
If lstMsg.ListCount > 15 Then lstMsg.RemoveItem 0
Winsock1.SendData sSend
txtMsg.Text = ""
End Sub
'********** 連結 **********
Public Sub subConnect()
lblName.Caption = sName & ":"
lstMsg.Clear '清除資訊
cmdSend.Enabled = False '設定送出鍵為無效
If bIfServer = True Then '當做伺服端
With Winsock1
.LocalPort = 2469 '自訂值,但值要在1024以上
.Bind
.Listen
End With
lstMsg.AddItem "等待連結---"
frmChat.Caption = frmChat.Caption & "---伺服端"
Else '當做用戶端
With Winsock1
.RemoteHost = sServerIP
.RemotePort = 2469
.Connect
End With
lstMsg.AddItem "正在連結---"
frmChat.Caption = frmChat.Caption & "---用戶端"
End If
End Sub
'********** 斷線離開 **********
Private Sub Form_Unload(Cancel As Integer)
If Winsock1.State = sckConnected Then
Winsock1.SendData sName & "離線了!"
DoEvents
End If
End
End Sub
'********** 完成連結動作 **********
Private Sub Winsock1_Connect()
cmdSend.Enabled = True
Winsock1.SendData sName & "上線囉!"
End Sub
'********** 發生要求連結 **********
Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
Winsock1.Close
Winsock1.Accept requestID
Winsock1.SendData "已和" & sName & "連上了!"
cmdSend.Enabled = True
End Sub
'********** 當有資料送達時 **********
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim b As Boolean
Dim sMsgString As String
Winsock1.GetData sMsgString, vbString, bytesTotal
lstMsg.AddItem sMsgString
If lstMsg.ListCount > 15 Then lstMsg.RemoveItem 0
End Sub
'********** 按下 Enter 時 **********
Private Sub txtMsg_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then cmdSend_Click
End Sub