Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm using sockets to create a chat simulation application using visual basic 2010. I've managed to make the connection work and send and receive data between two instances of my app. But when I close one of them, the other one starts getting the last received message and adding it to the message textbox repeatedly until the program jams and stops working. I've tried closing the connection on form closing like so:
VB
Private Sub ChatWindow_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If Mode = mode.Server Then
            ServerSocket.Stop()
        End If
        NetworkStream.Close()
        ClientSocket.Close()
    End Sub

I want the application to be able to act as server or just as client, so I've established a communication mode (server or client, if you start as server you open a tcplistener) and that seems to be working fine. It's just when I want to "Log off" or just close one of the two apps. When I researched all I could find was that I had to close the network stream as well as the tcpclient, but I tried that and I still have the same problem. How can I avoid this? How can I tell when one of the two end points disconnects and just show a message on screen to tell that the other end is no longer available? Additionally, how can I tell if there's a client available for connection, so the instance running as a server doesn't freeze when it gets no response?
Posted
Updated 27-May-18 6:07am

1 solution

using methodology might resolve this issue as:

C#
using (TcpClient tcpClient = new TcpClient())
{
   if (tcpClient != null)
   {
    tcpClient.GetStream().Close();
    tcpClient.Close();
    tcpClient = null;
   }
}
 
Share this answer
 
Comments
Vic91 7-Dec-11 23:05pm    
Do you think you could provide the vb coding for this please? I tried to translate it But I get an error. I'm guessing I should write this on the sub that awaits for the clients petitions?

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