⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 client1.frm

📁 网络聊天软件
💻 FRM
字号:
VERSION 5.00
Object = "{248DD890-BB45-11CF-9ABC-0080C7E7B78D}#1.0#0"; "mswinsck.ocx"
Begin VB.Form Form1 
   Caption         =   "Client1"
   ClientHeight    =   5385
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   9690
   LinkTopic       =   "Form1"
   ScaleHeight     =   5385
   ScaleWidth      =   9690
   StartUpPosition =   3  '窗口缺省
   Begin MSWinsockLib.Winsock Winsock1 
      Left            =   9000
      Top             =   4560
      _ExtentX        =   741
      _ExtentY        =   741
      _Version        =   393216
      Protocol        =   1
   End
   Begin VB.CommandButton cmdexit 
      Caption         =   "exit"
      Height          =   375
      Left            =   7800
      TabIndex        =   12
      Top             =   4920
      Width           =   855
   End
   Begin VB.CommandButton cmddatasend 
      Caption         =   "send"
      Height          =   375
      Left            =   6360
      TabIndex        =   11
      Top             =   4560
      Width           =   855
   End
   Begin VB.CommandButton cmdconnnect 
      Caption         =   "connect"
      Height          =   375
      Left            =   7800
      TabIndex        =   10
      Top             =   4320
      Width           =   855
   End
   Begin VB.TextBox txtdata_send 
      Height          =   495
      Left            =   240
      TabIndex        =   9
      Top             =   4440
      Width           =   5895
   End
   Begin VB.TextBox txtlocal_port 
      Height          =   495
      Left            =   6720
      TabIndex        =   8
      Top             =   3600
      Width           =   2295
   End
   Begin VB.TextBox txtlocal_ip 
      Height          =   495
      Left            =   6720
      TabIndex        =   6
      Top             =   2640
      Width           =   2295
   End
   Begin VB.TextBox txtremote_port 
      Height          =   495
      Left            =   6720
      TabIndex        =   4
      Top             =   1680
      Width           =   2295
   End
   Begin VB.TextBox txtremote_ip 
      Height          =   495
      Left            =   6720
      TabIndex        =   2
      Top             =   600
      Width           =   2295
   End
   Begin VB.Label Label5 
      Caption         =   "Local_port:"
      BeginProperty Font 
         Name            =   "宋体"
         Size            =   14.25
         Charset         =   134
         Weight          =   700
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      ForeColor       =   &H00000000&
      Height          =   375
      Left            =   6480
      TabIndex        =   7
      Top             =   3240
      Width           =   1935
   End
   Begin VB.Label Label4 
      Caption         =   "Local_ip:"
      BeginProperty Font 
         Name            =   "宋体"
         Size            =   14.25
         Charset         =   134
         Weight          =   700
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      ForeColor       =   &H00000000&
      Height          =   375
      Left            =   6480
      TabIndex        =   5
      Top             =   2280
      Width           =   1695
   End
   Begin VB.Label Label3 
      Caption         =   "Remote_port:"
      BeginProperty Font 
         Name            =   "宋体"
         Size            =   14.25
         Charset         =   134
         Weight          =   700
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      ForeColor       =   &H00000000&
      Height          =   375
      Left            =   6480
      TabIndex        =   3
      Top             =   1200
      Width           =   2175
   End
   Begin VB.Label Label2 
      Caption         =   "Remote_ip:"
      BeginProperty Font 
         Name            =   "宋体"
         Size            =   14.25
         Charset         =   134
         Weight          =   700
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      ForeColor       =   &H00000000&
      Height          =   375
      Left            =   6480
      TabIndex        =   1
      Top             =   120
      Width           =   1695
   End
   Begin VB.Label Label1 
      BackColor       =   &H00000000&
      ForeColor       =   &H00FFFFFF&
      Height          =   3735
      Left            =   120
      TabIndex        =   0
      Top             =   240
      Width           =   6015
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Dim data As String              '要发送的数据或接收到的数据
Dim old_string As String        '原来接受到的数据
Dim remote_ip As String         '发送方ip
Dim remote_port As String       '发送方port
Dim real_data As String         '接受到真正数据


Private Sub cmdconnnect_Click()
data = txtlocal_ip.Text & "@" & txtlocal_port.Text & "@" & "request connect!"     '发送连接请求
Winsock1.SendData data
End Sub

Private Sub cmddatasend_Click()
data = txtlocal_ip.Text & "@" & txtlocal_port & "@" & txtdata_send.Text
old_string = old_string & txtlocal_ip.Text & "发出信息:" & txtdata_send.Text & Chr(13)
Label1.Caption = old_string
Winsock1.SendData data
End Sub


Private Sub cmdexit_Click()
Dim quit As Integer
quit = MsgBox("您确定要退出吗?", 4 + 32 + 256)
If quit = 6 Then
  With Winsock1
     .RemoteHost = txtremote_ip.Text
     .RemotePort = Val(txtremote_port.Text)
     '.Bind Val(txtlocal_port.Text), txtlocal_ip.Text
  End With
  data = txtlocal_ip.Text & "@" & txtlocal_port.Text & "@" & "request disconnect!"    '退出时发出请求断开连接的请求(服务器不给断开原来给分配的winsock)
  Winsock1.SendData data
  Winsock1.Close
  End
End If
End Sub

Private Sub Form_Load()
data = ""
txtremote_ip.Text = "127.0.0.1"        '初始化ip和port
txtremote_port.Text = "1000"
txtlocal_ip.Text = "127.0.0.1"
txtlocal_port.Text = "1001"
cmddatasend.Enabled = False
If App.PrevInstance = True Then        '初始化winsock控件
   End
End If
With Winsock1
    .RemoteHost = txtremote_ip.Text
    .RemotePort = Val(txtremote_port.Text)
    .Bind Val(txtlocal_port.Text), txtlocal_ip.Text
End With
End Sub



Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim temp As String
Dim i As Integer
Dim j As Integer
Dim k As Integer
Winsock1.GetData data

For i = 1 To Len(data)                       '求出发送方ip
   temp = Mid(data, i, 1)
   If temp <> "@" Then
       remote_ip = remote_ip & temp
   Else
      Exit For
   End If
Next i

For j = i + 1 To Len(data)                   '求出发送方port
   temp = Mid(data, j, 1)
   If temp <> "@" Then
      remote_port = remote_port & temp
   Else
      Exit For
   End If
Next j

For k = j + 1 To Len(data)                   '求出发送方发来的真正数据
   temp = Mid(data, k, 1)
   If temp <> "@" Then
      real_data = real_data & temp
   Else
      Exit For
   End If
Next k

If real_data = "OK!" Then                     '提示与服务器连接成功
   cmddatasend.Enabled = True
   old_string = old_string & "Connect completed!" & Chr(13)
   Label1.Caption = old_string
   cmdconnnect.Enabled = False
   With Winsock1
       .RemoteHost = remote_ip
       .RemotePort = Val(remote_port)
       '.Bind Val(txtlocal_port.Text), txtlocal_ip.Text
   End With
   remote_ip = ""
   remote_port = ""
   real_data = ""
Else
   old_string = old_string & remote_ip & "发来信息:" & real_data & Chr(13)
   Label1.Caption = old_string
   remote_ip = ""
   remote_port = ""
   real_data = ""
End If

End Sub

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -