(Simon)
10/9/2002 9:07:00 PM
I have an application that has to send a small number of bytes (<12)
to many devices, and listen for slightly larger responses (<100) on
UDP sockets.
The app has to send the bytes to each IP address in a subnet. For
eample 213.215.154.1, 213.215.154.2, 213.215.154.3
......213.215.154.254.
Also it has to do this for several subnets.
All of this is MUST be acheived on the same port number, therefore it
has to continue to send bytes to addresses in the list, whilst at the
same time listen for and process any responses on the same port.
I do this by creating a new udp socket and binding it to a local end
piont;
mSock = New Socket(AddressFamily.InterNetwork, SocketType.Dgram,
ProtocolType.Udp)
Dim iLocalPort As Integer = 8450
Dim ipHostInfo As IPHostEntry = Dns.Resolve(Dns.GetHostName())
ipHostAddress = ipHostInfo.AddressList(0)
Dim ipepLocal = New IPEndPoint(ipHostAddress, iLocalPort)
mSock.Bind(ipepLocal)
Then I call mSock.BeginRecieve and supply the defined callback
delegate;
mSock.BeginReceive(m_bRecBuff, 0, 1024, SocketFlags.None, New
AsyncCallback(AddressOf OnReceive), Nothing)
So far so good, now I start looping through my all IP addresses and
use Send on each;
For m_iCnt = 254 To 1 Step -1
ipAddress = ipAddress.Parse(strIPAddress & CStr(m_iCnt))
m_ipepRemote = New IPEndPoint(ipAddress, 8450)
mSock.SendTo(m_bSendBuff, 0, m_bSendBuff.Length, SocketFlags.None,
m_ipepRemote)
Next
This appears to work OK, as I would have expected, with one bug
exception.
When data is sent to my socket, OnReceive is executed. OnReceive
looks something like this;
Public Sub OnReceive(ByVal ar As IAsyncResult)
iBytesRead = mSock.EndReceive(ar)
mSock.BeginReceive(m_bRecBuff, 0, 1024, SocketFlags.None, New
AsyncCallback(AddressOf OnReceive), Nothing)
end Sub
I call BeginReceive again, I want to continue listening for responses
from all those IPAddresses I am sending data to.
Now the problem, sometime calls to EndReceive and BeginReceive result
in a SocketException being thrown, and it appear that once it is
thrown it continues to be thrown when attempts are made to call the.
The exception reads;
System.Net.Sockets.SocketException: An existing connection was
forcibly closed by the remote host
CAn anyone help me. I have no idea what is causing this exception,
and it is stoping my app from working. Mostly the socket will receive
just fine for the first few addresses that return data, then this
problem occurs and I cannot process any more data.
Any help, explainations or work arounds would be greatly appriciated.
TIA
Simon