📄 form1.vb
字号:
Imports System.Net
Imports System.Net.Sockets
Imports System.IO
Imports System.Threading
Imports System.String
Public Class Form1
Inherits System.Windows.Forms.Form
Dim socket As socket
Dim logined As Boolean = False
#Region " Windows 窗体设计器生成的代码 "
Public Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
End Sub
'窗体重写处置以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
'注意:以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents Timer1 As System.Windows.Forms.Timer
Friend WithEvents TxtIDOrder As System.Windows.Forms.TextBox
Friend WithEvents Label3 As System.Windows.Forms.Label
Friend WithEvents TxtPassword As System.Windows.Forms.TextBox
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents BtExit As System.Windows.Forms.Button
Friend WithEvents BtLogin As System.Windows.Forms.Button
Friend WithEvents NotifyIcon1 As System.Windows.Forms.NotifyIcon
Friend WithEvents Label1 As System.Windows.Forms.Label
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container
Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
Me.TxtIDOrder = New System.Windows.Forms.TextBox
Me.Label3 = New System.Windows.Forms.Label
Me.TxtPassword = New System.Windows.Forms.TextBox
Me.Label2 = New System.Windows.Forms.Label
Me.BtExit = New System.Windows.Forms.Button
Me.BtLogin = New System.Windows.Forms.Button
Me.NotifyIcon1 = New System.Windows.Forms.NotifyIcon(Me.components)
Me.Label1 = New System.Windows.Forms.Label
Me.SuspendLayout()
'
'Timer1
'
Me.Timer1.Enabled = True
'
'TxtIDOrder
'
Me.TxtIDOrder.Location = New System.Drawing.Point(322, 168)
Me.TxtIDOrder.Name = "TxtIDOrder"
Me.TxtIDOrder.Size = New System.Drawing.Size(103, 21)
Me.TxtIDOrder.TabIndex = 35
Me.TxtIDOrder.Text = ""
'
'Label3
'
Me.Label3.Location = New System.Drawing.Point(206, 168)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(50, 32)
Me.Label3.TabIndex = 34
Me.Label3.Text = "编号:"
'
'TxtPassword
'
Me.TxtPassword.Location = New System.Drawing.Point(322, 240)
Me.TxtPassword.Name = "TxtPassword"
Me.TxtPassword.PasswordChar = Microsoft.VisualBasic.ChrW(42)
Me.TxtPassword.Size = New System.Drawing.Size(103, 21)
Me.TxtPassword.TabIndex = 33
Me.TxtPassword.Text = ""
'
'Label2
'
Me.Label2.Location = New System.Drawing.Point(206, 240)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(50, 24)
Me.Label2.TabIndex = 32
Me.Label2.Text = "密码:"
'
'BtExit
'
Me.BtExit.Location = New System.Drawing.Point(350, 320)
Me.BtExit.Name = "BtExit"
Me.BtExit.Size = New System.Drawing.Size(68, 32)
Me.BtExit.TabIndex = 31
Me.BtExit.Text = "退出"
'
'BtLogin
'
Me.BtLogin.Location = New System.Drawing.Point(233, 320)
Me.BtLogin.Name = "BtLogin"
Me.BtLogin.Size = New System.Drawing.Size(62, 32)
Me.BtLogin.TabIndex = 30
Me.BtLogin.Text = "登录"
'
'NotifyIcon1
'
Me.NotifyIcon1.Icon = CType(resources.GetObject("NotifyIcon1.Icon"), System.Drawing.Icon)
Me.NotifyIcon1.Text = "NotifyIcon1"
Me.NotifyIcon1.Visible = True
'
'Label1
'
Me.Label1.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(134, Byte))
Me.Label1.ForeColor = System.Drawing.SystemColors.Highlight
Me.Label1.Location = New System.Drawing.Point(184, 72)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(320, 24)
Me.Label1.TabIndex = 36
Me.Label1.Text = " 服务器与客户端通讯测试"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(678, 566)
Me.ControlBox = False
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.TxtIDOrder)
Me.Controls.Add(Me.TxtPassword)
Me.Controls.Add(Me.Label3)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.BtExit)
Me.Controls.Add(Me.BtLogin)
Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(134, Byte))
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None
Me.Name = "Form1"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "用户登录"
Me.ResumeLayout(False)
End Sub
#End Region
Sub establistsocket(ByVal state As Object)
Dim serveradd As IPAddress = Dns.Resolve("61.174.73.45").AddressList(0)
Dim enpoint As New IPEndPoint(serveradd, 50)
Dim temp As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
temp.Blocking = True '设置Socket对象处于阻塞状态
temp.Connect(enpoint) '连接服务器
socket = temp
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 5000)
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 5000)
If socket.Connected Then
Dim info As String
info = "L" & TxtIDOrder.Text.PadRight(6) & TxtPassword.Text.PadRight(10)
SendInfo(info) '发送登录信息
End If
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim ASCII As System.Text.Encoding = System.Text.Encoding.ASCII
Dim receivebyte(20) As Byte
Dim receivePage As String = String.Empty
If Not socket Is Nothing Then
If socket.Available <> 0 Then
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 1000)
Dim bytes As Int32 = socket.Receive(receivebyte, receivebyte.Length, 0) '接收二进制信息
receivePage = ASCII.GetString(receivebyte, 0, bytes) '转换为字符
ProcessInfo(receivePage)
End If
End If
End Sub
Public Sub ProcessInfo(ByVal info As String)
Dim flag As String
flag = info.Substring(0, 2)
Select Case flag
Case "MM"
MsgBox(info.Substring(2, Len(info) - 2))
Case "L0"
MsgBox("编号或密码输入有误,请重试!", MsgBoxStyle.Exclamation, Me.Text)
Case "L1"
MsgBox("恭喜你,登录成功!")
Me.Hide()
Case "L2"
MsgBox("你的剩余机时小于0,登录失败!", MsgBoxStyle.Exclamation, Me.Text)
Case "E0"
MsgBox("编号或密码输入有误,请重试!", MsgBoxStyle.Exclamation, Me.Text)
Case "E1"
MsgBox("恭喜你,退出成功!")
Me.Close()
Case "E2"
MsgBox("你的剩余机时小于0,请在下次上机前续机时,退出成功!", , Me.Text)
Me.Close()
End Select
End Sub
Private Sub BtLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtLogin.Click
If logined = True Then
Dim info As String
info = "L" & TxtIDOrder.Text.PadRight(6) & TxtPassword.Text.PadRight(10)
SendInfo(info)
Else
ThreadPool.QueueUserWorkItem(New System.Threading.WaitCallback(AddressOf establistsocket))
End If
End Sub
Function SendInfo(ByVal info As String)
Dim ASCII As System.Text.Encoding = System.Text.Encoding.ASCII
Dim byteSend As Byte()
byteSend = ASCII.GetBytes(info)
socket.Send(byteSend, byteSend.Length, 1)
End Function
Private Sub BtExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtExit.Click
Dim info As String
info = "E" & TxtIDOrder.Text.PadRight(6) & TxtPassword.Text.PadRight(10)
SendInfo(info)
End Sub
Private Sub NotifyIcon1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles NotifyIcon1.DoubleClick
Me.Show()
End Sub
Private Sub NotifyIcon1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseDown
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -