hi guys......can you please give me the full working code of the program, because i also encountered the same problem.....but i don't know where to put clientsocket.close() and the other corresponding code. i new to vb.net
AIM:1. when the user click on a disconnect button, the communication stop and user is able to reconnect again clicking on connect to server button.
2. the window form close properly

here is the client code:

Imports System.Net.Sockets
Imports System.Text
Public Class Form1
    Dim clientSocket As New System.Net.Sockets.TcpClient()
    Dim serverStream As NetworkStream
    Dim readData As String
    Dim infiniteCounter As Integer

    Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles send_message.Click
        Dim outStream As Byte() = _
        System.Text.Encoding.ASCII.GetBytes(TextBox2.Text + "$")
        serverStream.Write(outStream, 0, outStream.Length)
        serverStream.Flush()
    End Sub

    Private Sub msg()
        If Me.InvokeRequired Then
            Me.Invoke(New MethodInvoker(AddressOf msg))
        Else
            TextBox1.Text = TextBox1.Text + Environment.NewLine + " >> " + readData
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles connect_to_server.Click
        readData = "Conected to Chat Server ..."
        msg()
        clientSocket.Connect("127.0.0.1", 8888)
        'Label1.Text = "Client Socket Program - Server Connected ..."
        serverStream = clientSocket.GetStream()

        Dim outStream As Byte() = _
        System.Text.Encoding.ASCII.GetBytes(TextBox3.Text + "$")
        serverStream.Write(outStream, 0, outStream.Length)
        serverStream.Flush()

        Dim ctThread As Threading.Thread = New Threading.Thread(AddressOf getMessage)
        ctThread.Start()
    End Sub

    Private Sub getMessage()
        For infiniteCounter = 1 To 2
            infiniteCounter = 1
            serverStream = clientSocket.GetStream()
            Dim buffSize As Integer
            Dim inStream(10024) As Byte
            buffSize = clientSocket.ReceiveBufferSize
            serverStream.Read(inStream, 0, buffSize)
            Dim returndata As String = _
            System.Text.Encoding.ASCII.GetString(inStream)
            readData = "" + returndata
            msg()
        Next
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles disconnect.Click

    End Sub
End Class

button 3 is the disconnect button

Recommended Answers

All 2 Replies

>but i don't know where to put clientsocket.close() and the other corresponding code.

I suggest you to read this article/tutorial.

Private Sub Button2_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles connect_to_server.Click
       clientSocket=New System.Net.Sockets.TcpClient()
        readData = "Conected to Chat Server ..."
        msg()
        clientSocket.Connect("127.0.0.1", 8888)
        'Label1.Text = "Client Socket Program - Server Connected ..."
        serverStream = clientSocket.GetStream()

        Dim outStream As Byte() = _
        System.Text.Encoding.ASCII.GetBytes(TextBox3.Text + "$")
        serverStream.Write(outStream, 0, outStream.Length)
        serverStream.Flush()

        Dim ctThread As Threading.Thread = New Threading.Thread(AddressOf getMessage)
        ctThread.Start()
 
        ctThread.Join()
        clientsocket.close()
    End Sub

Thanks for replying........
when i add the two method:

ctThread.Join()

and

clientsocket.close()

, when the program is run .....the form freezes....therefore i cannot click on any button or write something........can you please give me a solution for it. Infact the program is infinite..... there is nothing which close the connection on the client side...

when i stop the program debug... i got a message:the program was forcibly closed...i continue getting the message until i stop the server program.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.