Howto find multiple servers using TCP

I have to search my subnet to look for servers that are listening on port 
30715. I am using one thread of a derived class of CAsyncSocket. I am using 
CEvents with a 200 milisecond timeout, if there is no connection within the 
200 miliseconds, then I know there is no serer. However searching from 
192.168.1.1 to 192.168.1.254 takes a long time. Is there a better way?

0
12/11/2007 9:20:01 PM
vc.mfc 33608 articles. 0 followers. Follow

9 Replies
794 Views

Similar Articles

[PageSpeed] 26

I can't think of another way.

But if the server program is your program, then I have a suggestion.
I have a server that alot of clients connect to, and I really didn't want to 
have to input the server address in every client.  So what I did was I used 
a multicast socket to find the server.  The server listens to traffic on a 
particular multicast address, and when the clients want to find the server 
application, they would send a message (where are you) using a multicast 
socket with that particular address, the server would receive the message 
and send a reply (Here I am) with its IP address. Now if there are more than 
one server, the client would get multiple "Here I am" replies. This entire 
process takes less than a second.

AliR.


"amccombs" <amccombs@discussions.microsoft.com> wrote in message 
news:129DBBB8-AD79-4588-91E0-0DF847EB11FB@microsoft.com...
>I have to search my subnet to look for servers that are listening on port
> 30715. I am using one thread of a derived class of CAsyncSocket. I am 
> using
> CEvents with a 200 milisecond timeout, if there is no connection within 
> the
> 200 miliseconds, then I know there is no serer. However searching from
> 192.168.1.1 to 192.168.1.254 takes a long time. Is there a better way?
> 


0
AliR3470 (3235)
12/11/2007 9:43:16 PM
Can you multicast on TCP?
 

"AliR (VC++ MVP)" wrote:

> I can't think of another way.
> 
> But if the server program is your program, then I have a suggestion.
> I have a server that alot of clients connect to, and I really didn't want to 
> have to input the server address in every client.  So what I did was I used 
> a multicast socket to find the server.  The server listens to traffic on a 
> particular multicast address, and when the clients want to find the server 
> application, they would send a message (where are you) using a multicast 
> socket with that particular address, the server would receive the message 
> and send a reply (Here I am) with its IP address. Now if there are more than 
> one server, the client would get multiple "Here I am" replies. This entire 
> process takes less than a second.
> 
> AliR.
> 
> 
> "amccombs" <amccombs@discussions.microsoft.com> wrote in message 
> news:129DBBB8-AD79-4588-91E0-0DF847EB11FB@microsoft.com...
> >I have to search my subnet to look for servers that are listening on port
> > 30715. I am using one thread of a derived class of CAsyncSocket. I am 
> > using
> > CEvents with a 200 milisecond timeout, if there is no connection within 
> > the
> > 200 miliseconds, then I know there is no serer. However searching from
> > 192.168.1.1 to 192.168.1.254 takes a long time. Is there a better way?
> > 
> 
> 
> 
0
12/11/2007 10:48:00 PM
No only UDP.  But the Multicast is only there for the server finding and 
then you would want to switch to a normal TCP socket once you find the 
server.

AliR.


"amccombs" <amccombs@discussions.microsoft.com> wrote in message 
news:902C317D-01C5-4FE1-97D9-B93F04749975@microsoft.com...
> Can you multicast on TCP?
>
>
> "AliR (VC++ MVP)" wrote:
>
>> I can't think of another way.
>>
>> But if the server program is your program, then I have a suggestion.
>> I have a server that alot of clients connect to, and I really didn't want 
>> to
>> have to input the server address in every client.  So what I did was I 
>> used
>> a multicast socket to find the server.  The server listens to traffic on 
>> a
>> particular multicast address, and when the clients want to find the 
>> server
>> application, they would send a message (where are you) using a multicast
>> socket with that particular address, the server would receive the message
>> and send a reply (Here I am) with its IP address. Now if there are more 
>> than
>> one server, the client would get multiple "Here I am" replies. This 
>> entire
>> process takes less than a second.
>>
>> AliR.
>>
>>
>> "amccombs" <amccombs@discussions.microsoft.com> wrote in message
>> news:129DBBB8-AD79-4588-91E0-0DF847EB11FB@microsoft.com...
>> >I have to search my subnet to look for servers that are listening on 
>> >port
>> > 30715. I am using one thread of a derived class of CAsyncSocket. I am
>> > using
>> > CEvents with a 200 milisecond timeout, if there is no connection within
>> > the
>> > 200 miliseconds, then I know there is no serer. However searching from
>> > 192.168.1.1 to 192.168.1.254 takes a long time. Is there a better way?
>> >
>>
>>
>> 


0
AliR3470 (3235)
12/12/2007 4:06:37 PM
If I send out a broadcast packet on UDP, will my class that is derived from 
CAsyncSocket::OnReceive get called when data comes back?

(Other references seem to think that I have sit and wait for a reply at the 
next line after sending)


"AliR (VC++ MVP)" wrote:

> No only UDP.  But the Multicast is only there for the server finding and 
> then you would want to switch to a normal TCP socket once you find the 
> server.
> 
> AliR.
> 
> 
> "amccombs" <amccombs@discussions.microsoft.com> wrote in message 
> news:902C317D-01C5-4FE1-97D9-B93F04749975@microsoft.com...
> > Can you multicast on TCP?
> >
> >
> > "AliR (VC++ MVP)" wrote:
> >
> >> I can't think of another way.
> >>
> >> But if the server program is your program, then I have a suggestion.
> >> I have a server that alot of clients connect to, and I really didn't want 
> >> to
> >> have to input the server address in every client.  So what I did was I 
> >> used
> >> a multicast socket to find the server.  The server listens to traffic on 
> >> a
> >> particular multicast address, and when the clients want to find the 
> >> server
> >> application, they would send a message (where are you) using a multicast
> >> socket with that particular address, the server would receive the message
> >> and send a reply (Here I am) with its IP address. Now if there are more 
> >> than
> >> one server, the client would get multiple "Here I am" replies. This 
> >> entire
> >> process takes less than a second.
> >>
> >> AliR.
> >>
> >>
> >> "amccombs" <amccombs@discussions.microsoft.com> wrote in message
> >> news:129DBBB8-AD79-4588-91E0-0DF847EB11FB@microsoft.com...
> >> >I have to search my subnet to look for servers that are listening on 
> >> >port
> >> > 30715. I am using one thread of a derived class of CAsyncSocket. I am
> >> > using
> >> > CEvents with a 200 milisecond timeout, if there is no connection within
> >> > the
> >> > 200 miliseconds, then I know there is no serer. However searching from
> >> > 192.168.1.1 to 192.168.1.254 takes a long time. Is there a better way?
> >> >
> >>
> >>
> >> 
> 
> 
> 
0
12/12/2007 5:04:01 PM
Here is how things work with UDP.  You will have to create a UDP socket 
(SOCK_DGRAM) instead of SOCK_STREAM.  And since it's a connection-less 
socket you don't call Connect.   When a packet is sent using a UDP socket 
(with SendTo) the OnReceive method gets called and you have to use 
ReceiveFrom instead of Receive to get the data.  Now there are some 
differences between multicast and regular udp packets, multicast packets 
only get routed to the computers that have subscribed to the multicast 
address, and multicast can cross subnet boundries where UDP broadcasts 
cannot.

Here is some sample code on how to create a sending and receiving multicast 
socket, and how you might do your OnReceive method

BOOL LSSocket::CreateReceivingMulticastSocket(LPCTSTR strGroupIP, UINT 
nGroupPort,DWORD &Error)
{
   /* Create socket for receiving packets from multicast group */
   if(!CAsyncSocket::Create(nGroupPort, SOCK_DGRAM,FD_READ))
   {
      Error = GetLastError();
      return FALSE;
   }

   BOOL bMultipleApps = TRUE;  /* allow reuse of local port if needed */
   SetSockOpt(SO_REUSEADDR, (void*)&bMultipleApps, sizeof(BOOL), 
SOL_SOCKET);

   /* Join the multicast group */
   m_mrMReq.imr_multiaddr.s_addr = inet_addr(strGroupIP); /* group addr */
   m_mrMReq.imr_interface.s_addr = htons(INADDR_ANY);  /* use default */
   if(setsockopt(m_hSocket, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char FAR 
*)&m_mrMReq, sizeof(m_mrMReq)) < 0)
   {
      Error = GetLastError();
      return FALSE;
   }

   m_UDP = TRUE;
   return TRUE;
}

BOOL LSSocket::CreateSendingSocket(UINT nTTL, BOOL bLoopBack,DWORD &Error)
{
   if(!Create(0, SOCK_DGRAM))  // Create an unconnected UDP socket
   {
      Error = GetLastError();
      return FALSE;
   }

   SetTTL(nTTL,Error);   // Set Time to Live as specified by user

   SetLoopBack(bLoopBack);    // Enable/Disable Loopback
   m_UDP = TRUE;
   return TRUE;
}

void LSSocket::OnReceive(int nErrorCode)
{
   char *Buffer = new char[30720];   //30K Buffer

   memset(Buffer,0,30720);

   int nRead = 0;
   CString Address;
   UINT Port = 0;
   if (m_UDP)
   {
      nRead = CAsyncSocket::ReceiveFrom(Buffer,30720,Address,Port);
   }
   else
   {
      nRead = CAsyncSocket::Receive(Buffer,30720);
   }

   if (nRead == 30720)
   {
      TRACE("Socket Receive Overflow\n");
   }

   if (nRead != SOCKET_ERROR)
   {
      if (nRead)
      {
         ProcessMessage(Buffer,nRead,Address,Port);
      }
   }
   else
   {
      CString Text;
      Text.Format("Socket Receive Error number %d\n", GetLastError());
      TRACE(Text);
   }

   delete [] Buffer;
   CAsyncSocket::OnReceive(nErrorCode);
}


AliR.



"amccombs" <amccombs@discussions.microsoft.com> wrote in message 
news:2882BF79-94A3-4745-A2F8-E4108A0E5C70@microsoft.com...
> If I send out a broadcast packet on UDP, will my class that is derived 
> from
> CAsyncSocket::OnReceive get called when data comes back?
>
> (Other references seem to think that I have sit and wait for a reply at 
> the
> next line after sending)
>
>
> "AliR (VC++ MVP)" wrote:
>
>> No only UDP.  But the Multicast is only there for the server finding and
>> then you would want to switch to a normal TCP socket once you find the
>> server.
>>
>> AliR.
>>
>>
>> "amccombs" <amccombs@discussions.microsoft.com> wrote in message
>> news:902C317D-01C5-4FE1-97D9-B93F04749975@microsoft.com...
>> > Can you multicast on TCP?
>> >
>> >
>> > "AliR (VC++ MVP)" wrote:
>> >
>> >> I can't think of another way.
>> >>
>> >> But if the server program is your program, then I have a suggestion.
>> >> I have a server that alot of clients connect to, and I really didn't 
>> >> want
>> >> to
>> >> have to input the server address in every client.  So what I did was I
>> >> used
>> >> a multicast socket to find the server.  The server listens to traffic 
>> >> on
>> >> a
>> >> particular multicast address, and when the clients want to find the
>> >> server
>> >> application, they would send a message (where are you) using a 
>> >> multicast
>> >> socket with that particular address, the server would receive the 
>> >> message
>> >> and send a reply (Here I am) with its IP address. Now if there are 
>> >> more
>> >> than
>> >> one server, the client would get multiple "Here I am" replies. This
>> >> entire
>> >> process takes less than a second.
>> >>
>> >> AliR.
>> >>
>> >>
>> >> "amccombs" <amccombs@discussions.microsoft.com> wrote in message
>> >> news:129DBBB8-AD79-4588-91E0-0DF847EB11FB@microsoft.com...
>> >> >I have to search my subnet to look for servers that are listening on
>> >> >port
>> >> > 30715. I am using one thread of a derived class of CAsyncSocket. I 
>> >> > am
>> >> > using
>> >> > CEvents with a 200 milisecond timeout, if there is no connection 
>> >> > within
>> >> > the
>> >> > 200 miliseconds, then I know there is no serer. However searching 
>> >> > from
>> >> > 192.168.1.1 to 192.168.1.254 takes a long time. Is there a better 
>> >> > way?
>> >> >
>> >>
>> >>
>> >>
>>
>>
>> 


0
AliR3470 (3235)
12/12/2007 5:37:24 PM
That works great, Thanks.

if you wanted to send back the same thing you got to the same IP that got 
from in the
OnReceive ReceiveFrom section, what would you do?

The SendTo option with SOCKADDR, I don't know what to put for 
SOCKADDR.sa_data.
I have the IP from the reply, so anther Broadcast message for a reply 
doesn't seem the correct thing to do.

BOOL bMultipleApps = TRUE;
SetSockOpt(SO_BROADCAST, (void*)&bMultipleApps, sizeof(BOOL), SOL_SOCKET);

CAsyncSocket::SendTo(Buffer, nRead, Port, NULL, MSG_DONTROUTE);


"AliR (VC++ MVP)" wrote:

> Here is how things work with UDP.  You will have to create a UDP socket 
> (SOCK_DGRAM) instead of SOCK_STREAM.  And since it's a connection-less 
> socket you don't call Connect.   When a packet is sent using a UDP socket 
> (with SendTo) the OnReceive method gets called and you have to use 
> ReceiveFrom instead of Receive to get the data.  Now there are some 
> differences between multicast and regular udp packets, multicast packets 
> only get routed to the computers that have subscribed to the multicast 
> address, and multicast can cross subnet boundries where UDP broadcasts 
> cannot.
> 
> Here is some sample code on how to create a sending and receiving multicast 
> socket, and how you might do your OnReceive method
> 
> BOOL LSSocket::CreateReceivingMulticastSocket(LPCTSTR strGroupIP, UINT 
> nGroupPort,DWORD &Error)
> {
>    /* Create socket for receiving packets from multicast group */
>    if(!CAsyncSocket::Create(nGroupPort, SOCK_DGRAM,FD_READ))
>    {
>       Error = GetLastError();
>       return FALSE;
>    }
> 
>    BOOL bMultipleApps = TRUE;  /* allow reuse of local port if needed */
>    SetSockOpt(SO_REUSEADDR, (void*)&bMultipleApps, sizeof(BOOL), 
> SOL_SOCKET);
> 
>    /* Join the multicast group */
>    m_mrMReq.imr_multiaddr.s_addr = inet_addr(strGroupIP); /* group addr */
>    m_mrMReq.imr_interface.s_addr = htons(INADDR_ANY);  /* use default */
>    if(setsockopt(m_hSocket, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char FAR 
> *)&m_mrMReq, sizeof(m_mrMReq)) < 0)
>    {
>       Error = GetLastError();
>       return FALSE;
>    }
> 
>    m_UDP = TRUE;
>    return TRUE;
> }
> 
> BOOL LSSocket::CreateSendingSocket(UINT nTTL, BOOL bLoopBack,DWORD &Error)
> {
>    if(!Create(0, SOCK_DGRAM))  // Create an unconnected UDP socket
>    {
>       Error = GetLastError();
>       return FALSE;
>    }
> 
>    SetTTL(nTTL,Error);   // Set Time to Live as specified by user
> 
>    SetLoopBack(bLoopBack);    // Enable/Disable Loopback
>    m_UDP = TRUE;
>    return TRUE;
> }
> 
> void LSSocket::OnReceive(int nErrorCode)
> {
>    char *Buffer = new char[30720];   //30K Buffer
> 
>    memset(Buffer,0,30720);
> 
>    int nRead = 0;
>    CString Address;
>    UINT Port = 0;
>    if (m_UDP)
>    {
>       nRead = CAsyncSocket::ReceiveFrom(Buffer,30720,Address,Port);
>    }
>    else
>    {
>       nRead = CAsyncSocket::Receive(Buffer,30720);
>    }
> 
>    if (nRead == 30720)
>    {
>       TRACE("Socket Receive Overflow\n");
>    }
> 
>    if (nRead != SOCKET_ERROR)
>    {
>       if (nRead)
>       {
>          ProcessMessage(Buffer,nRead,Address,Port);
>       }
>    }
>    else
>    {
>       CString Text;
>       Text.Format("Socket Receive Error number %d\n", GetLastError());
>       TRACE(Text);
>    }
> 
>    delete [] Buffer;
>    CAsyncSocket::OnReceive(nErrorCode);
> }
> 
> 
> AliR.
> 
> 
> 
> "amccombs" <amccombs@discussions.microsoft.com> wrote in message 
> news:2882BF79-94A3-4745-A2F8-E4108A0E5C70@microsoft.com...
> > If I send out a broadcast packet on UDP, will my class that is derived 
> > from
> > CAsyncSocket::OnReceive get called when data comes back?
> >
> > (Other references seem to think that I have sit and wait for a reply at 
> > the
> > next line after sending)
> >
> >
> > "AliR (VC++ MVP)" wrote:
> >
> >> No only UDP.  But the Multicast is only there for the server finding and
> >> then you would want to switch to a normal TCP socket once you find the
> >> server.
> >>
> >> AliR.
> >>
> >>
> >> "amccombs" <amccombs@discussions.microsoft.com> wrote in message
> >> news:902C317D-01C5-4FE1-97D9-B93F04749975@microsoft.com...
> >> > Can you multicast on TCP?
> >> >
> >> >
> >> > "AliR (VC++ MVP)" wrote:
> >> >
> >> >> I can't think of another way.
> >> >>
> >> >> But if the server program is your program, then I have a suggestion.
> >> >> I have a server that alot of clients connect to, and I really didn't 
> >> >> want
> >> >> to
> >> >> have to input the server address in every client.  So what I did was I
> >> >> used
> >> >> a multicast socket to find the server.  The server listens to traffic 
> >> >> on
> >> >> a
> >> >> particular multicast address, and when the clients want to find the
> >> >> server
> >> >> application, they would send a message (where are you) using a 
> >> >> multicast
> >> >> socket with that particular address, the server would receive the 
> >> >> message
> >> >> and send a reply (Here I am) with its IP address. Now if there are 
> >> >> more
> >> >> than
> >> >> one server, the client would get multiple "Here I am" replies. This
> >> >> entire
> >> >> process takes less than a second.
> >> >>
> >> >> AliR.
> >> >>
> >> >>
> >> >> "amccombs" <amccombs@discussions.microsoft.com> wrote in message
> >> >> news:129DBBB8-AD79-4588-91E0-0DF847EB11FB@microsoft.com...
> >> >> >I have to search my subnet to look for servers that are listening on
> >> >> >port
> >> >> > 30715. I am using one thread of a derived class of CAsyncSocket. I 
> >> >> > am
> >> >> > using
> >> >> > CEvents with a 200 milisecond timeout, if there is no connection 
> >> >> > within
> >> >> > the
> >> >> > 200 miliseconds, then I know there is no serer. However searching 
> >> >> > from
> >> >> > 192.168.1.1 to 192.168.1.254 takes a long time. Is there a better 
> >> >> > way?
> >> >> >
> >> >>
> >> >>
> >> >>
> >>
> >>
> >> 
> 
> 
> 
0
12/12/2007 11:04:00 PM
There is a version that takes a LCCTSTR for the address

int CAsyncSocket::SendTo(
   const void* lpBuf,
   int nBufLen,
   UINT nHostPort,
   LPCTSTR lpszHostAddress = NULL,
   int nFlags = 0
);


AliR.


"amccombs" <amccombs@discussions.microsoft.com> wrote in message 
news:B7EF0FED-80F3-4538-A1F4-4598314C1C8E@microsoft.com...
> That works great, Thanks.
>
> if you wanted to send back the same thing you got to the same IP that got
> from in the
> OnReceive ReceiveFrom section, what would you do?
>
> The SendTo option with SOCKADDR, I don't know what to put for
> SOCKADDR.sa_data.
> I have the IP from the reply, so anther Broadcast message for a reply
> doesn't seem the correct thing to do.
>
> BOOL bMultipleApps = TRUE;
> SetSockOpt(SO_BROADCAST, (void*)&bMultipleApps, sizeof(BOOL), SOL_SOCKET);
>
> CAsyncSocket::SendTo(Buffer, nRead, Port, NULL, MSG_DONTROUTE);
>
>
> "AliR (VC++ MVP)" wrote:
>
>> Here is how things work with UDP.  You will have to create a UDP socket
>> (SOCK_DGRAM) instead of SOCK_STREAM.  And since it's a connection-less
>> socket you don't call Connect.   When a packet is sent using a UDP socket
>> (with SendTo) the OnReceive method gets called and you have to use
>> ReceiveFrom instead of Receive to get the data.  Now there are some
>> differences between multicast and regular udp packets, multicast packets
>> only get routed to the computers that have subscribed to the multicast
>> address, and multicast can cross subnet boundries where UDP broadcasts
>> cannot.
>>
>> Here is some sample code on how to create a sending and receiving 
>> multicast
>> socket, and how you might do your OnReceive method
>>
>> BOOL LSSocket::CreateReceivingMulticastSocket(LPCTSTR strGroupIP, UINT
>> nGroupPort,DWORD &Error)
>> {
>>    /* Create socket for receiving packets from multicast group */
>>    if(!CAsyncSocket::Create(nGroupPort, SOCK_DGRAM,FD_READ))
>>    {
>>       Error = GetLastError();
>>       return FALSE;
>>    }
>>
>>    BOOL bMultipleApps = TRUE;  /* allow reuse of local port if needed */
>>    SetSockOpt(SO_REUSEADDR, (void*)&bMultipleApps, sizeof(BOOL),
>> SOL_SOCKET);
>>
>>    /* Join the multicast group */
>>    m_mrMReq.imr_multiaddr.s_addr = inet_addr(strGroupIP); /* group addr 
>> */
>>    m_mrMReq.imr_interface.s_addr = htons(INADDR_ANY);  /* use default */
>>    if(setsockopt(m_hSocket, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char FAR
>> *)&m_mrMReq, sizeof(m_mrMReq)) < 0)
>>    {
>>       Error = GetLastError();
>>       return FALSE;
>>    }
>>
>>    m_UDP = TRUE;
>>    return TRUE;
>> }
>>
>> BOOL LSSocket::CreateSendingSocket(UINT nTTL, BOOL bLoopBack,DWORD 
>> &Error)
>> {
>>    if(!Create(0, SOCK_DGRAM))  // Create an unconnected UDP socket
>>    {
>>       Error = GetLastError();
>>       return FALSE;
>>    }
>>
>>    SetTTL(nTTL,Error);   // Set Time to Live as specified by user
>>
>>    SetLoopBack(bLoopBack);    // Enable/Disable Loopback
>>    m_UDP = TRUE;
>>    return TRUE;
>> }
>>
>> void LSSocket::OnReceive(int nErrorCode)
>> {
>>    char *Buffer = new char[30720];   //30K Buffer
>>
>>    memset(Buffer,0,30720);
>>
>>    int nRead = 0;
>>    CString Address;
>>    UINT Port = 0;
>>    if (m_UDP)
>>    {
>>       nRead = CAsyncSocket::ReceiveFrom(Buffer,30720,Address,Port);
>>    }
>>    else
>>    {
>>       nRead = CAsyncSocket::Receive(Buffer,30720);
>>    }
>>
>>    if (nRead == 30720)
>>    {
>>       TRACE("Socket Receive Overflow\n");
>>    }
>>
>>    if (nRead != SOCKET_ERROR)
>>    {
>>       if (nRead)
>>       {
>>          ProcessMessage(Buffer,nRead,Address,Port);
>>       }
>>    }
>>    else
>>    {
>>       CString Text;
>>       Text.Format("Socket Receive Error number %d\n", GetLastError());
>>       TRACE(Text);
>>    }
>>
>>    delete [] Buffer;
>>    CAsyncSocket::OnReceive(nErrorCode);
>> }
>>
>>
>> AliR.
>>
>>
>>
>> "amccombs" <amccombs@discussions.microsoft.com> wrote in message
>> news:2882BF79-94A3-4745-A2F8-E4108A0E5C70@microsoft.com...
>> > If I send out a broadcast packet on UDP, will my class that is derived
>> > from
>> > CAsyncSocket::OnReceive get called when data comes back?
>> >
>> > (Other references seem to think that I have sit and wait for a reply at
>> > the
>> > next line after sending)
>> >
>> >
>> > "AliR (VC++ MVP)" wrote:
>> >
>> >> No only UDP.  But the Multicast is only there for the server finding 
>> >> and
>> >> then you would want to switch to a normal TCP socket once you find the
>> >> server.
>> >>
>> >> AliR.
>> >>
>> >>
>> >> "amccombs" <amccombs@discussions.microsoft.com> wrote in message
>> >> news:902C317D-01C5-4FE1-97D9-B93F04749975@microsoft.com...
>> >> > Can you multicast on TCP?
>> >> >
>> >> >
>> >> > "AliR (VC++ MVP)" wrote:
>> >> >
>> >> >> I can't think of another way.
>> >> >>
>> >> >> But if the server program is your program, then I have a 
>> >> >> suggestion.
>> >> >> I have a server that alot of clients connect to, and I really 
>> >> >> didn't
>> >> >> want
>> >> >> to
>> >> >> have to input the server address in every client.  So what I did 
>> >> >> was I
>> >> >> used
>> >> >> a multicast socket to find the server.  The server listens to 
>> >> >> traffic
>> >> >> on
>> >> >> a
>> >> >> particular multicast address, and when the clients want to find the
>> >> >> server
>> >> >> application, they would send a message (where are you) using a
>> >> >> multicast
>> >> >> socket with that particular address, the server would receive the
>> >> >> message
>> >> >> and send a reply (Here I am) with its IP address. Now if there are
>> >> >> more
>> >> >> than
>> >> >> one server, the client would get multiple "Here I am" replies. This
>> >> >> entire
>> >> >> process takes less than a second.
>> >> >>
>> >> >> AliR.
>> >> >>
>> >> >>
>> >> >> "amccombs" <amccombs@discussions.microsoft.com> wrote in message
>> >> >> news:129DBBB8-AD79-4588-91E0-0DF847EB11FB@microsoft.com...
>> >> >> >I have to search my subnet to look for servers that are listening 
>> >> >> >on
>> >> >> >port
>> >> >> > 30715. I am using one thread of a derived class of CAsyncSocket. 
>> >> >> > I
>> >> >> > am
>> >> >> > using
>> >> >> > CEvents with a 200 milisecond timeout, if there is no connection
>> >> >> > within
>> >> >> > the
>> >> >> > 200 miliseconds, then I know there is no serer. However searching
>> >> >> > from
>> >> >> > 192.168.1.1 to 192.168.1.254 takes a long time. Is there a better
>> >> >> > way?
>> >> >> >
>> >> >>
>> >> >>
>> >> >>
>> >>
>> >>
>> >>
>>
>>
>> 


0
AliR3470 (3235)
12/13/2007 4:14:41 PM
CEvents have little to do with CAsyncSocket, so I'm not sure what is going on.  

You are searching 253 connections at 200ms each, so that will take approximately a minute
under absolutely optimal conditions.

You could "speed it up" by simultaneously starting up, say, 20 connection attempts; they
will all fail within 200ms, so you could search 253 connections in about 3 seconds with a
200ms timeout.  Or try to connect to 50 servers simultaneously, and it should take about 1
second.
					joe

On Tue, 11 Dec 2007 13:20:01 -0800, amccombs <amccombs@discussions.microsoft.com> wrote:

>I have to search my subnet to look for servers that are listening on port 
>30715. I am using one thread of a derived class of CAsyncSocket. I am using 
>CEvents with a 200 milisecond timeout, if there is no connection within the 
>200 miliseconds, then I know there is no serer. However searching from 
>192.168.1.1 to 192.168.1.254 takes a long time. Is there a better way?
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
0
newcomer (15972)
12/16/2007 5:05:21 AM
But you can still use asynchronous sockets for UDP.

Obviously, this only works when you have a possible multicast router, or no router at all;
even within a single local domain like 192.168.1.n, if there are multiple segments with
routers, the routers typically have to be configured to forward multicast packets on the
multicast address (we hit this problem with some inter-building routing)
					joe

On Wed, 12 Dec 2007 17:37:24 GMT, "AliR \(VC++ MVP\)" <AliR@online.nospam> wrote:

>Here is how things work with UDP.  You will have to create a UDP socket 
>(SOCK_DGRAM) instead of SOCK_STREAM.  And since it's a connection-less 
>socket you don't call Connect.   When a packet is sent using a UDP socket 
>(with SendTo) the OnReceive method gets called and you have to use 
>ReceiveFrom instead of Receive to get the data.  Now there are some 
>differences between multicast and regular udp packets, multicast packets 
>only get routed to the computers that have subscribed to the multicast 
>address, and multicast can cross subnet boundries where UDP broadcasts 
>cannot.
>
>Here is some sample code on how to create a sending and receiving multicast 
>socket, and how you might do your OnReceive method
>
>BOOL LSSocket::CreateReceivingMulticastSocket(LPCTSTR strGroupIP, UINT 
>nGroupPort,DWORD &Error)
>{
>   /* Create socket for receiving packets from multicast group */
>   if(!CAsyncSocket::Create(nGroupPort, SOCK_DGRAM,FD_READ))
>   {
>      Error = GetLastError();
>      return FALSE;
>   }
>
>   BOOL bMultipleApps = TRUE;  /* allow reuse of local port if needed */
>   SetSockOpt(SO_REUSEADDR, (void*)&bMultipleApps, sizeof(BOOL), 
>SOL_SOCKET);
>
>   /* Join the multicast group */
>   m_mrMReq.imr_multiaddr.s_addr = inet_addr(strGroupIP); /* group addr */
>   m_mrMReq.imr_interface.s_addr = htons(INADDR_ANY);  /* use default */
>   if(setsockopt(m_hSocket, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char FAR 
>*)&m_mrMReq, sizeof(m_mrMReq)) < 0)
>   {
>      Error = GetLastError();
>      return FALSE;
>   }
>
>   m_UDP = TRUE;
>   return TRUE;
>}
>
>BOOL LSSocket::CreateSendingSocket(UINT nTTL, BOOL bLoopBack,DWORD &Error)
>{
>   if(!Create(0, SOCK_DGRAM))  // Create an unconnected UDP socket
>   {
>      Error = GetLastError();
>      return FALSE;
>   }
>
>   SetTTL(nTTL,Error);   // Set Time to Live as specified by user
>
>   SetLoopBack(bLoopBack);    // Enable/Disable Loopback
>   m_UDP = TRUE;
>   return TRUE;
>}
>
>void LSSocket::OnReceive(int nErrorCode)
>{
>   char *Buffer = new char[30720];   //30K Buffer
>
>   memset(Buffer,0,30720);
>
>   int nRead = 0;
>   CString Address;
>   UINT Port = 0;
>   if (m_UDP)
>   {
>      nRead = CAsyncSocket::ReceiveFrom(Buffer,30720,Address,Port);
>   }
>   else
>   {
>      nRead = CAsyncSocket::Receive(Buffer,30720);
>   }
>
>   if (nRead == 30720)
>   {
>      TRACE("Socket Receive Overflow\n");
>   }
>
>   if (nRead != SOCKET_ERROR)
>   {
>      if (nRead)
>      {
>         ProcessMessage(Buffer,nRead,Address,Port);
>      }
>   }
>   else
>   {
>      CString Text;
>      Text.Format("Socket Receive Error number %d\n", GetLastError());
>      TRACE(Text);
>   }
>
>   delete [] Buffer;
>   CAsyncSocket::OnReceive(nErrorCode);
>}
>
>
>AliR.
>
>
>
>"amccombs" <amccombs@discussions.microsoft.com> wrote in message 
>news:2882BF79-94A3-4745-A2F8-E4108A0E5C70@microsoft.com...
>> If I send out a broadcast packet on UDP, will my class that is derived 
>> from
>> CAsyncSocket::OnReceive get called when data comes back?
>>
>> (Other references seem to think that I have sit and wait for a reply at 
>> the
>> next line after sending)
>>
>>
>> "AliR (VC++ MVP)" wrote:
>>
>>> No only UDP.  But the Multicast is only there for the server finding and
>>> then you would want to switch to a normal TCP socket once you find the
>>> server.
>>>
>>> AliR.
>>>
>>>
>>> "amccombs" <amccombs@discussions.microsoft.com> wrote in message
>>> news:902C317D-01C5-4FE1-97D9-B93F04749975@microsoft.com...
>>> > Can you multicast on TCP?
>>> >
>>> >
>>> > "AliR (VC++ MVP)" wrote:
>>> >
>>> >> I can't think of another way.
>>> >>
>>> >> But if the server program is your program, then I have a suggestion.
>>> >> I have a server that alot of clients connect to, and I really didn't 
>>> >> want
>>> >> to
>>> >> have to input the server address in every client.  So what I did was I
>>> >> used
>>> >> a multicast socket to find the server.  The server listens to traffic 
>>> >> on
>>> >> a
>>> >> particular multicast address, and when the clients want to find the
>>> >> server
>>> >> application, they would send a message (where are you) using a 
>>> >> multicast
>>> >> socket with that particular address, the server would receive the 
>>> >> message
>>> >> and send a reply (Here I am) with its IP address. Now if there are 
>>> >> more
>>> >> than
>>> >> one server, the client would get multiple "Here I am" replies. This
>>> >> entire
>>> >> process takes less than a second.
>>> >>
>>> >> AliR.
>>> >>
>>> >>
>>> >> "amccombs" <amccombs@discussions.microsoft.com> wrote in message
>>> >> news:129DBBB8-AD79-4588-91E0-0DF847EB11FB@microsoft.com...
>>> >> >I have to search my subnet to look for servers that are listening on
>>> >> >port
>>> >> > 30715. I am using one thread of a derived class of CAsyncSocket. I 
>>> >> > am
>>> >> > using
>>> >> > CEvents with a 200 milisecond timeout, if there is no connection 
>>> >> > within
>>> >> > the
>>> >> > 200 miliseconds, then I know there is no serer. However searching 
>>> >> > from
>>> >> > 192.168.1.1 to 192.168.1.254 takes a long time. Is there a better 
>>> >> > way?
>>> >> >
>>> >>
>>> >>
>>> >>
>>>
>>>
>>> 
>
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
0
newcomer (15972)
12/16/2007 5:08:31 AM
Reply:

Similar Artilces:

Howto create textbox like in powerpoint
Hi Could someone tell me how to create powerpoint type textboxes? Are they windowless rich edit controls? Thanks Sachin They are rich edit control, but they have a window, they are owned by the window that they are being displayed on. AliR. "sachinkundu" <sachinkundu@gmail.com> wrote in message news:1170062520.486641.43880@m58g2000cwm.googlegroups.com... > Hi > > Could someone tell me how to create powerpoint type textboxes? Are > they windowless rich edit controls? > > Thanks > Sachin > > They are rich edit control, but they have a wind...

from Access to SQL server
I wonder who could help me with this. Is it possible to migrate an already programmed database in MS Access to SQL Server or this needs complete reprogramming? Thank you marcp Hi Marc, This depends largely on (a) your database and (b) whether you want to benefit from the increased performance that SQL server can deliver. With a simple database and no need to optimise performance, you may need to do little more than run the Upsizing Wizard. On the other hand, if you are moving to SQL Server in order to provide better support for large numbers of users, major revision is likely to be ne...

Adding an additional SMTP domain in Exchange 2003 server
Hi there, I recently setup an Exchange server 2003 in our domain. During the setup I added a second SMTP domain and its respective address on the same server. Now our company is rebranding and I have to change the email address from joe@abc.ca to joe@xyz.com. I would appreciate any help in accomplishing this on the Exchange server. Thank you My recommendation would be to add a new domain to your recipient policy for xyz.com. This will update all recipients with the new domain. You can also set it to be the primary address. It's usually a bad idea to remove old domains until yo...

Smtp Authentication
Hi all I'm an IBM Domino administrator.But i have to fix an exchange 2003 server in these days.The problem is; the exchange server was full of spam yesterday because of open relay, i deleted all the spam.Now i want to make an smtp authentication setting on the exchange server. the schema is as that: 1 MS 2003 server (domain controller) with exchange 2003 installed (all fixpacks are included) (192.168.0.1).15 clients (winXP+Outlook 2000/XP/2003 mixed). network adress is 192.168.0.0/24 all the computers access to internet by a dsl router (192.168.0.254), using 192.168.0.1 as DNS server...

"File in use" is incorrect
I have an excel file that user1 will open. Whenever he opens the file and another person tries to open the same file, the "File in use" message pops up with the read only option. The problem is that the notification says that User2 has the file open even though User1 is the actual user. User2 is not listed as the owner of the file, is not the creator of the file, nor does he have any specific permissions to access the file other than being in Group1. Would anyone know why the file notification would be incorrect regarding the user who really has the file open? Hi maybe the...

Outlook Web Access will not work on Exchange 2003 Server
Have a problem that has been driving me nuts for days and days and days. I have two exchange servers for two different clients I have configured them identically with the exception of two things. Both are win 2003 servers with the latest patches applied. Both have Exchange 2003 with SP1 installed. I have compared services with both servers and they have the same services started. Problem I am having is basic Outlook web access from a client running IE is working in one environment and not at all in the other. I have gone through tons of documentation, forums, looked at every detai...

Multiple installations of CRM 1.2 on virtual servers
I'd like to find out whether Microsoft considers installation of CRM on virtual servers a supported configuration. Does anybody know the answer to this question? The reason we are considering this type of environment is because we have 5 distinct sets of system requirements with different end-users. So, I assume it would be easier to have 5 installations of CRM instead of 1 installation. The type of customizations required for each installation will be very different and I'm not sure if CRM was designed to support this type of customization. Any feedback is appreciated. Regards, Re...

Multiple Portfolios in Money 2004
College saving and retirement saving usually have different time horizons and, therefore, require different investment strategies with different asset allocations. While Money 2004 allows one to view a portfolio's asset allocation, it seems to only allow one portfolio. Can anyone verify that only one portfolio is allowed in Money 2004? ...

How do I do multiple charts on the same page? Is there a merge fu #2
I have several charts to on students and want them to be on the same page. How do I do this? Also, is there a merge application within Excel that would enable me to merge names of students into the graphs without doing each one separately? ...

Event ID: 5891 is logged in the Exchange Server Application log by CRM 1.2
I am constantly getting this errors in Exchange 2003 App Log - 891 - CrmExchangeQueueService - An error occurred : <description>An unexpected error occurred. </description><details>The MIME header ended unexpectedly (in the value section) </details><file>d:\crm\build\3297\src\platform\include\util\crmmimeutil.inl</file><line>536</line> Could anyone advice what to do to fix it or post the content of this page: https://mbs.microsoft.com/knowledgebase/KBDisplay.aspx?scid=kb;en-us;894810 since I can't login because of some strange problems wi...

table of contents with multiple entries
I have a table of contents that I've made using code found on this site. However, several times in my report, a person's name is repeated and I'd like to include both pages in the table of contents. John Doe.................1, 23 Any suggestions would be appreciated. -- Message posted via AccessMonster.com http://www.accessmonster.com/Uwe/Forums.aspx/access-reports/200704/1 Presumably you have set up a table where you record the customer's name, and the page number. And you use the Print event of the (Detail?) section of your report to populate this table. And then you ...

Find replace problem
Hi I have some numbers denoted thus:- 02:34:56 (they can be any numbers at all separated by Colons and I want to replace the last Colon with a dot/period so it is 02:34.56 I tried to replace using **:**:** to **:**.** but it fills in with the latter How can I achieve please Neil Here is a way to achieve this effect without using Find/Replace: 1. Highlight your column, then Forman-> Cells-> Custom 2. the format your cells should be in is hh:mm:ss. In the Type: text box, change this format to hh:mm.ss Now, your column should be fixed. -- Anne Murray "neil40@btinternet.c...

Outlook xp
I can't figure out how to display a record of use of Microsoft and other computer applications in Outlook. ...

Problems With SQL Server
i'm having a problem with my sql server. i keep getting a messages that my server no longer exists .Native error 17 and SQL State 08001 Does anyone know what in the world is this. Please email me cifbartlett@aol.com or kishburks@hotmail.com. Jessie wrote: > i'm having a problem with my sql server. i keep getting a messages that my > server no longer exists .Native error 17 and SQL State 08001 Does anyone know > what in the world is this. Please email me cifbartlett@aol.com or > kishburks@hotmail.com. Connect to your server with IP, and verify in sql tool configurat...

I'M GOING INSANE trying to find out...
I've spent the last 18 hours trying to find the answer to this question: Microsoft Works Spreadsheet Version 6.0 - What's the key combination to enter the date or time in a cell? I'm sorry to be a nuisance, but even the Microsoft website is no help. It's driving me NUTS!!! ------------------------------------------------ ~~ Message posted from http://www.ExcelTip.com/ ~~ View and post usenet messages directly from http://www.ExcelForum.com/ Lady The current versions use Ctrl ; for date and Ctrl : for time. Try these!! Andy "Lady18wheeler" <Lady18wheele...

Leaving the mail on the server using outlook
How dow we leave the mail on the server after its checked using outlook. I know the setting in outlook express but cant seem to find it in outlook2003. Thanx It's in the advanced properties of your Internet Mail account....tools, email accounts, view/modify....etc. Sandeep wrote: > How dow we leave the mail on the server after its checked > using outlook. I know the setting in outlook express but > cant seem to find it in outlook2003. Thanx ...

Problem using MsgHook to send msg to running app
Hi, I am having a problem that I do not understand at all. :D I am trying to send a message to a running app called FileClean. This is a new version and the message hook is not able to obtain a hwnd for the running app. It does work with the older version, but I have not been able to find any difference's in the properties of either version and the app name is listed the same when running, in the task manager. Where else should I be looking for something different. Here is the code, this is from Karl's example, the FindWindow(FormClass, App.Title) returns 0 with th...

Find and delete rows
Hi and TIA. What I'm trying to accomplish is to run code that checks column A starting at A1. Run down until it finds the word "DeleteMe" and from that row until the end of the document delete all rows. Is that possible? Thanks for any advice and your time. -- Reggie Give this macro a try... Sub ProcessDeleteMe() Dim DM As Range With Worksheets("Sheet1") On Error GoTo Done Set DM = .Columns("A").Find(What:="DeleteMe", MatchCase:=False) Application.Intersect(.UsedRange, Range(DM, .Cells( _ ...

How can I create a fish-bone chart using excel?
I am having difficulty setting up a fishbone type chart. I can use text orientation on the top to get the angle I want but it doesn't work on the line below - the orintatinon is attached to the bottom of the cell. Can it be attached to the top of the cell? Hi, Can you not set the vertical alignment of the cell to top? Cheers Andy lfaris wrote: > I am having difficulty setting up a fishbone type chart. I can use text > orientation on the top to get the angle I want but it doesn't work on the > line below - the orintatinon is attached to the bottom of the cell. Can ...

Find row/column of value in Unsorted Array
I am trying to do the following. I have an unsorted array with values 12 columns wide, and 15 rows deep (Fantasy Football scores, 12 owners, 15 weeks) I can find the actual value using the large function on the UNSORTED array. But now I want to find the owner, and week, the largest value occurred. A visual can be seen at http://movieraiders.com/ffb/Categories.htm Under the records tab, weekly section I am having to manually type the week and Manager because I can't figure out how to pull the data from the corresponding worksheet. Thoughts? ------------------------------------------...

How to find a blank attribute through PFDAVAdmin
I would like to return all Public folders that have a blank attribute for PR_CONTAINER_Class e.g. (&(0x3613001E=IPF.Appointment)) would return appointments How do I return 0x3613001E=<NOTHING> I get Exception: Invalid Filter String if I leave it blank Thanks in advance for your help. ...

Multiple workbooks
I'm building a budget workbook system as follows: The first workbook consist of worksheets for each cost center that link to a summary page. The second workbook takes the amounts in the summary page from the two different workbooks (one being the 1st workbook of cost center and another with payroll information) to a master object code workbook by department heads and by two different catagories. The third workbook takes the information from the second workbook into a master worksheet that will show by two divisions and 5 or 6 catagories. I'm not sure how to do the links or is th...

Using Publisher with email
I am trying to create a 3 page newsletter and i want to imbed it into the email instead of having it as an attachment. I can't figure out how to format where all pages show up instead of just the 1st page once I send it to email preview. Kellie Have you considered a PDF? There are free converters around. www.primopdf.com is mentioned a lot here. If you have 2007 Microsoft has an add-in. https://www.microsoft.com/downloads/details.aspx?FamilyId=4D951911-3E7E-4AE6-B059-A2E79ED87041&displaylang=en -- Mary Sauer MSFT MVP http://office.microsoft.com/ http://msauer.mvps.org/ news...

HOWTO Force landscape printing with CPrintDialog.
HOWTO Force landscape printing with CPrintDialog. Please help, I have the following code I want to FORCE to print in landscape after the CPrintDialog finishes. This solution must not touch the CWinApp's "m_hDevMode" data member. This solution must only do something to the DC returned from the CPrintDialog, and it must override any possible orientation settings the user may have change while they were inside CPrintDialog. : CPrintDialog CPrintDialog_Temp(FALSE); if (CPrintDialog_Temp.DoModal() == IDOK) { // Do something here to FORCE the print out to be landscape ...

Error 0x80040900 server name not found
If I start up Outlook 2003, Outlook starts to dial up (if I am not connected to the internet already) and immediately tries to send/receive mail. Sending is succesfull (perhaps because there is nothing to send), but receiving always fails with the 0x80040900 error message (server name not found etc.) It appears as if outlook tries to get the mail before the connection is completed (Sometimes the connection dialog box is still open as the error message appears) If I am already connected to the internet before starting Outlook, everything functions OK. Any ideas? Note: I do use ADSL (LAN conne...