Article View: borland.public.cppbuilder.internet.socket
Article #3900Re: Searching for potential servers
From: Bob Gonder
Date: Tue, 17 May 2005 15:29
Date: Tue, 17 May 2005 15:29
80 lines
3860 bytes
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.
Technical Details
Message-ID:
<lj1k81dt1unk1bpas5i3bkhvj8k8dmvorp@4ax.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!news-out.nntp.be!propagator2-sterling.newsfeeds.com!news-in.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mailReferences:
<4289f9bf$1@newsgroups.borland.com> <4289fbaf@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,122cee9b7cf53719 X-Google-Attributes: gid11508e,public X-Google-Language: ENGLISH,ASCII-7-bit From: Bob Gonder <notbg@notmindspring.invalid> Newsgroups: borland.public.cppbuilder.internet.socket Subject: Re: Searching for potential servers Date: Tue, 17 May 2005 08:29:07 -0700 Organization: MindSpring Enterprises, Inc. Reply-To: notbg@notmindspring.invalid Message-ID: <lj1k81dt1unk1bpas5i3bkhvj8k8dmvorp@4ax.com> References: <4289f9bf$1@newsgroups.borland.com> <4289fbaf@newsgroups.borland.com> X-Newsreader: Forte Agent 1.93/32.576 English (American) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 67.77.144.171 X-Trace: newsgroups.borland.com 1116343747 67.77.144.171 (17 May 2005 08:29:07 -0700) Lines: 83 Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!news-out.nntp.be!propagator2-sterling.newsfeeds.com!news-in.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mail