By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,299 Members | 1,141 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,299 IT Pros & Developers. It's quick & easy.

Socket.Receive and BeginReceive Problem

P: n/a
I've got an odd problem here that I've been beating my head on for
days. I've written a class that uses a pre-established connection
for communication, meaning I can use this class for a server or a
client. After receiving a Socket that has been connected to a
RemoteEP, it begins 2 threads by calling out Begin(). My Send Thread
pauses until the class Parent wants to send data. Receive Thread
keeps
checking for incoming data.

My problem is within my Receive Thread. Everything looks good unitl I
call my Destroy command. I use a boolean variable to determine
weather or not to end my threads. Destroy() sets m_bDestroying =
TRUE. The Send() thread exits just fine when I set m_bDestroying =
TRUE. However, my ReciveMessage() thread reads m_bDestroying as
FALSE!! It's supposed to be TRUE. I never told it to change. Now
here's the twist. If I comment out the remoteSocket.Receive(data) in
the ReceiveMessage(),
ReceiveMessage() will read m_bDestroying as TRUE!!! It seems like the
Receive Sub does something to his thread. Can anyone explain?

Other Notes: I'm using the same program to run a client as server,
but using two independently declared variable, making two separate
instances of this class. However, if I just use the Server, the
codes successfully works. This suggests maybe a memory management
problem? I don't have any "Shared" methods or vars. I do have
ReadWriteLock that stop Async problems. Also, I have this EXACT same
problem if I use the Socket.BeginReceive instead of using my own
ReceiveMessage Thread. I can supply my full code to anyone who wants
to view it.

Here's important Snipits of my code with stuff taken out for ease of
viewing.

My RemoteConnection Class:

private m_bDestroying as boolean
Private sendMessageThread As Thread
Private ReceiveThread As Thread
Private remoteSocket as Socket

'This gets call called within a thread
Public Sub Begin()

'set vars
m_bDestroying = False

'Que is empty, so pause it
pauseOnEmptySendQue.Reset()

sendMessageThread = New Thread(AddressOf Send)
ReceiveThread = New Thread(AddressOf ReceiveMessage)

sendMessageThread.Start()

ReceiveThread.Start()
End Sub

Private Sub ReceiveMessage()

Dim bytesRead As Integer
Dim Data(1024) As Byte
Dim content As String
Dim tmpByte() As Byte

Try
While True

'************************************************* ************
'PROBLEM!!!! m_bDestroying is always FALSE!!!, Only if I declare it as
TRUE and
'don't change it.
'************************************************* ************
ReadLock.AquireReadLock(-1)
If m_bDestroying Then
messagePoster.Invoke("Successfully Exiting
ReceiveMessage Thread...")
Exit While
End If
ReadLock.ReleaseReadLock()

'************************************************* ***********
'If I comment out This Line, m_dDestroying suddenly
remoteSocket.Receive(Data)
'bytesRead = remoteSocket.Receive(Data)

'Bla Bla Bla
'ReceiveThread.sleep(1)
Exit While
End Sub

Private Sub Send()
Dim dataByte() As Byte
Dim needEOF As Boolean
While 1 = 1

pauseOnEmptySendQue.WaitOne()

'Stop While Loop if Thread is to be destroyed
If m_bDestroying Then
messagePoster.Invoke("Send Thread Successfully
exiting...")
Exit While
End If

'Bla Bla Bla
End While

End Sub

Public Function Destroy() As Boolean

Dim DoneDestroy As Boolean
ReadLock.AquireWriteLock(-1)
m_bDestroying = True
ReadLock.ReleaseWriteLock()

'wait for ReceiveThreadJoin to Join
ReceiveThread.Join()

'resume Thread to make it end
pauseOnEmptySendQue.Set()

'Wait until Thread is stopped
sendMessageThread.Join()
sendMessageThread = Nothing
remoteSocket.Shutdown(SocketShutdown.Both)
remoteSocket.Close()
'remoteSocket = Nothing

Return True

End Function

Thanks,
-Phil
Nov 20 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.