我正在尝试使用VB.Net编写一个简单的聊天应用程序,一切似乎正常,除了,尽管客户端已连接,但我的服务器未收到客户端发送给它的消息。
服务器代码:
Imports System.IO
Imports System.Net
Imports System.Net.Sockets
Imports System.Threading
Public Class TCPControl
Public Event MessageReceived(Sender As TCPControl, Data As String)
'Server config
Public ServerIP As IPAddress = IPAddress.Parse("10.14.140.132")
Public ServerPort As Integer = 9999
Public Server As TcpListener
Private CommThread As Thread
Public IsListening As Boolean = True
'Clients
Private Client As TcpClient
Private ClientData As StreamReader
Public Sub New()
Server = New TcpListener(ServerIP, ServerPort)
Server.Start()
CommThread = New Thread(New ThreadStart(AddressOf Listening))
End Sub
Private Sub Listening()
'Create listener loop
Do Until IsListening = False
'accept incoming connections
If Server.Pending = True Then
Client = Server.AcceptTcpClient
ClientData = New StreamReader(Client.GetStream)
End If
'raise event for incoming messages
Try
RaiseEvent MessageReceived(Me, ClientData.ReadLine)
Catch ex As Exception
End Try
'reduce CPU usage
Thread.Sleep(100)
Loop
End Sub
End Class
服务器端的Form1:
Public Class Form1
Private Server As TCPControl
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Server = New TCPControl
txtChat.Text = ":: Server started ::" & vbCrLf
AddHandler Server.MessageReceived, AddressOf OnLineReceived
End Sub
Private Delegate Sub UpdateTextDelegate(TB As TextBox, txt As String)
'update textbox
Private Sub UpdateText(TB As TextBox, txt As String)
If TB.InvokeRequired = True Then
TB.Invoke(New UpdateTextDelegate(AddressOf UpdateText), New Object() {TB, txt})
Else
If txt IsNot Nothing Then TB.AppendText(txt & vbCrLf)
End If
End Sub
Private Sub OnLineReceived(Sender As TCPControl, Data As String)
UpdateText(txtChat, Data)
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
Server.IsListening = False
End Sub
End Class
客户代码:
Imports System.Net
Imports System.Net.Sockets
Imports System.IO
Public Class TCPControl
Public Client As TcpClient
Public DataStream As StreamWriter
Public Sub New(Host As String, Port As Integer)
'Client
Client = New TcpClient(Host, port)
DataStream = New StreamWriter(Client.GetStream)
End Sub
Public Sub Send(Data As String)
DataStream.Write(Data & vbCrLf)
DataStream.Flush()
End Sub
End Class
客户端的Form1:
Public Class Form1
Private Client As TCPControl
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnConnect.Click
Client = New TCPControl("10.14.140.132", 9999)
If Client.Client.Connected Then btnConnect.Text = "Connected"
End Sub
Private Sub btnSend_Click(sender As Object, e As EventArgs) Handles btnSend.Click
SendMessage()
txtMessage.Clear()
txtMessage.Focus()
End Sub
Private Sub SendMessage()
If Client.Client.Connected Then Client.Send(txtMessage.Text)
End Sub
Private Sub txtMessage_TextChanged(sender As Object, e As EventArgs) Handles txtMessage.TextChanged
End Sub
Private Sub txtMessage_KeyDown(sender As Object, e As KeyEventArgs) Handles txtMessage.KeyDown
If e.KeyCode = Keys.Enter Then SendMessage()
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
If Client.Client.Connected Then
Client.DataStream.Close()
Client.Client.Close()
End If
End Sub
End Class
我已经看了一段时间,一切看起来都很好,但是我的服务器没有收到我尝试发送的任何消息。