Visual Basic 6.0 通过WinSock控件实现TCP/IP通讯

项目上有可能用到电脑间的数据传输,找了下网络资料,自己做了个程序测试一下。

可以选择作为服务器或者客户端。

VERSION 5.00
Object = "{248DD890-BB45-11CF-9ABC-0080C7E7B78D}#1.0#0"; "mswinsck.ocx"
Begin VB.Form Form1 
   Caption         =   "Form1"
   ClientHeight    =   6405
   ClientLeft      =   120
   ClientTop       =   450
   ClientWidth     =   11355
   LinkTopic       =   "Form1"
   ScaleHeight     =   6405
   ScaleWidth      =   11355
   StartUpPosition =   3  '窗口缺省
   Begin VB.TextBox Text2 
      Height          =   375
      Left            =   6480
      TabIndex        =   7
      Text            =   "7988"
      Top             =   4320
      Width           =   3495
   End
   Begin VB.TextBox Text1 
      Enabled         =   0   'False
      Height          =   375
      Left            =   6480
      TabIndex        =   6
      Text            =   "192.168.31.24"
      Top             =   3840
      Width           =   3495
   End
   Begin VB.TextBox txtoutput 
      Height          =   375
      Left            =   2880
      TabIndex        =   5
      Text            =   "Text1"
      Top             =   1560
      Width           =   4935
   End
   Begin VB.TextBox txtsenddata 
      Height          =   375
      Left            =   2880
      TabIndex        =   4
      Text            =   "Text1"
      Top             =   960
      Width           =   4935
   End
   Begin VB.Timer Timer1 
      Interval        =   500
      Left            =   3120
      Top             =   5160
   End
   Begin VB.CommandButton Command1 
      Caption         =   "Command1"
      Height          =   615
      Left            =   8520
      TabIndex        =   2
      Top             =   2400
      Width           =   1695
   End
   Begin MSWinsockLib.Winsock Tcpserver 
      Left            =   6720
      Top             =   5160
      _ExtentX        =   741
      _ExtentY        =   741
      _Version        =   393216
   End
   Begin VB.OptionButton Option2 
      Caption         =   "客户端"
      Height          =   255
      Left            =   8640
      TabIndex        =   1
      Top             =   960
      Width           =   1695
   End
   Begin VB.OptionButton Option1 
      Caption         =   "服务器"
      Height          =   255
      Left            =   8640
      TabIndex        =   0
      Top             =   600
      Value           =   -1  'True
      Width           =   1695
   End
   Begin VB.Label Label1 
      Caption         =   "Label1"
      Height          =   495
      Left            =   720
      TabIndex        =   3
      Top             =   3000
      Width           =   2535
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Sub Command1_Click()

If Option1.Value Then
    Tcpserver.Close
    Tcpserver.LocalPort = CInt(Val(Text2.Text)) '设置侦听端口
    Tcpserver.Listen               '侦听
ElseIf Option2.Value Then
    Tcpserver.Close
    Tcpserver.RemoteHost = Trim(Text1.Text) '设置服务器IP
    Tcpserver.RemotePort = CInt(Val(Text2.Text)) '设置服务器端口
    Tcpserver.Connect               '连接
End If
End Sub

Private Sub Form_Load()
    Tcpserver.Close
End Sub

Private Sub Option1_Click()
Text1.Enabled = Option2.Value
End Sub

Private Sub Option2_Click()
Text1.Enabled = Option2.Value
End Sub

Private Sub Tcpserver_ConnectionRequest(ByVal requestID As Long)
If Tcpserver.State <> sckClosed Then Tcpserver.Close
Tcpserver.Accept requestID
End Sub


Private Sub txtsenddata_change()

Tcpserver.SendData txtsenddata.Text

End Sub

Private Sub tcpserver_DataArrival(ByVal bytesTotal As Long)

Dim strData As String

Tcpserver.GetData strData

txtoutput.Text = strData
End Sub

Private Sub Timer1_Timer()
Select Case Tcpserver.State
    Case 0: Label1.Caption = "缺省的,关闭"
    Case 1: Label1.Caption = "打开"
    Case 2: Label1.Caption = "侦听"
    Case 3: Label1.Caption = "连接挂起"
    Case 4: Label1.Caption = "识别主机"
    Case 5: Label1.Caption = "已识别主机"
    Case 6: Label1.Caption = "正在连接"
    Case 7: Label1.Caption = "已经连接"
    Case 8
        Label1.Caption = "同级人员正在关闭连接"
        Tcpserver.Close
        Command1_Click
    Case 9: Label1.Caption = "错误"
End Select

End Sub

 

版权声明:本文内容来源于网络搜集无法获知原创作者,仅供个人学习用途,若侵犯到您的权益请联系我们及时删除。邮箱:1370723259@qq.com

发表评论