6 Replies - 555 Views - Last Post: 16 October 2013 - 03:18 PM Rate Topic: -----

#1 panzerfaust67  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 23-March 13

How to properly close a client socket in a chat program

Posted 15 October 2013 - 09:21 AM

Im tweaking a basic chat application i found on the internet which includes a server and the client i will provide dl links to the vb files. The bug is when you connect to the server and click the exit server button it does not close the socket properly and causes the client to spaz out and the server makes an error. I want to be able to close the clients connection to the server without causing an error. I'm new to server code in vb so help would be appreciated. the two things i tweaked so far i made a custom ip textbox for joining WAN servers and the exit button which doesn't work properly.

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: How to properly close a client socket in a chat program

#2 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 673
  • View blog
  • Posts: 2,960
  • Joined: 03-December 12

Re: How to properly close a client socket in a chat program

Posted 15 October 2013 - 09:31 AM

Post the code you need help with in [code] tags and any errors you receive with line numbers preferably. I don't think anyone is going to download a zip and run unknown code.
Was This Post Helpful? 0
  • +
  • -

#3 panzerfaust67  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 23-March 13

Re: How to properly close a client socket in a chat program

Posted 15 October 2013 - 09:48 AM

here is the error im getting it wont let me put the pic here i think its too big http://www.mediafire...8r93x/error.png
 
 Imports System.Net.Sockets
Imports System.Text
Module Module1
    Dim clientsList As New Hashtable
    Sub Main()
        Dim serverSocket As New TcpListener(8888)
        Dim clientSocket As TcpClient
        Dim infiniteCounter As Integer
        Dim counter As Integer

        serverSocket.Start()
        msg("Chat Server Started ....")
        counter = 0
        infiniteCounter = 0
        For infiniteCounter = 1 To 2
            infiniteCounter = 1
            counter += 1
            clientSocket = serverSocket.AcceptTcpClient()

            Dim bytesFrom(10024) As Byte
            Dim dataFromClient As String

            Dim networkStream As NetworkStream = _
            clientSocket.GetStream()
            networkStream.Read(bytesFrom, 0, CInt(clientSocket.ReceiveBufferSize))
            dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom)
            dataFromClient = _
            dataFromClient.Substring(0, dataFromClient.IndexOf("$"))

            clientsList(dataFromClient) = clientSocket

            broadcast(dataFromClient + " Joined ", dataFromClient, False)

            msg(dataFromClient + " Joined chat room ")
            Dim client As New handleClinet
            client.startClient(clientSocket, dataFromClient, clientsList)
        Next

        clientSocket.Close()
        serverSocket.Stop()
        msg("exit")
        Console.ReadLine()
    End Sub

    Sub msg(ByVal mesg As String)
        mesg.Trim()
        Console.WriteLine(" >> " + mesg)
    End Sub
    Private Sub broadcast(ByVal msg As String, _
    ByVal uName As String, ByVal flag As Boolean)
        Dim Item As DictionaryEntry
        For Each Item In clientsList
            Dim broadcastSocket As TcpClient
            broadcastSocket = CType(Item.Value, TcpClient)
            Dim broadcastStream As NetworkStream = _
                    broadcastSocket.GetStream()
            Dim broadcastBytes As [Byte]()

            If flag = True Then
                broadcastBytes = Encoding.ASCII.GetBytes(uName + " says : " + msg)
            Else
                broadcastBytes = Encoding.ASCII.GetBytes(msg)
            End If

            broadcastStream.Write(broadcastBytes, 0, broadcastBytes.Length)
            broadcastStream.Flush()
        Next
    End Sub

    Public Class handleClinet
        Dim clientSocket As TcpClient
        Dim clNo As String
        Dim clientsList As Hashtable

        Public Sub startClient(ByVal inClientSocket As TcpClient, _
        ByVal clineNo As String, ByVal cList As Hashtable)
            Me.clientSocket = inClientSocket
            Me.clNo = clineNo
            Me.clientsList = cList
            Dim ctThread As Threading.Thread = New Threading.Thread(AddressOf doChat)
            ctThread.Start()
        End Sub

        Private Sub doChat()
            Dim infiniteCounter As Integer
            Dim requestCount As Integer
            Dim bytesFrom(10024) As Byte
            Dim dataFromClient As String
            Dim sendBytes As [Byte]()
            Dim serverResponse As String
            Dim rCount As String
            requestCount = 0
            For infiniteCounter = 1 To 2
                infiniteCounter = 1
                Try
                    requestCount = requestCount + 1
                    Dim networkStream As NetworkStream = _
                            clientSocket.GetStream()
                    networkStream.Read(bytesFrom, 0, CInt(clientSocket.ReceiveBufferSize))
                    dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom)
                    dataFromClient = _
                        dataFromClient.Substring(0, dataFromClient.IndexOf("$"))
                    msg("From client - " + clNo + " : " + dataFromClient)
                    rCount = Convert.ToString(requestCount)

                    broadcast(dataFromClient, clNo, True)
                Catch ex As Exception
                    MsgBox(ex.ToString)
                End Try
            Next
        End Sub

    End Class
End Module


This post has been edited by panzerfaust67: 15 October 2013 - 09:49 AM

Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9204
  • View blog
  • Posts: 34,581
  • Joined: 12-June 08

Re: How to properly close a client socket in a chat program

Posted 15 October 2013 - 09:52 AM

Okay.. so put a break point at the top of that function and step through your code. What collection/object are you trying to access out of the range of available indexes? Since your screen shot doesn't show line numbers I am taking a stab and say line 101 is probably something around:
18	                    dataFromClient = _
19	                        dataFromClient.Substring(0, dataFromClient.IndexOf("$"))


.. though I am unsure. Again.. breakpoint, step through your code, and examine your collections/calls/etc.
Was This Post Helpful? 0
  • +
  • -

#5 panzerfaust67  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 23-March 13

Re: How to properly close a client socket in a chat program

Posted 15 October 2013 - 09:54 AM

the exit server button code on the client is simpily
 clientSocket.Close()


Im pretty sure that is the correct code but it causes error
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9204
  • View blog
  • Posts: 34,581
  • Joined: 12-June 08

Re: How to properly close a client socket in a chat program

Posted 15 October 2013 - 12:22 PM

I am unclear - is that in response to my post, or another one?
Was This Post Helpful? 0
  • +
  • -

#7 panzerfaust67  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 23-March 13

Re: How to properly close a client socket in a chat program

Posted 16 October 2013 - 03:18 PM

View Postmodi123_1, on 15 October 2013 - 12:22 PM, said:

I am unclear - is that in response to my post, or another one?


Ive tried looking at both the client and the server the small piece i posted above was for the client. I believe the flaw is all server sided. I have tried debugging at the lines you suggested and that is indeed where the problem is but im not sure how i could possibly fix it. I was under the impression i could simply just throw in a couple of on error resume next's but that didnt work.

This post has been edited by panzerfaust67: 16 October 2013 - 03:19 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1