2 Attachment(s)
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! :thumb:
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.
Re: Console chat server. How to stop stream to client.
I believe this is what is causing the crash:
vb Code:
While (True)
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(TimeOfDay + " " + dataFromClient + " Joined ", dataFromClient, False)
msg(TimeOfDay + " " + dataFromClient + " Joined chat room ")
Dim client As New handleClinet
client.startClient(clientSocket, dataFromClient, clientsList)
End While
or
vb Code:
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
While (True)
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
End While
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.
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
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.
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.)
Re: Console chat server. How to stop stream to client.
So...
Do While clientSocket.Connected = True
?
Re: Console chat server. How to stop stream to client.
Or, put more simply:
Code:
Do While clientSocket.Connected
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...
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.
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!!
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.
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.
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.
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.
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.
2 Attachment(s)
Re: Console chat server. How to stop stream to client.
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.
Re: Console chat server. How to stop stream to client.
Okay. Thank you Champion! :thumb:
1 Attachment(s)
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.
Re: Console chat server. How to stop stream to client.
Okay! Wow, I never expected to get this much help!! Thanks a lot Campion!