BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Searching for potential servers

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Internet Socket)
View previous topic :: View next topic  
Author Message
Andrue Cope [TeamB]
Guest





PostPosted: Tue May 17, 2005 2:03 pm    Post subject: Searching for potential servers Reply with quote



I already have code in place to set up client/server links across our
network and it works well. Unfortunately I now need to be able search
for available servers so that I can get the user to pick one from a
list.

My problem is that I have several hundred potential addresses to
search. I can't go trying to establish a TClientSocket connection to
every possible address because that would take hours. Instead I'm
looking at sending a Ping but to a particular port. I thought I'd found
some code that did exactly that but it seems not:

saDest.sin_addr.s_addr = *((u_long FAR *) (lpHost->h_addr));
saDest.sin_family = AF_INET;
saDest.sin_port = Port;

// Send ICMP echo request
SendEchoRequest(rawSocket, &saDest);

// Use select() to wait for data to be received
nRet = WaitForEchoReply(rawSocket);
if (nRet == SOCKET_ERROR)
return false;

if (!nRet)
return false;

...this doesn't seem to care what port number you use.

I suppose in effect I'm looking for some port sniffing code :-/

Alternatively how else would you implement a system where you could
have half a dozen servers scattered across a couple of hundred
addresses? FWIW the servers are being used to remotely access desktop
machines in a very ad-hoc manner. They are brought up/taken down as
needed and rarely stay active for more than a couple of hours.

Basically if 'Jim' has a problem he brings up the server for an hour or
two while 'Bob' investigates.
--
Andrue Cope [TeamB]
[Bicester, Uk]
http://info.borland.com/newsgroups/guide.html
Back to top
Andrue Cope [TeamB]
Guest





PostPosted: Tue May 17, 2005 2:11 pm    Post subject: Re: Searching for potential servers Reply with quote



Actually sending a single broadcast request would probably be better
but a minor constraint here is development time. This project isn't
worth spending days on unfortunately and is so rarely used that the
occasional flood of a few hundred pings going round our network can be
tolerated :)

--
Andrue Cope [TeamB]
[Bicester, Uk]
http://info.borland.com/newsgroups/guide.html
Back to top
Hans Galema
Guest





PostPosted: Tue May 17, 2005 2:55 pm    Post subject: Re: Searching for potential servers Reply with quote



Andrue Cope [TeamB] wrote:
Quote:
I already have code in place to set up client/server links across our
network and it works well. Unfortunately I now need to be able search
for available servers so that I can get the user to pick one from a
list.

If I understand correct you mean your serverapp's equipped with a TServerSocket ?

Quote:
My problem is that I have several hundred potential addresses to
search. I can't go trying to establish a TClientSocket connection to
every possible address because that would take hours.

Well a little bit less I think. But anyhow.

You could provide every server with a UDP server.
Then add an UDP client to the app(s) that "search for available servers".
The UDP client does a broadcast upon which every UDP server that
is "in the air" sends its ip.

Hans.

Back to top
Bob Gonder
Guest





PostPosted: Tue May 17, 2005 3:29 pm    Post subject: Re: Searching for potential servers Reply with quote

Andrue Cope [TeamB] wrote:

Quote:
Actually sending a single broadcast request would probably be better
but a minor constraint here is development time. This project isn't
worth spending days on unfortunately and is so rarely used that the
occasional flood of a few hundred pings going round our network can be
tolerated Smile

server sets up a TCP listen()
client and server sets up a UDP Recv Any
client UDP broadcasts a datagram with a "request" flag in it.
client sees the datagram, ignores it because it is a request.
server sees the datagram, that it is a request, and if
not-busy replies with a datagram "answer" datagram.
server sees the answer datagram and ignores it.
client sees the answer datgram and logs it.
After 1 second, client displays the list of answers.
client selects one and TCP connects to it.
server accepts() incoming connection
server turns off it's UDP Recv Any.

Since you have TCP running, here's UDPListen...

static SOCKET ListenSocket; //global so main can use it
struct UDPPACKET u; // define UDPPACKET as you like

/* Thread */
#pragma argsused
static unsigned long _stdcall UdpListeningThread(void*x)
{
struct sockaddr_in ListenAddr;
struct sockaddr_in SenderAddr;
int SenderAddrSize = sizeof(SenderAddr);
int t = TRUE;
int received;

//-----------------------------------------------
// Create a receiver socket to receive datagrams
//Notify("Creating UDP Socket");
ListenSocket = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP );
if( ListenSocket != INVALID_SOCKET )
{
/* need this for multiple listeners */
if( 0 == setsockopt( ListenSocket, SOL_SOCKET,
SO_REUSEADDR, (char*)&t, sizeof(t) ))
{
//-----------------------------------------------
// Bind the socket to any address and the specified port.
ListenAddr.sin_family = AF_INET;
ListenAddr.sin_port = htons( UDP_CS_PORT );
ListenAddr.sin_addr.s_addr = htonl( INADDR_ANY );

//Notify("Binding UDP Socket");
if( SOCKET_ERROR != bind( ListenSocket,
(SOCKADDR *) &ListenAddr,
sizeof(ListenAddr) ) )
{
while( !Canceled )
{
Aborted = FALSE;
//-----------------------------------------------
// Call the recvfrom function to receive datagrams on
// the bound socket.
// This blocks. Close ListenSocket from MainThread to
// blast us out.
//Notify( "Ready to Receive Connections..." );
received = recvfrom( ListenSocket, (char*)&u,
sizeof(UDPPACKET), 0,
(SOCKADDR *)&SenderAddr,
&SenderAddrSize );

We know who this datagram came from:
wsprintf( entry,"Received %d bytes from %s:%d - %.11srn",
received,
inet_ntoa(SenderAddr.sin_addr),
ntohs(SenderAddr.sin_port),
&u.Name );

If you have the server pass it's name in the datagram (u.Name), it'll
make the picklist easier.




Back to top
Andrue Cope [TeamB]
Guest





PostPosted: Tue May 17, 2005 3:39 pm    Post subject: Re: Searching for potential servers Reply with quote

Oooh - interesting code. I'll be looking at that tomorrow AM. Thx.

--
Andrue Cope [TeamB]
[Bicester, Uk]
http://info.borland.com/newsgroups/guide.html
Back to top
Andrue Cope [TeamB]
Guest





PostPosted: Tue May 17, 2005 3:47 pm    Post subject: Re: Searching for potential servers Reply with quote

Hans Galema wrote:

Quote:
If I understand correct you mean your serverapp's equipped with a
TServerSocket ?

Yes.

Quote:
You could provide every server with a UDP server.

I had a look at that earlier but couldn't find much documentation. Bob
seems to have solved that problem though.

The other area I toyed with was UPnP. Although not essential it would
add further value to the project. Right now it's almost a case of a
technology without a purpose. The more bells and whistles I can add the
better as long as I don't spend too much time on it.

Some of the items on my to-do list are really quite cool (including my
favourite:exporting DLL functions across TCP/IP) but until I can
bolster the use-case they're not going to happen.
--
Andrue Cope [TeamB]
[Bicester, Uk]
http://info.borland.com/newsgroups/guide.html

Back to top
Andrue Cope [TeamB]
Guest





PostPosted: Wed May 18, 2005 1:33 pm    Post subject: Re: Searching for potential servers Reply with quote

That was relatively painless - thanks a lot.

It also opens the door up to UPnP so I might be able to sneak some time
in there and push things even further.

--
Andrue Cope [TeamB]
[Bicester, Uk]
http://info.borland.com/newsgroups/guide.html
Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Internet Socket) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.