Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Connect to all LAN servers

Connect to all LAN servers

Scheduled Pinned Locked Moved C / C++ / MFC
sysadmin
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Manmohan29
    wrote on last edited by
    #1

    i want to connect my application to all servers on LAN using TCP sockets.

    UINT CD1MessageDlg::ScanLAN(LPVOID pParam)
    {
    unsigned int tf0, tf1, tf2, tf3;
    CString tip = "";

    CD1MessageDlg \*t;
    t=(CD1MessageDlg\*)pParam;
    BOOL x = 1;
    // Loop for scanning LAN
    for (tf0 = 192; tf0 <= 192; tf0++)
    {
    	for (tf1 = 168; tf1 <= 168; tf1++)
    	{
    		for (tf2 = 109; tf2 <= 117; tf2++)
    		{
    			for (tf3 = 1; tf3 <= 255; tf3++)
    			{
    				tip.Format("%u.%u.%u.%u", tf0, tf1, tf2, tf3);
    				t->m\_sConnectSocket.Connect(tip, 50000);
    

    //.
    //.
    //.
    // I will detach the socket and pass to seperate thread after it finds a server on LAN
    //this line is not working even if i set my ip to 192.168.109.2
    //t->m_sConnectSocket.Connect(tip, 50000);
    }
    // reset tf3 to default value
    tf3 = 0;
    }
    // reset tf2 to default value
    tf2 = 109;
    }
    // reset tf1 to default value
    tf1 = 168;
    }
    return 0;
    }

    one more ques. can i reuse the same socket again and again for finding server as i'm doing in my this code.

    Future Lies in Present. Manmohan Bishnoi

    G E 2 Replies Last reply
    0
    • M Manmohan29

      i want to connect my application to all servers on LAN using TCP sockets.

      UINT CD1MessageDlg::ScanLAN(LPVOID pParam)
      {
      unsigned int tf0, tf1, tf2, tf3;
      CString tip = "";

      CD1MessageDlg \*t;
      t=(CD1MessageDlg\*)pParam;
      BOOL x = 1;
      // Loop for scanning LAN
      for (tf0 = 192; tf0 <= 192; tf0++)
      {
      	for (tf1 = 168; tf1 <= 168; tf1++)
      	{
      		for (tf2 = 109; tf2 <= 117; tf2++)
      		{
      			for (tf3 = 1; tf3 <= 255; tf3++)
      			{
      				tip.Format("%u.%u.%u.%u", tf0, tf1, tf2, tf3);
      				t->m\_sConnectSocket.Connect(tip, 50000);
      

      //.
      //.
      //.
      // I will detach the socket and pass to seperate thread after it finds a server on LAN
      //this line is not working even if i set my ip to 192.168.109.2
      //t->m_sConnectSocket.Connect(tip, 50000);
      }
      // reset tf3 to default value
      tf3 = 0;
      }
      // reset tf2 to default value
      tf2 = 109;
      }
      // reset tf1 to default value
      tf1 = 168;
      }
      return 0;
      }

      one more ques. can i reuse the same socket again and again for finding server as i'm doing in my this code.

      Future Lies in Present. Manmohan Bishnoi

      G Offline
      G Offline
      Garth J Lancaster
      wrote on last edited by
      #2

      you probably can - but if you fail to connect your socket will 'block' for the timeout, so you may want to use a socket pool furthermore,

      Manmohan29 wrote:

      for (tf0 = 192; tf0 <= 192; tf0++) { for (tf1 = 168; tf1 <= 168; tf1++) {

      loops are redundant, and in

      Manmohan29 wrote:

      tip.Format("%u.%u.%u.%u", tf0, tf1, tf2, tf3);

      you're not specifying a port - so what do you hope to connect to ?

      M 1 Reply Last reply
      0
      • G Garth J Lancaster

        you probably can - but if you fail to connect your socket will 'block' for the timeout, so you may want to use a socket pool furthermore,

        Manmohan29 wrote:

        for (tf0 = 192; tf0 <= 192; tf0++) { for (tf1 = 168; tf1 <= 168; tf1++) {

        loops are redundant, and in

        Manmohan29 wrote:

        tip.Format("%u.%u.%u.%u", tf0, tf1, tf2, tf3);

        you're not specifying a port - so what do you hope to connect to ?

        M Offline
        M Offline
        Manmohan29
        wrote on last edited by
        #3

        Garth J Lancaster wrote:

        for (tf0 = 192; tf0 <= 192; tf0++) { for (tf1 = 168; tf1 <= 168; tf1++) { loops are redundant

        this code is for only testing purpose. i will modify my function to take ip address range from user.

        Garth J Lancaster wrote:

        tip.Format("%u.%u.%u.%u", tf0, tf1, tf2, tf3); you're not specifying a port - so what do you hope to connect to ?

        i'm specifying port 50000

        t->m_sConnectSocket.Connect(tip, 50000);

        Future Lies in Present. Manmohan Bishnoi

        G 1 Reply Last reply
        0
        • M Manmohan29

          Garth J Lancaster wrote:

          for (tf0 = 192; tf0 <= 192; tf0++) { for (tf1 = 168; tf1 <= 168; tf1++) { loops are redundant

          this code is for only testing purpose. i will modify my function to take ip address range from user.

          Garth J Lancaster wrote:

          tip.Format("%u.%u.%u.%u", tf0, tf1, tf2, tf3); you're not specifying a port - so what do you hope to connect to ?

          i'm specifying port 50000

          t->m_sConnectSocket.Connect(tip, 50000);

          Future Lies in Present. Manmohan Bishnoi

          G Offline
          G Offline
          Garth J Lancaster
          wrote on last edited by
          #4

          Manmohan29 wrote:

          i'm specifying port 50000

          my mistake - I had forgotten the Connect() parameters - I was thinking you were specifying the timeout .. in which case, as I said, if you get a connect fail, you'll have to wait for the timeout for the socket to become available again I think ... 'g'

          1 Reply Last reply
          0
          • M Manmohan29

            i want to connect my application to all servers on LAN using TCP sockets.

            UINT CD1MessageDlg::ScanLAN(LPVOID pParam)
            {
            unsigned int tf0, tf1, tf2, tf3;
            CString tip = "";

            CD1MessageDlg \*t;
            t=(CD1MessageDlg\*)pParam;
            BOOL x = 1;
            // Loop for scanning LAN
            for (tf0 = 192; tf0 <= 192; tf0++)
            {
            	for (tf1 = 168; tf1 <= 168; tf1++)
            	{
            		for (tf2 = 109; tf2 <= 117; tf2++)
            		{
            			for (tf3 = 1; tf3 <= 255; tf3++)
            			{
            				tip.Format("%u.%u.%u.%u", tf0, tf1, tf2, tf3);
            				t->m\_sConnectSocket.Connect(tip, 50000);
            

            //.
            //.
            //.
            // I will detach the socket and pass to seperate thread after it finds a server on LAN
            //this line is not working even if i set my ip to 192.168.109.2
            //t->m_sConnectSocket.Connect(tip, 50000);
            }
            // reset tf3 to default value
            tf3 = 0;
            }
            // reset tf2 to default value
            tf2 = 109;
            }
            // reset tf1 to default value
            tf1 = 168;
            }
            return 0;
            }

            one more ques. can i reuse the same socket again and again for finding server as i'm doing in my this code.

            Future Lies in Present. Manmohan Bishnoi

            E Offline
            E Offline
            Emilio Garavaglia
            wrote on last edited by
            #5

            Consider the possibility to use "multicast" http://en.wikipedia.org/wiki/IP_multicast[^]. Check with your network administrator if this is possible in you environment, before spreading a potentially huge number of independent identical flows, or you may risk to sit down your network.

            2 bugs found. > recompile ... 65534 bugs found. :doh:

            M 1 Reply Last reply
            0
            • E Emilio Garavaglia

              Consider the possibility to use "multicast" http://en.wikipedia.org/wiki/IP_multicast[^]. Check with your network administrator if this is possible in you environment, before spreading a potentially huge number of independent identical flows, or you may risk to sit down your network.

              2 bugs found. > recompile ... 65534 bugs found. :doh:

              M Offline
              M Offline
              Manmohan29
              wrote on last edited by
              #6

              emilio_grv wrote:

              Check with your network administrator if this is possible in you environment

              It is not possible in our college network as told by our N/W Admin. Thanx. :)

              Future Lies in Present. Manmohan Bishnoi

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups