📄 form1.vb
字号:
Imports System.Net.Sockets
Imports System.Text
Public Class Form1
Inherits System.Windows.Forms.Form
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu
#Region " Windows 窗体设计器生成的代码 "
Public Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
End Sub
'窗体重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
MyBase.Dispose(disposing)
End Sub
'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
Friend WithEvents Panel1 As System.Windows.Forms.Panel
Friend WithEvents TextBox3 As System.Windows.Forms.TextBox
Friend WithEvents Label3 As System.Windows.Forms.Label
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents Button3 As System.Windows.Forms.Button
Friend WithEvents Panel2 As System.Windows.Forms.Panel
Friend WithEvents Label4 As System.Windows.Forms.Label
Private Sub InitializeComponent()
Me.MainMenu1 = New System.Windows.Forms.MainMenu
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.Label1 = New System.Windows.Forms.Label
Me.Label2 = New System.Windows.Forms.Label
Me.TextBox2 = New System.Windows.Forms.TextBox
Me.Panel1 = New System.Windows.Forms.Panel
Me.TextBox3 = New System.Windows.Forms.TextBox
Me.Label3 = New System.Windows.Forms.Label
Me.Button2 = New System.Windows.Forms.Button
Me.Button3 = New System.Windows.Forms.Button
Me.Panel2 = New System.Windows.Forms.Panel
Me.Label4 = New System.Windows.Forms.Label
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(104, 40)
Me.TextBox1.Text = "192.168.0.100"
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(8, 43)
Me.Label1.Size = New System.Drawing.Size(96, 16)
Me.Label1.Text = "远程IP地址:"
Me.Label1.TextAlign = System.Drawing.ContentAlignment.TopRight
'
'Label2
'
Me.Label2.Location = New System.Drawing.Point(8, 67)
Me.Label2.Size = New System.Drawing.Size(96, 16)
Me.Label2.Text = "端口号:"
Me.Label2.TextAlign = System.Drawing.ContentAlignment.TopRight
'
'TextBox2
'
Me.TextBox2.Location = New System.Drawing.Point(104, 64)
Me.TextBox2.Text = "10200"
'
'Panel1
'
Me.Panel1.BackColor = System.Drawing.Color.Silver
Me.Panel1.Location = New System.Drawing.Point(16, 120)
Me.Panel1.Size = New System.Drawing.Size(208, 3)
'
'TextBox3
'
Me.TextBox3.Location = New System.Drawing.Point(16, 152)
Me.TextBox3.Size = New System.Drawing.Size(208, 21)
Me.TextBox3.Text = ""
'
'Label3
'
Me.Label3.Location = New System.Drawing.Point(16, 136)
Me.Label3.Text = "要发送的内容:"
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(112, 184)
Me.Button2.Size = New System.Drawing.Size(104, 24)
Me.Button2.Text = "连接/发送"
'
'Button3
'
Me.Button3.Location = New System.Drawing.Point(144, 224)
Me.Button3.Size = New System.Drawing.Size(72, 24)
Me.Button3.Text = "关闭"
'
'Panel2
'
Me.Panel2.BackColor = System.Drawing.Color.Gainsboro
Me.Panel2.Controls.Add(Me.Label4)
Me.Panel2.Controls.Add(Me.Label2)
Me.Panel2.Controls.Add(Me.TextBox1)
Me.Panel2.Controls.Add(Me.Label1)
Me.Panel2.Controls.Add(Me.TextBox2)
Me.Panel2.Location = New System.Drawing.Point(16, 8)
Me.Panel2.Size = New System.Drawing.Size(208, 100)
'
'Label4
'
Me.Label4.Font = New System.Drawing.Font("宋体", 9.0!, System.Drawing.FontStyle.Bold)
Me.Label4.Location = New System.Drawing.Point(8, 8)
Me.Label4.Text = "连接选项"
'
'Form1
'
Me.Controls.Add(Me.Panel2)
Me.Controls.Add(Me.Button3)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.TextBox3)
Me.Controls.Add(Me.Label3)
Me.Controls.Add(Me.Panel1)
Me.Menu = Me.MainMenu1
Me.MinimizeBox = False
Me.Text = "PPC Client"
End Sub
#End Region
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Me.Close()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim Client As TcpClient
Dim Buffer() As Byte
Dim SendData, rtndata As String
Dim InBuff(1000) As Byte
'创建TcpClient对象的实例
Client = New TcpClient
'通过计算机名称和端口号连接到指定的计算机
Client.Connect(System.Net.IPAddress.Parse(TextBox1.Text), CInt(TextBox2.Text))
SendData = TextBox3.Text
'按一定的编码规则对要传递的数据进行编码
Buffer = Encoding.Default.GetBytes(SendData)
'向已连接的服务程序发送数据
Client.GetStream().Write(Buffer, 0, Buffer.Length)
'DataAvailable 指示NetworkStream 上是否有可用的数据。如果可以在流上读取数据,则为 true;
'否则为 false,只要有数据存在就等待传输完毕
While Not Client.GetStream.DataAvailable()
Application.DoEvents()
End While
'接收由服务程序传递回客户端的数据并显示在对话框上
If Client.GetStream.DataAvailable() Then
Client.GetStream().Read(InBuff, 0, InBuff.Length)
rtndata = "服务程序已经成功收到指令,在" & Encoding.Default.GetString(InBuff, 0, InBuff.Length)
MsgBox(rtndata)
End If
'断开连接、关闭对象并释放与 TcpClient 关联的所有资源
Client.Close()
End Sub
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -