Results 1 to 21 of 21

Thread: Console chat server. How to stop stream to client.

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2009
    Posts
    25

    Console chat server. How to stop stream to client.

    Hello all. I am building a chat client/server.

    Everything on the client end now works fine, but I am having problems with my console based server. Users can connect and chat just fine, but when a user disconnects, the server does not know how recognize this, often spamming the last message that was sent by the user, follow by the server app crashing.

    I need to find a way to stop streaming info to the user (and what ever else I need to stop to keep the server from crashing) and broadcast a message that says which user is disconnecting.

    This is my first chat/client program so I'm kinda lost as what I should do... Any help!? Thanks!
    Attached Images Attached Images   

  2. #2
    Frenzied Member Campion's Avatar
    Join Date
    Jul 2007
    Location
    UT
    Posts
    1,098

    Re: Console chat server. How to stop stream to client.

    How are you sending the data? Is it a timer, or some sort of event? Please post the code in question.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Mar 2009
    Posts
    25

    Re: Console chat server. How to stop stream to client.

    I believe this is what is causing the crash:

    vb Code:
    1. While (True)
    2.             counter += 1
    3.             clientSocket = serverSocket.AcceptTcpClient()
    4.  
    5.             Dim bytesFrom(10024) As Byte
    6.             Dim dataFromClient As String
    7.  
    8.             Dim networkStream As NetworkStream = _
    9.             clientSocket.GetStream()
    10.             networkStream.Read(bytesFrom, 0, CInt(clientSocket.ReceiveBufferSize))
    11.             dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom)
    12.             dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"))
    13.  
    14.             clientsList(dataFromClient) = clientSocket
    15.  
    16.             broadcast(TimeOfDay + " " + dataFromClient + " Joined ", dataFromClient, False)
    17.  
    18.             msg(TimeOfDay + " " + dataFromClient + " Joined chat room ")
    19.             Dim client As New handleClinet
    20.             client.startClient(clientSocket, dataFromClient, clientsList)
    21.         End While

    or

    vb Code:
    1. Private Sub doChat()
    2.             'Dim infiniteCounter As Integer
    3.             Dim requestCount As Integer
    4.             Dim bytesFrom(10024) As Byte
    5.             Dim dataFromClient As String
    6.             Dim sendBytes As [Byte]()
    7.             Dim serverResponse As String
    8.             Dim rCount As String
    9.             requestCount = 0
    10.  
    11.             While (True)
    12.                 Try
    13.                     requestCount = requestCount + 1
    14.                     Dim networkStream As NetworkStream = clientSocket.GetStream()
    15.                     networkStream.Read(bytesFrom, 0, CInt(clientSocket.ReceiveBufferSize))
    16.                     dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom)
    17.                     dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"))
    18.                     msg("From client - " + clNo + " : " + dataFromClient)
    19.                     rCount = Convert.ToString(requestCount)
    20.  
    21.                     broadcast(dataFromClient, clNo, True)
    22.                 Catch ex As Exception
    23.                     MsgBox(ex.ToString)
    24.                 End Try
    25.             End While
    26.         End Sub

    Once the client disconnects and no longer sends data, the server crashes. I think I had it backwards earlier, I need to stop attempting to read data from a client. As I said this is my first server, most of the code is from a tutorial.
    Last edited by BowserYo; Apr 2nd, 2009 at 01:23 PM.

  4. #4
    Frenzied Member Campion's Avatar
    Join Date
    Jul 2007
    Location
    UT
    Posts
    1,098

    Re: Console chat server. How to stop stream to client.

    It looks to me like you are setting yourself up for failure by not having an escape route for your loops.

    Try replacing the Whiles with

    Code:
    Do While Socket.State = connected

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Mar 2009
    Posts
    25

    Re: Console chat server. How to stop stream to client.

    Okay that is giving me these errors:

    'state' is not a member of 'System.Net.Sockets.Socket'.

    name 'connected' is not declared.

    I assume I am to rewrite that somehow but I do not know how.

  6. #6
    Frenzied Member Campion's Avatar
    Join Date
    Jul 2007
    Location
    UT
    Posts
    1,098

    Re: Console chat server. How to stop stream to client.

    You'll have to replace those with the name of your socket variable, and the proper name for the disconnected state, which will pop up after the equals sign. (I don't have VB available at the moment.)

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Mar 2009
    Posts
    25

    Re: Console chat server. How to stop stream to client.

    So...

    Do While clientSocket.Connected = True

    ?

  8. #8
    Frenzied Member Campion's Avatar
    Join Date
    Jul 2007
    Location
    UT
    Posts
    1,098

    Re: Console chat server. How to stop stream to client.

    Or, put more simply:

    Code:
    Do While clientSocket.Connected

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Mar 2009
    Posts
    25

    Re: Console chat server. How to stop stream to client.

    Okay cool beans! Now the server does not crash but it still gives me the big error (above). I'm not really sure why I am getting that error though...
    Last edited by BowserYo; Apr 2nd, 2009 at 01:57 PM.

  10. #10
    Frenzied Member Campion's Avatar
    Join Date
    Jul 2007
    Location
    UT
    Posts
    1,098

    Re: Console chat server. How to stop stream to client.

    Since it's telling that it can't do anything with something that's closed, put an Exit Do in the Catch block. This will exit your loop should the connection fail sometime between the loop start and the stream being created.

    I'm used to asyncronous connections, so forgive me, since I dont' work with Syncronous.

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Mar 2009
    Posts
    25

    Re: Console chat server. How to stop stream to client.

    Okay... I'll try and get something going in the catch block.

    Nah that's fine man! Your helping me a whole lot!!

  12. #12
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Console chat server. How to stop stream to client.

    I said this in the other thread. MSDN has a chat program. You should find it, understand it, and then modify it to meet your needs.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Mar 2009
    Posts
    25

    Re: Console chat server. How to stop stream to client.

    dbasnett: I was trying that a while before I knew about these forums. Thats how I found the code I am using it now, I am trying to modify it to suit my needs, and learn something at the same time.

    Thanks.

  14. #14
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Console chat server. How to stop stream to client.

    I don't have the MSDN chat program, but I don't remember seeing this

    While (True)

    in it.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Mar 2009
    Posts
    25

    Re: Console chat server. How to stop stream to client.

    This program is not the MSDN program, I found those on another website. If we are thinking of the same chat program, the one I tried turned out to be a lot more complicated.

  16. #16
    Frenzied Member Campion's Avatar
    Join Date
    Jul 2007
    Location
    UT
    Posts
    1,098

    Re: Console chat server. How to stop stream to client.

    In order to get to a more specific answer, I'm going to have to need access to a VB client, but that's not going to be for at least another 2 days (work schedule.) I'm not ignoring you.

  17. #17

    Thread Starter
    Junior Member
    Join Date
    Mar 2009
    Posts
    25

    Re: Console chat server. How to stop stream to client.

    Here is my program:
    Attached Files Attached Files

  18. #18
    Frenzied Member Campion's Avatar
    Join Date
    Jul 2007
    Location
    UT
    Posts
    1,098

    Re: Console chat server. How to stop stream to client.

    Thanks. This will help a lot in th long run. I'll take a look at it later today when I get home.

  19. #19

    Thread Starter
    Junior Member
    Join Date
    Mar 2009
    Posts
    25

    Re: Console chat server. How to stop stream to client.

    Okay. Thank you Champion!

  20. #20
    Frenzied Member Campion's Avatar
    Join Date
    Jul 2007
    Location
    UT
    Posts
    1,098

    Re: Console chat server. How to stop stream to client.

    Okay. I found out your issue. Basically, what's happening is that the socket is trying to get data from a stream that it is no longer connected to. So, I solved that by putting an IF block into the loop that throws an event to clean up.

    Also, I moved the Client object OUTSIDE of the module block. It's more confusing with it inside.

    Please throw this into a new folder so that you can look at and compare the difference.
    Attached Files Attached Files

  21. #21

    Thread Starter
    Junior Member
    Join Date
    Mar 2009
    Posts
    25

    Re: Console chat server. How to stop stream to client.

    Okay! Wow, I never expected to get this much help!! Thanks a lot Campion!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width