论坛: 菜鸟乐园 标题: VB聊天程序 复制本贴地址    
作者: guxin [guxin]    论坛用户   登录
出处:Http://www.xtnet.net.cn
好几天没写什么东西给大家看了,今天地没什么好写的(技术也没有多高,只给写几个烂程序,大家就这样看吧)就写一个简单的聊天程序吧。不过大家可以改成一个远程控制程序,一会就让大家知道了,好了不多说了!以后用我的习惯,直接写出代码,慢慢看啊!!
Private Sub cmd_connect_Click()
    连接
    SockClient.RemoteHost = txt_ip.Text
    SockClient.Connect
End Sub

Private Sub cmd_quit_Click()
    退出
  SockClient.Close
    End
End Sub
Private Sub cmd_send_Click()
    '发送文本
    Dim MySendData As String
    MySendData = txt_send.Text
    SockClient.SendData MySendData
    txt_send.Text = ""
End Sub

Private Sub cmd_end_Click()
    退出
    SockClient.SendData "quit"
End Sub

Private Sub Form_Load()
    '服务器开始监听
    SockServer.Listen

End Sub

Private Sub SockClient_Close()
    '激活连接按钮
    cmd_connect.Enabled = True
    txt_send.Text = ""
    txt_get.Text = ""
End Sub

Private Sub SockClient_Connect()
    MsgBox "连接成功", vbInformation, "ok!"
    '使连接按钮无效,避免错误
 
    cmd_connect.Enabled = False
End Sub

Private Sub SockClient_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)
    '出错后关闭
    SockClient.Close
End Sub

Private Sub SockServer_Close()
    cmd_connect.Enabled = True
End Sub

Private Sub SockServer_ConnectionRequest(ByVal requestID As Long)
    '判断服务器不关闭时接受连接请求
    If SockServer.State <> sckClosed Then SockServer.Close
        SockServer.Accept requestID
End Sub

Private Sub SockServer_DataArrival(ByVal bytesTotal As Long)
    Dim MyGetData As String
    '得到数据
    SockServer.GetData MyGetData
    If MyGetData = "quit" Then
        SockServer.Close
        SockServer.Listen
        End
    End If
    txt_get.Text = txt_get.Text & Chr(13) & Chr(10) & Time & MyGetData
End Sub
Private Sub Timer1_Timer()
    '不断的监控服务器和客户端状态
    Select Case SockClient.State
        Case 0: StatusBar1.Panels(2) = "客户端关闭"
        Case 1: StatusBar1.Panels(2) = "客户端打开"
        Case 7: StatusBar1.Panels(2) = "客户端已经连接"
    End Select
    Select Case SockServer.State
        Case 0: StatusBar1.Panels(1) = "服务器关闭"
        Case 2: StatusBar1.Panels(1) = "服务器侦听"
        Case 7: StatusBar1.Panels(1) = "服务器已经连接"
    End Select
End Sub
Private Sub txt_send_KeyPress(KeyAscii As Integer)
    '发现按下回车键时就发送文本
    If KeyAscii = 13 Then
    cmd_send_Click
End If
End Sub
不知道大家有没有看到这段
If MyGetData = "quit" Then
        SockServer.Close
        SockServer.Listen
        End
End If
  向对方发送quit指令,就会退出聊天程序,但是当我们发送如果不是quit而是别的呢?比如说是"重起"."关机"......哈哈!!大家想到了吧,怎么做就是大家的事了,你们就看这办吧!不懂的大家可以交流一下。




地主 发表时间: 05-10-25 08:50

论坛: 菜鸟乐园

20CN网络安全小组版权所有
Copyright © 2000-2010 20CN Security Group. All Rights Reserved.
论坛程序编写:NetDemon

粤ICP备05087286号