Socket recive problem

R

Rene Sørensen

I'm using .NET 2.0 VS 2005

I'm creating a function that dos something similar to the.
SmoApplication.EnumAvailableSqlServers() function. But for som resone
I get an error or do i?. The problem is that the program just return
from the function when it reach the recive part( reviced =
socket.Receive(bytBuffer); ), below here you can se the code. I used
eathereal to check the package that is send and reviced, that looks
all fine. I do revice the correct package from the SQLServer. Dos
anyone have a clue here? im lost after 3 days hard work on it.

Thanks is advanced

Rene


// RECIVED STRING FROM SQL SERVER
ServerName;MASTER;InstanceName;BKUPEXEC;IsClustered;No;Version;8.00.194;tcp;2369;npMASTER\pipe\M$BKUPEXEC\sql\query;;ServerName;MASTER;InstanceName;SQLEXPRE;Islustered;No;Version;9.00.2047.00;;

ArrayList servers = new ArrayList();
ArrayList list = null;
SqlServerInfo info = new SqlServerInfo();
Socket socket = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp);

socket.EnableBroadcast = true;
socket.ReceiveTimeout = 3000;


try
{
// ask for all MSSQL servers
byte[] msg = new byte[] { 0x02 };
byte[] bytBuffer = new byte[1024];
int reviced = 0;
IPEndPoint endP = new IPEndPoint(IPAddress.Broadcast, 1434);
socket.SendTo(msg, endP);
do
{
// IT JUST STOPS HERE AND RETURN FROM THE FUNCTION WITH AN
// EMPTY LIST SOMETIMES IT THROWS AN EXCEPTION
// broadcast A connection attempt failed because the connected
// party did not properly respond after a period of time, or
// established connection failed because connected host has
// failed to respond socket recive

reviced = socket.Receive(bytBuffer);

// There can be more than one instance on the same server
list = info.ParseServers(bytBuffer);
for (int i = 0; i < list.Count; i++)
{
servers.Add(new SqlServerInfo(null, (string)list));
}
socket.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReceiveTimeout, 300);
} while (reviced != 0);
}
catch (SocketException socex)
{
const int WSAETIMEDOUT = 10060;// Connection timed out.
const int WSAEHOSTUNREACH = 10065; // No route to host.

// Re-throw if it's not a timeout.
if (socex.ErrorCode == WSAETIMEDOUT || socex.ErrorCode ==
WSAEHOSTUNREACH)
{
// Just go away
}
else
{
throw;
}
}
finally
{
socket.Close();
}
// Copy from the untyped but expandable ArrayList, to a
// type-safe but fixed array of SqlServerInfos.
SqlServerInfo[] aServers = new SqlServerInfo[servers.Count];
servers.CopyTo(aServers);
return aServers;
 
W

William Stacey [MVP]

Why are you setting receive timeout to 300ms? Also, remember you don't
receive messages, you receive bytes. So any receive could return 1 or more
bytes until receive returns 0 (i.e. shutdown). So your
ParseServers(bytBuffer) method may fail at times.

--
William Stacey [MVP]

| I'm using .NET 2.0 VS 2005
|
| I'm creating a function that dos something similar to the.
| SmoApplication.EnumAvailableSqlServers() function. But for som resone
| I get an error or do i?. The problem is that the program just return
| from the function when it reach the recive part( reviced =
| socket.Receive(bytBuffer); ), below here you can se the code. I used
| eathereal to check the package that is send and reviced, that looks
| all fine. I do revice the correct package from the SQLServer. Dos
| anyone have a clue here? im lost after 3 days hard work on it.
|
| Thanks is advanced
|
| Rene
|
|
| // RECIVED STRING FROM SQL SERVER
|
ServerName;MASTER;InstanceName;BKUPEXEC;IsClustered;No;Version;8.00.194;tcp;2369;npMASTER\pipe\M$BKUPEXEC\sql\query;;ServerName;MASTER;InstanceName;SQLEXPRE;Islustered;No;Version;9.00.2047.00;;
|
| ArrayList servers = new ArrayList();
| ArrayList list = null;
| SqlServerInfo info = new SqlServerInfo();
| Socket socket = new Socket(AddressFamily.InterNetwork,
| SocketType.Dgram, ProtocolType.Udp);
|
| socket.EnableBroadcast = true;
| socket.ReceiveTimeout = 3000;
|
|
| try
| {
| // ask for all MSSQL servers
| byte[] msg = new byte[] { 0x02 };
| byte[] bytBuffer = new byte[1024];
| int reviced = 0;
| IPEndPoint endP = new IPEndPoint(IPAddress.Broadcast, 1434);
| socket.SendTo(msg, endP);
| do
| {
| // IT JUST STOPS HERE AND RETURN FROM THE FUNCTION WITH AN
| // EMPTY LIST SOMETIMES IT THROWS AN EXCEPTION
| // broadcast A connection attempt failed because the connected
| // party did not properly respond after a period of time, or
| // established connection failed because connected host has
| // failed to respond socket recive
|
| reviced = socket.Receive(bytBuffer);
|
| // There can be more than one instance on the same server
| list = info.ParseServers(bytBuffer);
| for (int i = 0; i < list.Count; i++)
| {
| servers.Add(new SqlServerInfo(null, (string)list));
| }
| socket.SetSocketOption(SocketOptionLevel.Socket,
| SocketOptionName.ReceiveTimeout, 300);
| } while (reviced != 0);
| }
| catch (SocketException socex)
| {
| const int WSAETIMEDOUT = 10060;// Connection timed out.
| const int WSAEHOSTUNREACH = 10065; // No route to host.
|
| // Re-throw if it's not a timeout.
| if (socex.ErrorCode == WSAETIMEDOUT || socex.ErrorCode ==
| WSAEHOSTUNREACH)
| {
| // Just go away
| }
| else
| {
| throw;
| }
| }
| finally
| {
| socket.Close();
| }
| // Copy from the untyped but expandable ArrayList, to a
| // type-safe but fixed array of SqlServerInfos.
| SqlServerInfo[] aServers = new SqlServerInfo[servers.Count];
| servers.CopyTo(aServers);
| return aServers;
|
 
R

Rene Sørensen

Thanks for your reply
Why are you setting receive timeout to 300ms?
Well so fare it never reach the point where it's set so the problem
has to be found elseware.
Also, remember you don't receive messages, you receive bytes.
So any receive could return 1 or more
bytes until receive returns 0 (i.e. shutdown). So your
ParseServers(bytBuffer) method may fail at times.
Yes your are rigth about that, but problem is it never reach the
"ParseServers(bytBuffer)" it just stops at the
"socket.Recive(byteBuffer)" and i mean realy stops.
It never gets to this point "list = info.ParseServers(bytBuffer);" or
futher, only when a exception ocure. It's like
"socket.Recive(byteBuffer)" acts like an return for the fucntion, for
me thats prety wierd?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top