VB6.0 通过WinSock控件实现TCP/IP通讯
项目上有可能用到电脑间的数据传输,找了下网络资料,自己做了个程序测试一下。
可以选择作为服务器或者客户端。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
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 |
坐等大佬D盘更新,祝大佬全家福寿安康
有公众号之类的吗,之前这个网站打不开了
[…] 关于使用Change…
[…] 关于使用Functi…
谢谢提醒,已删 :)