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
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

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

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


End Sub

Private Sub ReceiveMessage()

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

While True

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

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

'Bla Bla Bla
Exit While
End Sub

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


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

'Bla Bla Bla
End While

End Sub

Public Function Destroy() As Boolean

Dim DoneDestroy As Boolean
m_bDestroying = True

'wait for ReceiveThreadJoin to Join

'resume Thread to make it end

'Wait until Thread is stopped
sendMessageThread = Nothing
'remoteSocket = Nothing

Return True

End Function

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.