通过TcpClient和TcpServer使用流

时间:2017-03-27 10:35:00

标签: .net vb.net tcp stream streaming

我尝试将数据从服务器(TcpListener)发送到客户端(TcpClient)。我使用Windows Form Application和下一个代码:

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

我正在尝试做什么:我创建服务器,然后按下按钮ButtonClientConnection' and create and connect client (TcpClient). That works - I get strings服务器已启动and客户端连接`在textbox1中。

现在我想做:在textbox2中输入一些文本,按下按钮ButtonSendData并通过流将这些数据从客户端传输到服务器,并将此数据写入另一个文本框(textbox3)。 我的尝试在代码中,但没有任何事情发生 - 正如我在调试器中看到的那样,我在流中写了一些数据,但无法读取它,因为我从流中读取数据的代码在客户端连接时只执行了一次。

如何永久读取数据?并按ButtonSendData将其正确写入流中 我做了一些解决方法,它看起来像是在线:

i = stream.Read(bytes, 0, bytes.Length)

代码在这里停止,在这个帖子中无法继续。

1 个答案:

答案 0 :(得分:1)

您通过错误的信息流发送数据。 stream是服务器的流,但您没有tcpclnt的流。

试试这个:

Dim clientstream As NetworkStream

Private Sub ButtonClientConnection_Click(sender As Object, e As EventArgs) Handles Button2.Click
    tcpclnt = New TcpClient()
    tcpclnt.Connect("127.0.0.115", 40000)
    clientstream = tcpclnt.GetStream()
End Sub

Private Sub ButtonSendData_Click(sender As Object, e As EventArgs) Handles Button3.Click
    Dim msgbyte() As Byte = Encoding.UTF8.GetBytes(TextBox2.Text)
    clientstream.Write(msgbyte, 0, msgbyte.Length)
End Sub

如您所见,我还删除了StringReader,因为它完全没必要。您创建它只是为了再次将其读回字符串,您可以直接从TextBox2.Text获取字节。