【发布时间】:2017-08-20 00:31:59
【问题描述】:
我尝试将数据从服务器 (TcpListener) 发送到客户端 (TcpClient)。我使用 Windows 窗体应用程序和下一个代码:
Imports System.Text
Imports System.Net
Imports System.Net.Sockets
Imports System.Threading
Imports System.Runtime.InteropServices
Public Class Form1
Private Server As TcpListener = Nothing
Private ServerThread As Thread = Nothing
Dim tcpclnt As TcpClient
Dim stream As NetworkStream
Public Shared bytes(1024) As Byte
Public Shared data As String = Nothing
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Shown
Server = New TcpListener(IPAddress.Any, 40000)
ServerThread = New Thread(AddressOf ConnectionListener)
ServerThread.IsBackground = True
ServerThread.Start()
TextBox1.Text = "Server started!"
End Sub
Private Sub ConnectionListener()
Try
Server.Start()
While True
Dim myClient As TcpClient = Server.AcceptTcpClient()
Dim T As New Thread(AddressOf SomeClientActions)
T.Start(myClient)
Invoke(Sub() TextBox1.Text = TextBox1.Text + Environment.NewLine + "Client connected")
' HERE IM TRYING TO READ DATA FROM STREAM
stream = myClient.GetStream()
Dim i As Int32
i = stream.Read(bytes, 0, bytes.Length)
While True
' Translate data bytes to a ASCII string.
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i)
Invoke(Sub() TextBox3.Text = "Received: {0}" + data)
' Process the data sent by the client.
data = data.ToUpper()
Dim msg As Byte() = System.Text.Encoding.ASCII.GetBytes(data)
' Send back a response.
stream.Write(msg, 0, msg.Length)
Invoke(Sub() TextBox3.Text = "Sent: {0}" + data)
i = stream.Read(bytes, 0, bytes.Length)
End While
myClient.Close()
End While
Catch ex As Exception
MessageBox.Show("Unable to Accept Connections", "Server Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End Try
Application.ExitThread()
End Sub
Private Sub SomeClientActions(ByVal client As Object)
' ... do something with "client" in here ...
' MAYBE SOME CODE HERE?..
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
Server.Stop()
End Sub
Private Sub ButtonClientConnection_Click(sender As Object, e As EventArgs) Handles Button2.Click
tcpclnt = New TcpClient()
tcpclnt.Connect("127.0.0.115", 40000)
End Sub
Private Sub ButtonSendData_Click(sender As Object, e As EventArgs) Handles Button3.Click
' HERE IM TRYING TO WRITE DATA TO STREAM FROM TEXTBOX
Dim msg As New System.IO.StringReader(TextBox2.Text)
Dim msgstr As String = msg.ReadToEnd
Dim msgbyte() As Byte = Encoding.UTF8.GetBytes(msgstr)
stream.Write(msgbyte, 0, msgbyte.Length)
End Sub
End Class
我正在尝试做的事情:我创建服务器,然后在 textbox1 中按下按钮 ButtonClientConnection' and create and connect client (TcpClient). That works - I get stringsServer startedandClient connected`。
现在我想做:在 textbox2 中放入一些文本,按下按钮 ButtonSendData 并通过流将这些数据从客户端传输到服务器,并将这些数据写入另一个文本框(textbox3)。
我的尝试在代码中,但没有任何反应 - 正如我在调试器中看到的那样,我在流中写入了一些数据,但无法读取它,因为我用于从流中读取数据的代码在客户端连接时仅执行 1 次。
如何永久读取数据?并按ButtonSendData 将其正确写入流式传输?
我做了一些解决方法,看起来它卡在了线上:
i = stream.Read(bytes, 0, bytes.Length)
代码停在这里,不能在这个线程中进一步。
【问题讨论】:
-
NetworkStread.Read()阻塞调用线程,直到有东西要接收,所以当你发送数据时应该继续执行。 -
@VisualVincent 啊哈!所以我只是错误地发送数据?你能看看我的
ButtonSendData_Click函数吗?也许我在另一个线程中做?
标签: .net vb.net tcp stream streaming