Need help - Socket get nuts

M

Marty

Hi,

I hope you can help me with this one (my code is below my message).

I have many clients connecting to my server. When any of them
disconnect, something happen.

This user get disconnect from the server and this is fine. But
sometime, I can't reproduce exactly the situation (maybe the client
crashed), the related socket in server seems to get nuts and still think
that it is connected to the client.

Here is my part of the code that contain a possible bug... The socket
keep calling rcvSocketMessages() to infinity. It receive an empty ""
character in strTempMesg.

What I don't understand, it is why is pass through the first and second
"if" that concern the mySocket state. Should it be able to detect that
the client is no more there?

Do you have any idea why the mySocket keep its connected state?

I hope my explanation was clear, otherwise, ask me and I'll answer your
question. I really have to correct this problem.

Thank you very much.

Marty

Private mySocket As Socket

Private Sub rcvSocketMessages(ByVal ar As IAsyncResult)
Try
Dim BytesRead As Integer
Dim strTempMesg As String

If (Not mySocket Is Nothing) Then
If (mySocket.Connected) Then

BytesRead = CurSocket.EndReceive(ar)
strTempMesg = Encoding.ASCII.GetString(Buffer, 0, BytesRead)
'Do something with strTempMsg

CurSocket.BeginReceive(Buffer, 0, READ_BUFFER_SIZE,
SocketFlags.None, AddressOf rcvSocketMessages, Nothing)

Else
forceSocketDisconnection()
End If
Else
'forceSocketDisconnection()
End If

Catch e As Exception
'catch error
End Try
 
S

Supra

as fact as i understand. can u removed those two (2)
forceSocketDisconnection() ? u cannot put forceSocketDisconnection()
after CurSocket.BeginReceive. i think u can put
forceSocketDisconnection() before CurSocket.BeginReceive . the code i
used: also i added ondisconect.

Private Sub sockDataArrival(ByVal ar As IAsyncResult)
Dim state As StateObject = CType(ar.AsyncState, StateObject)
Dim client As Socket = state.workSocket
Dim bytesRead As Integer

Try
bytesRead = client.EndReceive(ar)
Catch
Exit Sub
End Try

Try
Dim Data() As Byte = state.buffer
If bytesRead = 0 Then
client.Shutdown(SocketShutdown.Both)
client.Close()
RaiseEvent onDisconnect()
Exit Sub
End If
ReDim state.buffer(32767)

client.BeginReceive(state.buffer, 0, state.BufferSize, 0,
AddressOf sockDataArrival, state)
RaiseEvent onDataArrival(Data, bytesRead)
Catch
RaiseEvent onError(Err.Description)
Exit Sub
End Try

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top