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. c++ upd brodcast client

c++ upd brodcast client

Scheduled Pinned Locked Moved C / C++ / MFC
c++sysadminhardwarequestion
9 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.
  • A Offline
    A Offline
    arishri
    wrote on last edited by
    #1

    hi I'm programming a c++ client to receive packets from a server (UDP broadcast). This is a hardware switch (One direction only) system so I have no information regarding the senders address or any other info. It all compiles and binds OK but no packets received.

    WSAStartup(0x0101, &_wsaData);
    memset((char *)&_sockaddr, 0, sizeof(_sockaddr));
    _sockaddr.sin_addr.s_addr = _nAdapterIp;
    _sockaddr.sin_family = _nadressFam;
    _sockaddr.sin_port = _usPort; //Gave it a try with (0) - also not working
    _socket = socket(_nadressFam, SOCK_DGRAM, 0); //Gave it a try with (17) - UDP for the 3 rd parameter - not working
    char broadcast='1';
    setsockopt(_socket, SOL_SOCKET, SO_BROADCAST, &broadcast ,sizeof(broadcast));
    bind(_socket, (sockaddr*) & _sockaddr, sizeof (_sockaddr));
    char* recvbuf = new char[_nPacketSize + 1];
    int nSize = recv(_socket, recvbuf, _nPacketSize + 1, 0);

    Any ideas? Thanks,

    J 1 Reply Last reply
    0
    • A arishri

      hi I'm programming a c++ client to receive packets from a server (UDP broadcast). This is a hardware switch (One direction only) system so I have no information regarding the senders address or any other info. It all compiles and binds OK but no packets received.

      WSAStartup(0x0101, &_wsaData);
      memset((char *)&_sockaddr, 0, sizeof(_sockaddr));
      _sockaddr.sin_addr.s_addr = _nAdapterIp;
      _sockaddr.sin_family = _nadressFam;
      _sockaddr.sin_port = _usPort; //Gave it a try with (0) - also not working
      _socket = socket(_nadressFam, SOCK_DGRAM, 0); //Gave it a try with (17) - UDP for the 3 rd parameter - not working
      char broadcast='1';
      setsockopt(_socket, SOL_SOCKET, SO_BROADCAST, &broadcast ,sizeof(broadcast));
      bind(_socket, (sockaddr*) & _sockaddr, sizeof (_sockaddr));
      char* recvbuf = new char[_nPacketSize + 1];
      int nSize = recv(_socket, recvbuf, _nPacketSize + 1, 0);

      Any ideas? Thanks,

      J Offline
      J Offline
      jeron1
      wrote on last edited by
      #2

      One important thing to do is look at the return codes for the methods that you are calling and your socket constructor, most times they will give you a clue about what is wrong. Also, what are values _nAdapterIp, _nadressFam, and _usPort? Could you be suffering from endian type issues?

      A M 2 Replies Last reply
      0
      • J jeron1

        One important thing to do is look at the return codes for the methods that you are calling and your socket constructor, most times they will give you a clue about what is wrong. Also, what are values _nAdapterIp, _nadressFam, and _usPort? Could you be suffering from endian type issues?

        A Offline
        A Offline
        arishri
        wrote on last edited by
        #3

        sorry abut that, I shorten it so the code will be clear.... The actual code has the tests:

        if (WSAStartup(0x0101, &_wsaData) != 0)
        {
        fprintf(stderr, "Could not open Windows connection.\n");
        exit(0);
        }

        char broadcast= (char)1;

        if (setsockopt(\_socket, SOL\_SOCKET, SO\_BROADCAST, &broadcast ,sizeof(broadcast)) != 0)
        {
            fprintf(stderr, "Could not set socket options.\\n");
            WSACleanup();
            exit(0);
        }
        

        etc....

        J 1 Reply Last reply
        0
        • A arishri

          sorry abut that, I shorten it so the code will be clear.... The actual code has the tests:

          if (WSAStartup(0x0101, &_wsaData) != 0)
          {
          fprintf(stderr, "Could not open Windows connection.\n");
          exit(0);
          }

          char broadcast= (char)1;

          if (setsockopt(\_socket, SOL\_SOCKET, SO\_BROADCAST, &broadcast ,sizeof(broadcast)) != 0)
          {
              fprintf(stderr, "Could not set socket options.\\n");
              WSACleanup();
              exit(0);
          }
          

          etc....

          J Offline
          J Offline
          jeron1
          wrote on last edited by
          #4

          arishri wrote:

          _socket = socket(_nadressFam, SOCK_DGRAM, 0); //Gave it a try with (17) - UDP for the 3 rd parameter - not working

          How could you tell it wasn't working? I assume IPPROTO_UDP equates to '17'. When your application is running, and you go to DOS prompt and do a "netstat -a" do you see your socket with the proper port listed?

          A 2 Replies Last reply
          0
          • J jeron1

            arishri wrote:

            _socket = socket(_nadressFam, SOCK_DGRAM, 0); //Gave it a try with (17) - UDP for the 3 rd parameter - not working

            How could you tell it wasn't working? I assume IPPROTO_UDP equates to '17'. When your application is running, and you go to DOS prompt and do a "netstat -a" do you see your socket with the proper port listed?

            A Offline
            A Offline
            arishri
            wrote on last edited by
            #5

            _socket = socket(_nadressFam, SOCK_DGRAM, 17);
            _socket1 = socket(_nadressFam, SOCK_DGRAM, 17);

            if ((\_socket == INVALID\_SOCKET) || (\_socket1 == INVALID\_SOCKET))
            {
                fprintf(stderr, "Could not create socket.\\n");
                WSACleanup();
                exit(0);
            }
            
            J 1 Reply Last reply
            0
            • J jeron1

              arishri wrote:

              _socket = socket(_nadressFam, SOCK_DGRAM, 0); //Gave it a try with (17) - UDP for the 3 rd parameter - not working

              How could you tell it wasn't working? I assume IPPROTO_UDP equates to '17'. When your application is running, and you go to DOS prompt and do a "netstat -a" do you see your socket with the proper port listed?

              A Offline
              A Offline
              arishri
              wrote on last edited by
              #6

              int nSize = recv(_socket, recvbuf,_nPacketSize + 1, 0);

              Get no packages here....

              J 1 Reply Last reply
              0
              • A arishri

                _socket = socket(_nadressFam, SOCK_DGRAM, 17);
                _socket1 = socket(_nadressFam, SOCK_DGRAM, 17);

                if ((\_socket == INVALID\_SOCKET) || (\_socket1 == INVALID\_SOCKET))
                {
                    fprintf(stderr, "Could not create socket.\\n");
                    WSACleanup();
                    exit(0);
                }
                
                J Offline
                J Offline
                jeron1
                wrote on last edited by
                #7

                Stick with one socket for now. If it still fails, call WSAGetLastError() and see what the error is.

                1 Reply Last reply
                0
                • A arishri

                  int nSize = recv(_socket, recvbuf,_nPacketSize + 1, 0);

                  Get no packages here....

                  J Offline
                  J Offline
                  jeron1
                  wrote on last edited by
                  #8

                  I couldn't be firewall related, could it?

                  1 Reply Last reply
                  0
                  • J jeron1

                    One important thing to do is look at the return codes for the methods that you are calling and your socket constructor, most times they will give you a clue about what is wrong. Also, what are values _nAdapterIp, _nadressFam, and _usPort? Could you be suffering from endian type issues?

                    M Offline
                    M Offline
                    Member 10442175
                    wrote on last edited by
                    #9

                    #include
                    main()
                    {
                    char m;
                    int p;
                    float n;
                    scanf ("%s%d%f",&m,&p,&n);
                    }

                    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