📄 frmmain.vb
字号:
Me.Enabled = CType(resources.GetObject("$this.Enabled"), Boolean)
Me.Font = CType(resources.GetObject("$this.Font"), System.Drawing.Font)
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
Me.ImeMode = CType(resources.GetObject("$this.ImeMode"), System.Windows.Forms.ImeMode)
Me.Location = CType(resources.GetObject("$this.Location"), System.Drawing.Point)
Me.MaximizeBox = False
Me.MaximumSize = CType(resources.GetObject("$this.MaximumSize"), System.Drawing.Size)
Me.Menu = Me.mnuMain
Me.MinimumSize = CType(resources.GetObject("$this.MinimumSize"), System.Drawing.Size)
Me.Name = "frmMain"
Me.RightToLeft = CType(resources.GetObject("$this.RightToLeft"), System.Windows.Forms.RightToLeft)
Me.StartPosition = CType(resources.GetObject("$this.StartPosition"), System.Windows.Forms.FormStartPosition)
Me.Text = resources.GetString("$this.Text")
Me.Visible = CType(resources.GetObject("$this.Visible"), Boolean)
Me.TabControl1.ResumeLayout(False)
Me.tpChatPage.ResumeLayout(False)
Me.tpListUsers.ResumeLayout(False)
Me.ResumeLayout(False)
End Sub
#End Region
#Region " Standard Menu Code "
' <System.Diagnostics.DebuggerStepThrough()> has been added to some procedures since they are
' not the focus of the demo. Remove them if you wish to debug the procedures.
' This code simply shows the About form.
<System.Diagnostics.DebuggerStepThrough()> Private Sub mnuAbout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuAbout.Click
' Open the About form in Dialog Mode
Dim frm As New frmAbout()
frm.ShowDialog(Me)
frm.Dispose()
End Sub
' This code will close the form.
<System.Diagnostics.DebuggerStepThrough()> Private Sub mnuExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuExit.Click
' Close the current form
Me.Close()
End Sub
#End Region
Const READ_BUFFER_SIZE As Integer = 255
Const PORT_NUM As Integer = 10000
Private client As TcpClient
Private readBuffer(READ_BUFFER_SIZE) As Byte
' Pop up a Connect user dialog and send a message requesting user to log in to chat.
Sub AttemptLogin()
Dim frmConnectUser As New frmConnectUser()
frmConnectUser.StartPosition = FormStartPosition.CenterParent
frmConnectUser.ShowDialog(Me)
SendData("CONNECT|" & frmConnectUser.txtUserLogin.Text)
frmConnectUser.Dispose()
End Sub
' Clear the Users listbox, and request the server to send the list of users.
Private Sub btnListUsers_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnListUsers.Click
lstUsers.Items.Clear()
SendData("REQUESTUSERS")
End Sub
' Send the contents of the Send textbox if it isn't blank.
Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
If txtSend.Text <> "" Then
DisplayText("You say: " & txtSend.Text & Chr(13) & Chr(10))
SendData("CHAT|" & txtSend.Text)
txtSend.Text = ""
End If
End Sub
' Writes text to the output textbox.
Private Sub DisplayText(ByVal text As String)
txtDisplay.AppendText(text)
End Sub
' This is the callback function for TcpClient.GetStream.Begin to get an
' asynchronous read.
Private Sub DoRead(ByVal ar As IAsyncResult)
Dim BytesRead As Integer
Dim strMessage As String
Try
' Finish asynchronous read into readBuffer and return number of bytes read.
BytesRead = client.GetStream.EndRead(ar)
If BytesRead < 1 Then
' If no bytes were read server has close. Disable input window.
MarkAsDisconnected()
Exit Sub
End If
' Convert the byte array the message was saved into, minus two for the
' Chr(13) and Chr(10)
strMessage = Encoding.ASCII.GetString(readBuffer, 0, BytesRead - 2)
ProcessCommands(strMessage)
' Start a new asynchronous read into readBuffer.
client.GetStream.BeginRead(readBuffer, 0, READ_BUFFER_SIZE, AddressOf DoRead, Nothing)
Catch e As Exception
MarkAsDisconnected()
End Try
End Sub
' Send the server a disconnect message
Private Sub frmMain_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
' Send only if server is still running.
If btnSend.Enabled = True Then
SendData("DISCONNECT")
End If
End Sub
' When the form starts, this subroutine will connect to the server and attempt to
' log in.
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim frmConnectUser As New frmConnectUser()
Try
' The TcpClient is a subclass of Socket, providing higher level
' functionality like streaming.
client = New TcpClient("127.0.0.1", PORT_NUM)
' Start an asynchronous read invoking DoRead to avoid lagging the user
' interface.
client.GetStream.BeginRead(readBuffer, 0, READ_BUFFER_SIZE, AddressOf DoRead, Nothing)
' Make sure the window is showing before popping up connection dialog.
Me.Show()
AttemptLogin()
Catch Ex As Exception
MsgBox("Server is not active. Please start server and try again.", _
MsgBoxStyle.Exclamation, Me.Text)
Me.Dispose()
End Try
End Sub
' This subroutine adds a list of users to listbox.
Private Sub ListUsers(ByVal users() As String)
Dim I As Integer
For I = 1 To users.Length - 1
lstUsers.Items.Add(users(I))
Next
End Sub
' When the server disconnects, prevent further chat messages from being sent.
Private Sub MarkAsDisconnected()
txtSend.ReadOnly = True
btnSend.Enabled = False
End Sub
' Process the command received from the server, and take appropriate action.
Private Sub ProcessCommands(ByVal strMessage As String)
Dim dataArray() As String
' Message parts are divided by "|" Break the string into an array accordingly.
dataArray = strMessage.Split(Chr(124))
' dataArray(0) is the command.
Select Case dataArray(0)
Case "JOIN"
' Server acknowledged login.
DisplayText("You have joined the chat" & Chr(13) & Chr(10))
Case "CHAT"
' Received chat message, display it.
DisplayText(dataArray(1) & Chr(13) & Chr(10))
Case "REFUSE"
' Server refused login with this user name, try to log in with another.
AttemptLogin()
Case "LISTUSERS"
' Server sent a list of users.
ListUsers(dataArray)
Case "BROAD"
' Server sent a broadcast message
DisplayText("ServerMessage: " & dataArray(1) & Chr(13) & Chr(10))
End Select
End Sub
' Use a StreamWriter to send a message to server.
Private Sub SendData(ByVal data As String)
Dim writer As New IO.StreamWriter(client.GetStream)
writer.Write(data & vbCr)
writer.Flush()
End Sub
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -