| 网站首页 | 业界新闻 | 技术文章 | 视频教程 | 下载频道 | 程序源码 | 个人空间 | 编程论坛 |
 
| 技术教程首页 | 开发语言 | WEB开发 | .NET技术 | 数据库 | 操作系统 | 网页制作 |
 
 
您现在的位置: 编程中国 >> 技术教程 >> 开发语言 >> VB >> 编程实例 >> 正文
  ►  用Winsock制作一套聊天室和对话系统
用Winsock制作一套聊天室和对话系统
作者:佚名    阅读人次:……    文章来源:未知    发布时间:2005-12-28    网友评论()条
 





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 = "连接"

End Sub



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



上一页  [1] [2] [3] 下一页

 

 
文章录入:静夜思    责任编辑:静夜思 
  • 上一篇文章:

  • 下一篇文章:

  •  
    相关文章
    原创地带
    24小时热门帖子