473,394 Members | 2,020 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 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 2324
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Gabriele Farina | last post by:
Hi, I'd like to manage multiple socket connection without having to use Threads. I'd like to manage every connection when it is returned from socket_Accept(), without having to wait for the...
0
by: Gonçalo Rodrigues | last post by:
Hi, I have a problem with threads and sockets. I'll try to describe the problem in words with pseudo-code. I've been working on a few classes to make it easier to work with threads. This...
2
by: Paul A. Steckler | last post by:
I need to write a TCP/IP server in C# that can handle multiple connections. My first try was to use TCPListener instances in multiple .NET threads. Of course, I got an exception from...
10
by: Eric | last post by:
I'm looking at this page in the MSDN right here: ms-help://MS.MSDNQTR.2003FEB.1033/cpref/html/frlrfsystemcollectionsarraylist classsynchronizedtopic2.htm (or online here:...
2
by: Ronodev.Sen | last post by:
the way my program needs to go is -- 1) open a socket and listen on it 2) moment a client connects to the socket - process some data (by sending it to another machine), get the result and send...
6
by: roblugt | last post by:
I have what I imagine is a well-known .Net networking problem, but even though I've Googled for some time I've not yet come across a thread where this has been fully explained... There is a...
2
by: dariophoenix | last post by:
Hi, I am trying to encapsulate Linux sockets and POSIX threads in C++ classes (I work in Knoppix, using KDevelop). Since sockets and threads are new to me, I searched for example code and found...
2
by: jgbid | last post by:
Hi, I'm trying to build an IP Scanner inc c# for a specific port (80) and for specific IP Ranges. For example 24.36.148.1 to 24.36.148.255 My first step was to use TcpClient, but there are...
0
by: Qui Sum | last post by:
I write ip/port scanner so users using search on our LAN know if the particular ftp is online or not. The class I write has the following structure: public class IPScanner { public...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.