[询问] TclListener两次无法收到来自客户端的消息 [英] [Ask] TclListener Can't Receive Message From Client Twice

查看:77
本文介绍了[询问] TclListener两次无法收到来自客户端的消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我正在尝试使用VB.Net 2010创建一个非常简单的聊天应用程序.客户端可以连接到服务器,也可以发送消息.但是,当我第二次尝试发送消息时,客户端生成了错误.错误消息是"

Hi all, I''m trying to make a very simple chat application using VB.Net 2010. The client can connect to the server and can send message too. But, when I tried to send message for the second time the client generated error. The error message is "

Unable to write data to the transport connection: An established connection was aborted by the software in your host machine.

"

请帮帮我.我真的不知道如何解决这个问题.

这是服务器代码

"

Help me, please. I''m really having no idea how to solve this problem.

Here is the Server Code

Public Class frmServer

    Dim server As TcpListener
    Dim client As TcpClient
    Private Sub frmServer_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim ipEndPoint As IPEndPoint = New IPEndPoint(IPAddress.Any, 2000)
        server = New TcpListener(ipEndPoint)
        server.Start(5)

        'Accepted client
        client = New TcpClient()
        Dim thr As Thread = New Thread(New ThreadStart(AddressOf OnReceive))
        thr.Start()
    End Sub

    Private Sub OnReceive()
        Dim msg As String
        While True
            Thread.Sleep(1000)
            client = server.AcceptTcpClient()
            Using stream As NetworkStream = client.GetStream()
                Dim bMsg(1023) As Byte
                Dim i As Integer = stream.Read(bMsg, 0, bMsg.Length)
                If i > 0 Then
                    msg = System.Text.Encoding.ASCII.GetString(bMsg)
                    If InvokeRequired Then
                        Invoke(New _Read(AddressOf Read), msg)
                    End If
                End If
            End Using
        End While

    End Sub

    Private Delegate Sub _Read(ByVal msg As String)
    Private Sub Read(ByVal msg As String)
        MessageBox.Show(msg)
    End Sub

End Class




这是客户代码




Here is the Client Code

Public Class frmClient
    Dim client As TcpClient

    Private Sub btnConnect_Click(sender As System.Object, e As System.EventArgs) Handles btnConnect.Click
        Dim ipEndPoint As IPEndPoint = New IPEndPoint(IPAddress.Parse("127.0.0.1"), 2000)
        client = New TcpClient()
        client.Connect(ipEndPoint)
        If client.Connected Then
            Me.Text = "client - Connected to Server 127.0.0.1"
        End If
    End Sub

    Private Sub btnSend_Click(sender As System.Object, e As System.EventArgs) Handles btnSend.Click
        If client.Connected Then
            Dim msg() As Byte = System.Text.Encoding.ASCII.GetBytes(Me.txtClient.Text)
            Dim stream As NetworkStream = client.GetStream()
            stream.Write(msg, 0, msg.Length)
        End If
    End Sub
End Class

推荐答案



您的服务器代码不正确. OnReceive实际上是OnAccept ++.它连续接受新连接,因此删除先前接受的连接.
如果要接受多个连接,则需要有一个线程来接受连接,这将触发一个新线程来读取数据.如果您确定可以使用一个连接,则需要花一些时间来读取数据.
请注意,中止的连接(从客户端)将引发您可能要处理的异常.

希望这会有所帮助.

干杯,AT


<添加>
奇怪的是,我从您的邮箱中收到的问题现在消失了;您已经解决了它,或者您不想一直困扰这个问题.无论如何,我对代码做了一些小的修改,但要注意,尽管它可能起作用,但它绝不是好的代码!!!

它确实缺乏结构和安全性/崩溃保护.所以:很适合使用玩具,但没有生产代码.请这样处理.

Hi,

Your server code is not OK. The OnReceive is actually OnAccept++. It continuaously accepts a new connection and therefor drops the previous accepted connection.
If you want to accept multiple connections you''ll need to have one thread for accepting connections that will fire a new thread for reading the data. If you are confindent that you can do with one connection you''l need to put the while around the reading of data.
Note that a aborted connection (from the client side) will throw an exception which you might want to handle.

Hope this helps.

Cheers, AT


<added>
Odd enough the question i got from you in my mailbox is now gone; either you have solved it or you don''t want to have the question lingering around. Anyway I have made a few small modifications to the code with the big note that though it may work It Is By No Means Good Code!!!!

It does lack structure and security / crash protection. So: nice to toy with but not production code. Please treat it as such.

Private Sub OnReceive()
    Dim msg As String
    Thread.Sleep(1000)
    client = server.AcceptTcpClient()
    
    Using stream As NetworkStream = client.GetStream()
    Dim bMsg(1023) As Byte
    
    While True
       Dim i As Integer = stream.Read(bMsg, 0, bMsg.Length)
       If i > 0 Then
          msg = System.Text.Encoding.ASCII.GetString(bMsg)
          If InvokeRequired Then
             Invoke(New _Read(AddressOf Read), msg)
          End If
       End If
    End While
 
    End Using
 
End Sub



好吧,它至少应该帮助您整夜.
小提示:如果您喜欢阅读的内容,请随时投票:)

干杯,AT



Well, it should at least help you through the night.
small note: feel free to upvote if you like what you''re reading :)

Cheers, AT


这篇关于[询问] TclListener两次无法收到来自客户端的消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆