472,145 Members | 1,304 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,145 software developers and data experts.

Issue with multiple threads and System.Net.Sockets.Socket

We have an application that has a 200 count loop that does the following:

' Each time thru the loop we pass the next IP Address is a range (aka
192.168.4.50 thru 192.168.4.254)
Try
If mUIO_Threads(i) Is Nothing Then
mUIO_Threads(i) = New System.Threading.Thread(AddressOf
mUIO_DAQ(i).InitDAQ)
mUIO_Threads(i).Name = mUIO_DAQ(i).UIO_IPAddr
mUIO_Threads(i).IsBackground = False
Else
If Not mUIO_Threads(i) Is Nothing Then
mUIO_Threads(i).Abort()
While mUIO_Threads(i).ThreadState=Threading.ThreadState. Running
End While
mUIO_Threads(i) = Nothing
End If
mUIO_Threads(i) = New System.Threading.Thread(AddressOf
mUIO_DAQ(i).InitDAQ)
mUIO_Threads(i).Name = mUIO_DAQ(i).UIO_IPAddr
mUIO_Threads(i).IsBackground = False
End If
mUIO_Threads(i).Start()

stime = Environment.TickCount
While (Environment.TickCount < stime + 100) And (Not mUIO_DAQ(i).UIO_Opened)
System.Windows.Forms.Application.DoEvents()
End While

Catch ex As Threading.ThreadStateException
Debug.WriteLine("Poll: Threading Error <" & ex.ToString & ">")
Catch ex As Exception
Debug.WriteLine("Poll: Exception Error <" & ex.ToString & ">")
End Try


The call to "InitDAQ" creates a TCPIP socket and calls
System.Net.Sockets.Socket.Connect as follows:

Me.m_Socket = New
System.Net.Sockets.Socket(Net.Sockets.AddressFamil y.InterNetwork,
SocketType.Stream, Net.Sockets.ProtocolType.IP)
ipAddressEndPoint = New
System.Net.IPEndPoint(IpAddressArg.Parse(pchIpAddr essArg), nPort)
OpenSockets = SIOMM_OK
Me.m_Socket.Connect(ipAddressEndPoint)
Some of the IP Addresses passed are not online and therefore will fail to
connect. Our test has ip 192.168.4.50 valid and also 192.168.4.253 and
192.168.4.254 valid. All others in the range were offline. We get a
connection working with 192.168.4.50 and eventually after 5 minutes get the
other two also. If we cut the range down to 192.168.4.250 to 192.168.4.254
we connect to 253 and 254 immediately on the first time throught the loop.
If we adjust the range to 192.168.4.240 to 192.168.4.254, we don't connect
the first time through the loop but we do get it the second time through. It
is almost as if we can only get data from the first 10 attempted sockets and
have to wait for them to timeout before we can get data from the next ten.
We have tried setting the socket to Non-Blocking but that didn't seem to
help. We do see spurts of 10 threads exiting at one time. Any ideas

Nov 21 '05 #1
2 2235
Why are you using an array of threads. Why are you not using the thread
appartment?
"Stressed Out Developer" <St******************@discussions.microsoft.com>
wrote in message news:41**********************************@microsof t.com...
We have an application that has a 200 count loop that does the following:

' Each time thru the loop we pass the next IP Address is a range (aka
192.168.4.50 thru 192.168.4.254)
Try
If mUIO_Threads(i) Is Nothing Then
mUIO_Threads(i) = New System.Threading.Thread(AddressOf
mUIO_DAQ(i).InitDAQ)
mUIO_Threads(i).Name = mUIO_DAQ(i).UIO_IPAddr
mUIO_Threads(i).IsBackground = False
Else
If Not mUIO_Threads(i) Is Nothing Then
mUIO_Threads(i).Abort()
While mUIO_Threads(i).ThreadState=Threading.ThreadState. Running
End While
mUIO_Threads(i) = Nothing
End If
mUIO_Threads(i) = New System.Threading.Thread(AddressOf
mUIO_DAQ(i).InitDAQ)
mUIO_Threads(i).Name = mUIO_DAQ(i).UIO_IPAddr
mUIO_Threads(i).IsBackground = False
End If
mUIO_Threads(i).Start()

stime = Environment.TickCount
While (Environment.TickCount < stime + 100) And (Not
mUIO_DAQ(i).UIO_Opened)
System.Windows.Forms.Application.DoEvents()
End While

Catch ex As Threading.ThreadStateException
Debug.WriteLine("Poll: Threading Error <" & ex.ToString & ">")
Catch ex As Exception
Debug.WriteLine("Poll: Exception Error <" & ex.ToString & ">")
End Try


The call to "InitDAQ" creates a TCPIP socket and calls
System.Net.Sockets.Socket.Connect as follows:

Me.m_Socket = New
System.Net.Sockets.Socket(Net.Sockets.AddressFamil y.InterNetwork,
SocketType.Stream, Net.Sockets.ProtocolType.IP)
ipAddressEndPoint = New
System.Net.IPEndPoint(IpAddressArg.Parse(pchIpAddr essArg), nPort)
OpenSockets = SIOMM_OK
Me.m_Socket.Connect(ipAddressEndPoint)
Some of the IP Addresses passed are not online and therefore will fail to
connect. Our test has ip 192.168.4.50 valid and also 192.168.4.253 and
192.168.4.254 valid. All others in the range were offline. We get a
connection working with 192.168.4.50 and eventually after 5 minutes get
the
other two also. If we cut the range down to 192.168.4.250 to
192.168.4.254
we connect to 253 and 254 immediately on the first time throught the loop.
If we adjust the range to 192.168.4.240 to 192.168.4.254, we don't connect
the first time through the loop but we do get it the second time through.
It
is almost as if we can only get data from the first 10 attempted sockets
and
have to wait for them to timeout before we can get data from the next ten.
We have tried setting the socket to Non-Blocking but that didn't seem to
help. We do see spurts of 10 threads exiting at one time. Any ideas

Nov 21 '05 #2
lets work out 1 here then you sould be able to work with more
Remember each thead can only be used 1 time before it is trash
so
first off your app has a 200 loop count, I am pritty sure you can only throw
25 threads at a time.


private thd as system.threading.thread

private eip as string
private sip as string
private structure iprange
dim startip as string
dim endip as string
end structure

public sub New
dim ssplit as object

ips = new iprange
ips.startip = 192.168.0.250
ips.endip = 192.168.0.254

ssplit = split(ips.end,".")
eip = ssplit(3)
ssplit = split(ips.startip ,".")
sip= ssplit(3)
for i = sip to eip

thd = new system.threading.thread addressof
thd_tick(left(ips.endip,(len(ips.endip)-len(ssplit(3)) & i)
next

end sub
private sub thd_tick(byval ip as string)
'do your stuff here with the ip address
end sub
this is just a quick dirty example. I do not know if it even works because I
am coding in OutLook.. Lol it should give you the right idea.
I want to ask again why are you using an array() to hold your threads? Look
in to the thread appartment, I have never used it so I can not give you an
example. I have never needed to use it. But the appartment I think takes
care of handling the thread throwing. Meaning if you have 50 threads
waiting to go, it will only throw 25 when 1 finishes it throws another.. I
might be pulling that out of my ass. But I think that is what it is for.
Now what that code is doing is for every ip address it is creating a new
thread.
I want to say you have a max number of threads of 25. Not 100% on that. but
I do know there is a max.
so you gotta make sure your checking
that code is not above.
About the sockets..
I dont know if this is a problem but I would assume it would be.. It to make
sure that the port your trying to use is already not in use by another
thread.
You half to wait for them to time out? is ack/nak on. If so then the socket
is it waiting for a comply packet? Are you closing the socket out?
I would like to see a thread appartment in action. If you want some help
with any of this you can send you code to De****@hotmail.com

gotta run and get the kid from daycare. But let me know if any of this
helps.
DevGuy



"Stressed Out Developer" <St******************@discussions.microsoft.com>
wrote in message news:41**********************************@microsof t.com...
We have an application that has a 200 count loop that does the following:

' Each time thru the loop we pass the next IP Address is a range (aka
192.168.4.50 thru 192.168.4.254)
Try
If mUIO_Threads(i) Is Nothing Then
mUIO_Threads(i) = New System.Threading.Thread(AddressOf
mUIO_DAQ(i).InitDAQ)
mUIO_Threads(i).Name = mUIO_DAQ(i).UIO_IPAddr
mUIO_Threads(i).IsBackground = False
Else
If Not mUIO_Threads(i) Is Nothing Then
mUIO_Threads(i).Abort()
While mUIO_Threads(i).ThreadState=Threading.ThreadState. Running
End While
mUIO_Threads(i) = Nothing
End If
mUIO_Threads(i) = New System.Threading.Thread(AddressOf
mUIO_DAQ(i).InitDAQ)
mUIO_Threads(i).Name = mUIO_DAQ(i).UIO_IPAddr
mUIO_Threads(i).IsBackground = False
End If
mUIO_Threads(i).Start()

stime = Environment.TickCount
While (Environment.TickCount < stime + 100) And (Not
mUIO_DAQ(i).UIO_Opened)
System.Windows.Forms.Application.DoEvents()
End While

Catch ex As Threading.ThreadStateException
Debug.WriteLine("Poll: Threading Error <" & ex.ToString & ">")
Catch ex As Exception
Debug.WriteLine("Poll: Exception Error <" & ex.ToString & ">")
End Try


The call to "InitDAQ" creates a TCPIP socket and calls
System.Net.Sockets.Socket.Connect as follows:

Me.m_Socket = New
System.Net.Sockets.Socket(Net.Sockets.AddressFamil y.InterNetwork,
SocketType.Stream, Net.Sockets.ProtocolType.IP)
ipAddressEndPoint = New
System.Net.IPEndPoint(IpAddressArg.Parse(pchIpAddr essArg), nPort)
OpenSockets = SIOMM_OK
Me.m_Socket.Connect(ipAddressEndPoint)
Some of the IP Addresses passed are not online and therefore will fail to
connect. Our test has ip 192.168.4.50 valid and also 192.168.4.253 and
192.168.4.254 valid. All others in the range were offline. We get a
connection working with 192.168.4.50 and eventually after 5 minutes get
the
other two also. If we cut the range down to 192.168.4.250 to
192.168.4.254
we connect to 253 and 254 immediately on the first time throught the loop.
If we adjust the range to 192.168.4.240 to 192.168.4.254, we don't connect
the first time through the loop but we do get it the second time through.
It
is almost as if we can only get data from the first 10 attempted sockets
and
have to wait for them to timeout before we can get data from the next ten.
We have tried setting the socket to Non-Blocking but that didn't seem to
help. We do see spurts of 10 threads exiting at one time. Any ideas

Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Gabriele Farina | last post: by
reply views Thread by Gonçalo Rodrigues | last post: by
2 posts views Thread by Paul A. Steckler | last post: by
10 posts views Thread by Eric | last post: by
reply views Thread by Saiars | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.