By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,054 Members | 1,304 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,054 IT Pros & Developers. It's quick & easy.

Socket recive problem

P: n/a
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;IsClustere d;No;Version;8.00.194;tcp;2369;npMASTER\pipe\M$BKU PEXEC\sql\query;;ServerName;MASTER;InstanceName;SQ LEXPRE;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[i]));
}
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;

Sep 28 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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]

"Rene Sørensen" <re**@deltadk.dkwrote in message
news:3o********************************@4ax.com...
| 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;IsClustere d;No;Version;8.00.194;tcp;2369;npMASTER\pipe\M$BKU PEXEC\sql\query;;ServerName;MASTER;InstanceName;SQ LEXPRE;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[i]));
| }
| 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;
|
Sep 29 '06 #2

P: n/a
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?

On Thu, 28 Sep 2006 21:37:31 -0400, "William Stacey [MVP]"
<wi************@gmail.comwrote:
>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.
Sep 29 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.