VERSION 5.00
Object = "{248DD890-BB45-11CF-9ABC-0080C7E7B78D}#1.0#0"; "MSWINSCK.OCX"
Begin VB.Form Form1 
   Caption         =   "网络聊天器"
   ClientHeight    =   3825
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   3990
   LinkTopic       =   "Form1"
   ScaleHeight     =   3825
   ScaleWidth      =   3990
   StartUpPosition =   3  '窗口缺省
   Begin MSWinsockLib.Winsock sockSer 
      Left            =   2145
      Top             =   2475
      _ExtentX        =   741
      _ExtentY        =   741
      _Version        =   393216
      RemotePort      =   1001
      LocalPort       =   1002
   End
   Begin MSWinsockLib.Winsock sockClient 
      Left            =   1095
      Top             =   2460
      _ExtentX        =   741
      _ExtentY        =   741
      _Version        =   393216
      RemotePort      =   1002
      LocalPort       =   1001
   End
   Begin VB.ListBox lstRecord 
      Height          =   1320
      Left            =   105
      TabIndex        =   6
      Top             =   1905
      Width           =   3735
   End
   Begin VB.CommandButton cmdExit 
      Caption         =   "退  出"
      Height          =   375
      Left            =   2760
      TabIndex        =   5
      Top             =   3360
      Width           =   1095
   End
   Begin VB.CommandButton cmdClose 
      Caption         =   "断开链接"
      Height          =   375
      Left            =   1440
      TabIndex        =   4
      Top             =   3360
      Width           =   1095
   End
   Begin VB.CommandButton cmdSend 
      Caption         =   "发送消息"
      Height          =   375
      Left            =   120
      TabIndex        =   3
      Top             =   3360
      Width           =   1095
   End
   Begin VB.CommandButton cmdConnect 
      Caption         =   "连 接"
      Height          =   375
      Left            =   3000
      TabIndex        =   2
      Top             =   240
      Width           =   855
   End
   Begin VB.TextBox txtIP 
      Height          =   270
      Left            =   1095
      TabIndex        =   1
      Text            =   "127.0.0.1"
      Top             =   240
      Width           =   1695
   End
   Begin VB.TextBox txtSend 
      Height          =   735
      Left            =   120
      MultiLine       =   -1  'True
      ScrollBars      =   2  'Vertical
      TabIndex        =   0
      Top             =   870
      Width           =   3735
   End
   Begin VB.Label Label3 
      Caption         =   "消息记录："
      Height          =   225
      Left            =   120
      TabIndex        =   9
      Top             =   1665
      Width           =   975
   End
   Begin VB.Label Label2 
      Caption         =   "发送消息："
      Height          =   225
      Left            =   105
      TabIndex        =   8
      Top             =   630
      Width           =   975
   End
   Begin VB.Label Label1 
      Caption         =   "计算机："
      Height          =   255
      Left            =   330
      TabIndex        =   7
      Top             =   270
      Width           =   780
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Sub cmdClose_Click()
    sockClient.SendData "^Close^"   '发送预先约好的关闭代码
End Sub

Private Sub cmdConnect_Click()
    sockClient.RemoteHost = txtIP.Text  '为客户机设置远程主机的主机名或IP
    sockClient.Connect          '连接远程主机
    cmdSend.Enabled = True      '激活按钮
    cmdClose.Enabled = True
    cmdConnect.Enabled = False
End Sub

Private Sub cmdExit_Click()
    '检查Winsock控件是否关闭连接，如果没有，则关闭，然后退出程序
    If sockClient.State <> sckClosed Then sockClient.Close
    If sockSer.State <> sckClosed Then sockSer.Close
    End
End Sub

Private Sub cmdSend_Click()
    Dim strSend As String
    strSend = sockClient.LocalHostName + ":" + txtSend.Text
    sockClient.SendData strSend     '发送字符串
    lstRecord.AddItem strSend       '将发送的信息添加到记录列表
    lstRecord.TopIndex = lstRecord.NewIndex '将新记录置于顶端
End Sub

Private Sub Form_Load()
    cmdSend.Enabled = False     '为防止出错而使按钮不可用
    cmdClose.Enabled = False
    sockSer.Listen              '打开服务器进行侦听
End Sub

Private Sub sockClient_Close()
    cmdConnect.Enabled = True
    sockClient.Close        '关闭客户端
    txtSend.Text = ""

End Sub

Private Sub sockClient_Connect()
    MsgBox "连接成功!", vbInformation, "提示"
    cmdConnect.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)
    MsgBox "连接出错！关闭连接！", vbInformation, "警告"
    sockClient.Close
End Sub

Private Sub sockSer_ConnectionRequest(ByVal requestID As Long)
    If sockSer.State <> sckClosed Then sockSer.Close '关闭侦听
    sockSer.Accept requestID    '服务器接收请求
    
End Sub

Private Sub sockSer_DataArrival(ByVal bytesTotal As Long)
    Dim strGet As String
    sockSer.GetData strGet      '服务器获取数据
    If strGet = "^Close^" Then  '如果收到预定的关闭信号
        sockSer.Close
        sockSer.Listen
    End If
    lstRecord.AddItem strGet    '将收到的数据添加到记录列表
    lstRecord.TopIndex = lstRecord.NewIndex
    
End Sub

