Help | How to keep the client connected

Circel

New member
Joined
Oct 25, 2012
Messages
2
Programming Experience
1-3
i build some chat, client server, but after 1 message the client send, he get disconnected any tips?
server :
VB.NET:
Imports System.Net.Sockets
Imports System.Text
Public Class Form1
    Dim thread As System.Threading.Thread
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If (Button1.Text = "Server - OFF") Then
            Button1.Text = "Server - ON"
            MsgBox("The server is online now")
            thread = New System.Threading.Thread(AddressOf server)
            thread.Start()
        ElseIf (Button1.Text = "Server - ON") Then
            Button1.Text = "Server - OFF"
            MsgBox("The server is no offline now")
        End If
    End Sub
    Private Sub server()
        Dim portNumber As Integer = 8000
        Dim tcpListener As New TcpListener(portNumber)
        TextBox1.Text = TextBox1.Text + "[Server] Loading Server..."
        Try


            Label1.Text = "Status - Server is ON"
            tcpListener.Start()
            TextBox1.Text = TextBox1.Text + vbCrLf & "[Server] Server Loaded succesfully, waiting for client"
            While (True)
                Dim tcpClient As TcpClient = tcpListener.AcceptTcpClient()
                Dim networkStream As NetworkStream = tcpClient.GetStream()
                TextBox1.Text = TextBox1.Text + vbCrLf & "Client connection request aproved"
                Dim bytes(tcpClient.ReceiveBufferSize) As Byte
                networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
                Dim clientdata As String = Encoding.ASCII.GetString(bytes)
                TextBox1.Text = TextBox1.Text + vbCrLf & "[Client] " + clientdata


            End While
        Catch ex As SocketException
            TextBox1.Text = TextBox1.Text + "Uknown error, server load fail!"
        End Try
    End Sub


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        CheckForIllegalCrossThreadCalls = False
    End Sub
End Class
client :
VB.NET:
Imports System.Net.Sockets
Imports System.Text
Public Class Form1
    Dim thread As System.Threading.Thread
    Dim clientSocket As New System.Net.Sockets.TcpClient()
    Dim serverStream As NetworkStream
    Private Sub connect()
        Try
            clientSocket.Connect("127.0.0.1", 8000)
            Label1.Text = "Status - Server Connected "
        Catch ex As SocketException
            MessageBox.Show("Unknown Error occurd", "Error")
        End Try
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        connect()
    End Sub


    Private Sub Button2_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        thread = New System.Threading.Thread(AddressOf client)
        thread.Start()
    End Sub
    Private Sub client()
        If TextBox2.Text = "" Then
            MessageBox.Show("Message cant be empty", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Else
            Try
                Dim serverStream As NetworkStream = clientSocket.GetStream()
                Dim outStream As Byte() = System.Text.Encoding.ASCII.GetBytes(TextBox2.Text)
                serverStream.Write(outStream, 0, outStream.Length)
            Catch ex As SocketException
                MessageBox.Show("Uknown Error occurd", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End If
    End Sub


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        CheckForIllegalCrossThreadCalls = False
    End Sub
End Class
 
Hi.
when you send a message to the Server, it will be closed automatically. I had a same problem in VB6 like this.
I used ListenPort and Listen method in Close_Event of the Socket.! But in VB.NET is different.
write a sub procedure to Listen to the port again and call it end of the Loop. I think it will be solved.
 
im sitting here for few hours trying to make what u said, no success
can u give me example?
edit :
success, thank you Mohamad even though your answer didnt work for me i'm glad someone here is ready to give some help.
and for the coming from google, i solve it in the next form :
this soultion isnt ideal but it work, cuz after write to stream the connection get closed, you need to do the next thing :
on every send u make u connect with a new clientsocket :
Dim clientSocket As New System.Net.Sockets.TcpClient()
clientSocket.Connect("127.0.0.1", 8000)
and after u done write to stream u close (dispose as well) the connection :
clientSocket.Close()
 
Last edited:
I noticed in your code that you contain the AcceptTcpClient() inside your while loop. This will halt the program as it is waiting for another "clientSocket.Connect" response from the client. This is most likely what caused the client to disconnect.


VB.NET:
                Dim tcpClient As TcpClient = tcpListener.AcceptTcpClient()                
                Dim networkStream As NetworkStream = tcpClient.GetStream()                
                TextBox1.Text = TextBox1.Text + vbCrLf & "Client connection request aproved"                            
                While (True)                                
                      Dim bytes(tcpClient.ReceiveBufferSize) As Byte                
                      networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))                
                      Dim clientdata As String = Encoding.ASCII.GetString(bytes)                
                      TextBox1.Text = TextBox1.Text + vbCrLf & "[Client] " + clientdata                           
               End While


This should work without having to dispose of the stream and socket.
 
im sitting here for few hours trying to make what u said, no success
can u give me example?
edit :
success, thank you Mohamad even though your answer didnt work for me i'm glad someone here is ready to give some help.
and for the coming from google, i solve it in the next form :
this soultion isnt ideal but it work, cuz after write to stream the connection get closed, you need to do the next thing :
on every send u make u connect with a new clientsocket :

and after u done write to stream u close (dispose as well) the connection :

Well, I'm glad that your problem has been solved. :)
 
Dear KevinBL
Would you please explain the below codes: Why did you use this code?

If (Button1.Text = "Server - OFF") Then
Button1.Text = "Server - ON"
MsgBox("The server is online now")
thread = New System.Threading.Thread(AddressOf server)
thread.Start()
ElseIf (Button1.Text = "Server - ON") Then
Button1.Text = "Server - OFF"
MsgBox("The server is no offline now")
End If
Do you know any sources to describe all the methods for Socket programming ( System.Net.Sockets ) ??

Thanks
 
Back
Top