🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.cppbuilder.internet.socket
7 messages
7 total messages Started by Andrue Cope [TeamB] Tue, 17 May 2005 14:03
Searching for potential servers
#3898
Author: Andrue Cope [TeamB]
Date: Tue, 17 May 2005 14:03
42 lines
2329 bytes
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
Re: Searching for potential servers
#3901
Author: Andrue Cope [TeamB]
Date: Tue, 17 May 2005 14:11
10 lines
1281 bytes
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
Re: Searching for potential servers
#3899
Author: Hans Galema
Date: Tue, 17 May 2005 14:55
20 lines
1905 bytes
Andrue Cope [TeamB] wrote:
> 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 ?

> 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.
Re: Searching for potential servers
#3900
Author: Bob Gonder
Date: Tue, 17 May 2005 15:29
80 lines
3860 bytes
Andrue Cope [TeamB] wrote:

>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 :)

	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 - %.11s\r\n",
			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.
Re: Searching for potential servers
#3904
Author: Andrue Cope [TeamB]
Date: Tue, 17 May 2005 15:39
6 lines
1213 bytes
Oooh - interesting code. I'll be looking at that tomorrow AM. Thx.

-- 
Andrue Cope [TeamB]
[Bicester, Uk]
http://info.borland.com/newsgroups/guide.html
Re: Searching for potential servers
#3902
Author: Andrue Cope [TeamB]
Date: Tue, 17 May 2005 15:47
24 lines
1839 bytes
Hans Galema wrote:

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

Yes.

> 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
Re: Searching for potential servers
#3906
Author: Andrue Cope [TeamB]
Date: Wed, 18 May 2005 13:33
9 lines
1183 bytes
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
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads