Click here to Skip to main content
15,889,315 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
VB
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
VB
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
Posted
Updated 8-Jan-12 20:51pm
v2

1 solution

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.

VB
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


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
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900