Re: [2005] Sockets Problem
Arent you using any error trapping for IOException ?
Re: [2005] Sockets Problem
No.
In a previous chat application I made, I was using the same exact code and such an error never befalled me. This one is new.
Why, what kind do you have in mind?
Re: [2005] Sockets Problem
Well at least on the server side you will need to trap and handle the exceptions that are relevant. Then from there you can send your message back to the originating client that the other client is disconnected etc.
Re: [2005] Sockets Problem
Well, upon putting the entire function in a try/catch block, it does not crash. However, it does not feel like good coding practice.
Oh well. Whatever works, I guess.
Re: [2005] Sockets Problem
I've recently been working with tcp sockets and a while back came across the same problem. I tackled the problem by checking if the bytesRead <= 0.
What happens is when the client has some kind of abnormal disconnect the stream received is zero (as far as i know). To handle the problem i used this code in the streamReceived method from my client:
Code:
Try
SyncLock client.GetStream 'so no other threads try to access at same time
BytesRead = client.GetStream.EndRead(ar)
If (BytesRead <= 0) Then 'Abnormal disconnect from client
RaiseEvent Disconnected(Me)
End If
End SyncLock
Catch e As Exception
'error
End Try
I also had another block of code that handled the Disconnected() event to remove the username from the UI etc.
I hope this helps
[EDIT]
Something else that might help - when the client disconnects/closes the connection by choice or forcibly, if the client is coded in vb you can try putting the following into the FormClosing event of the main form:
Code:
Select Case e.CloseReason
Case CloseReason.ApplicationExitCall
'closing procedure
Case CloseReason.FormOwnerClosing
'closing procedure
Case CloseReason.MdiFormClosing
'closing procedure
Case CloseReason.None
'closing procedure
Case CloseReason.TaskManagerClosing
'closing procedure
Case CloseReason.UserClosing
'closing procedure
Case CloseReason.WindowsShutDown
'closing procedure
End Select
You can try selecting which closing reason may apply and then send a text message to the server with a disconnect command.
Both of these methods have seemed quite effective to me.
Re: [2005] Sockets Problem
Thanks. These try/catch's actually have some use, after all.
Re: [2005] Sockets Problem
Try Catch blocks are error handling and every good app will make use of them as needed. You surely dont want your app to crash all the time because of unhandled errors. It wouldnt look too good or professional. :D
Re: [2005] Sockets Problem
Oh and Sockets from SocketExceptions ;) You can also catch normal Exceptions though :)