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. I need socket help UDP broadcast, sendbuf

I need socket help UDP broadcast, sendbuf

Scheduled Pinned Locked Moved C / C++ / MFC
databasetestingbeta-testinghelp
14 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.
  • J Offline
    J Offline
    jkirkerx
    wrote on last edited by
    #1

    I copied some sample code from msdn to create a socket, and modified it for testing. I want to broadcast a udp dgram, but I cant figure out what to send. I'm using ethereal to examine what SQL Management sends, and I'm trying to copy the behavior. So it looks like my first send is accurate, but I'm not sending the correct data out for a response back from sqlbrowser. I'm convinced if I send the right stuff, I will get my responses back from all the SQL servers. If i'm in the wrong forum sorry, didn't really know where to post it. I tried 0x02 in the send buffer. iResult = getaddrinfo( "255.255.255.255", "ms-sql-m", &hints, &result ); I think this is what SQL Management sent

    0000 ff ff ff ff ff ff 00 13 72 36 67 30 08 00 45 00 ........ r6g0..E.
    0010 00 1d 6f 9a 00 00 80 11 07 8a c0 a8 03 04 ff ff ..o..... ........
    0020 ff ff 05 8b 05 9a 00 09 2f 0b 02 ........ /..

    And this is what I sent

    0000 ff ff ff ff ff ff 00 13 72 36 67 30 08 00 45 00 ........ r6g0..E.
    0010 00 22 8c 51 00 00 80 11 ea cd c0 a8 03 04 ff ff .".Q.... ........
    0020 ff ff 06 bc 05 9a 00 0e 9c 8f 30 78 32 66 30 62 ........ ..0x2f0b

    G A J 3 Replies Last reply
    0
    • J jkirkerx

      I copied some sample code from msdn to create a socket, and modified it for testing. I want to broadcast a udp dgram, but I cant figure out what to send. I'm using ethereal to examine what SQL Management sends, and I'm trying to copy the behavior. So it looks like my first send is accurate, but I'm not sending the correct data out for a response back from sqlbrowser. I'm convinced if I send the right stuff, I will get my responses back from all the SQL servers. If i'm in the wrong forum sorry, didn't really know where to post it. I tried 0x02 in the send buffer. iResult = getaddrinfo( "255.255.255.255", "ms-sql-m", &hints, &result ); I think this is what SQL Management sent

      0000 ff ff ff ff ff ff 00 13 72 36 67 30 08 00 45 00 ........ r6g0..E.
      0010 00 1d 6f 9a 00 00 80 11 07 8a c0 a8 03 04 ff ff ..o..... ........
      0020 ff ff 05 8b 05 9a 00 09 2f 0b 02 ........ /..

      And this is what I sent

      0000 ff ff ff ff ff ff 00 13 72 36 67 30 08 00 45 00 ........ r6g0..E.
      0010 00 22 8c 51 00 00 80 11 ea cd c0 a8 03 04 ff ff .".Q.... ........
      0020 ff ff 06 bc 05 9a 00 0e 9c 8f 30 78 32 66 30 62 ........ ..0x2f0b

      G Offline
      G Offline
      Goto_Label_
      wrote on last edited by
      #2

      Since you got no error messages.. Gee, im not sure.. could you please post more of your code? look here for hints.

      J 1 Reply Last reply
      0
      • J jkirkerx

        I copied some sample code from msdn to create a socket, and modified it for testing. I want to broadcast a udp dgram, but I cant figure out what to send. I'm using ethereal to examine what SQL Management sends, and I'm trying to copy the behavior. So it looks like my first send is accurate, but I'm not sending the correct data out for a response back from sqlbrowser. I'm convinced if I send the right stuff, I will get my responses back from all the SQL servers. If i'm in the wrong forum sorry, didn't really know where to post it. I tried 0x02 in the send buffer. iResult = getaddrinfo( "255.255.255.255", "ms-sql-m", &hints, &result ); I think this is what SQL Management sent

        0000 ff ff ff ff ff ff 00 13 72 36 67 30 08 00 45 00 ........ r6g0..E.
        0010 00 1d 6f 9a 00 00 80 11 07 8a c0 a8 03 04 ff ff ..o..... ........
        0020 ff ff 05 8b 05 9a 00 09 2f 0b 02 ........ /..

        And this is what I sent

        0000 ff ff ff ff ff ff 00 13 72 36 67 30 08 00 45 00 ........ r6g0..E.
        0010 00 22 8c 51 00 00 80 11 ea cd c0 a8 03 04 ff ff .".Q.... ........
        0020 ff ff 06 bc 05 9a 00 0e 9c 8f 30 78 32 66 30 62 ........ ..0x2f0b

        A Offline
        A Offline
        Albert Holguin
        wrote on last edited by
        #3

        Is that what's in the UDP data portion of the packet (not sure where UDP header ended and your data started)? If so, then you can just make a buffer that has the same exact data, shouldn't be hard just do something like:

        //Only define data here not the UDP header info
        const char RequestBuffer[] = {0xff,0xff,...,0x02} //pseudo-code, enter all numbers one byte at a time
        //Remember char type is one byte... and one byte is two hex digits

        ...then just send that... except, if you're not really sure what's being sent, you may not get exactly what you're expecting... there's probably an API that defines the interactions between a "SQL Manager" and other SQL servers. If you're trying to create an application that simulates a manager, you should probably find whatever standard defines those interactions and implement that instead of just trying to blindly copy a data packet. Good luck. :)

        J 1 Reply Last reply
        0
        • A Albert Holguin

          Is that what's in the UDP data portion of the packet (not sure where UDP header ended and your data started)? If so, then you can just make a buffer that has the same exact data, shouldn't be hard just do something like:

          //Only define data here not the UDP header info
          const char RequestBuffer[] = {0xff,0xff,...,0x02} //pseudo-code, enter all numbers one byte at a time
          //Remember char type is one byte... and one byte is two hex digits

          ...then just send that... except, if you're not really sure what's being sent, you may not get exactly what you're expecting... there's probably an API that defines the interactions between a "SQL Manager" and other SQL servers. If you're trying to create an application that simulates a manager, you should probably find whatever standard defines those interactions and implement that instead of just trying to blindly copy a data packet. Good luck. :)

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

          I know the basics, 8 bit makes a byte, and so forth, but I can't read the code I posted. I am however getting better at using the ethereal, and highlighting the portions of the transmission, such as data. The data being sent seems to be just 1 byte, with a value of 02, and is represented as a dot . I was thinking last night that perhaps instead of sending a char, like you would use to request a web page, I need to send a bytes.

          A 1 Reply Last reply
          0
          • G Goto_Label_

            Since you got no error messages.. Gee, im not sure.. could you please post more of your code? look here for hints.

            J Offline
            J Offline
            jkirkerx
            wrote on last edited by
            #5

            I hard coded the value for experimenting, The sample code I'm using is for sending text like requesting a web page from a server, and I think I need to send hex code, so I'm going to try and figure out how to send data in a different format. I think the other article in Code Project is correct, in which I need to send 0x02. The szSendBuf is where I place the data.

            char* szSendBuf = "STX";
            char recvbuf[DEFAULT_BUFLEN];
            int iResult;
            int recvbuflen = DEFAULT_BUFLEN;

            // Initialize Winsock
            iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
            if (iResult != 0) {
                printf("WSAStartup failed with error: %d\\n", iResult);
                return 1;
            }
            
            ZeroMemory( &hints, sizeof(hints) );
            hints.ai\_family = AF\_INET;
            hints.ai\_socktype = SOCK\_DGRAM;
            hints.ai\_protocol = IPPROTO\_UDP;
            
            // Resolve the server address and port
            iResult = getaddrinfo(
                "255.255.255.255",
                "ms-sql-m",
                &hints,
                &result
            );
            
            if ( iResult != 0 ) {
                printf("getaddrinfo failed with error: %d\\n", iResult);
                WSACleanup();
                //return 1;
            }
            
            // Attempt to connect to an address until one succeeds
            for(ptr=result; ptr != NULL ;ptr=ptr->ai\_next) {
            
                // Create a SOCKET for connecting to server
                ConnectSocket = socket(ptr->ai\_family, ptr->ai\_socktype, ptr->ai\_protocol);
            
                if (ConnectSocket == INVALID\_SOCKET) {
                    printf("socket failed with error: %ld\\n", WSAGetLastError());
                    WSACleanup();
                    return 1;
                }
            
                // Connect to server.
                iResult = connect( ConnectSocket, ptr->ai\_addr, (int)ptr->ai\_addrlen);
                if (iResult == SOCKET\_ERROR) {
                    closesocket(ConnectSocket);
                    ConnectSocket = INVALID\_SOCKET;
                    continue;
                }
                break;
            }
            
            freeaddrinfo(result);
            
            if (ConnectSocket == INVALID\_SOCKET) {
                printf("Unable to connect to server!\\n");
                WSACleanup();
                return 1;
            }
            
            // Send an initial buffer
            iResult = send(
                ConnectSocket,
                szSendBuf,
                (int)strlen(szSendBuf),
                0
            );
            
            1 Reply Last reply
            0
            • J jkirkerx

              I know the basics, 8 bit makes a byte, and so forth, but I can't read the code I posted. I am however getting better at using the ethereal, and highlighting the portions of the transmission, such as data. The data being sent seems to be just 1 byte, with a value of 02, and is represented as a dot . I was thinking last night that perhaps instead of sending a char, like you would use to request a web page, I need to send a bytes.

              A Offline
              A Offline
              Albert Holguin
              wrote on last edited by
              #6

              If the actual data that is being sent is a x02 (hex), then that's what you need to send... looking at your packet, it looks like you have something like "0x2f0b" (as text, not bytes)... that seems like you're accidentally sending the address of your buffer instead of the values contained in it.

              J 1 Reply Last reply
              0
              • A Albert Holguin

                If the actual data that is being sent is a x02 (hex), then that's what you need to send... looking at your packet, it looks like you have something like "0x2f0b" (as text, not bytes)... that seems like you're accidentally sending the address of your buffer instead of the values contained in it.

                J Offline
                J Offline
                jkirkerx
                wrote on last edited by
                #7

                ohhhhhhhhhhh. I think I'm closer now. I'm sending cc, but I'm suppose to send 20. I manually altered the buffer size in send to 1, instead of using (int)strlen(sendbuf) So I think I'm building the data too big. This is a lot to learn, but I need to learn it. here's what I'm using now, I think I'm on the right track

                BOOL CA_SQLServer_Scan::_socket_Broadcast_SQLServers( void )
                {
                #define DEFAULT_BUFLEN 512
                #define DEFAULT_PORT 1434

                int iResult;
                WSADATA wsaData;
                
                SOCKET ConnectSocket = INVALID\_SOCKET;
                struct sockaddr\_in clientService; 
                
                int recvbuflen = DEFAULT\_BUFLEN;	
                char recvbuf\[DEFAULT\_BUFLEN\] = "";
                
                char mac\[5\];
                char sendbuf\[1\];
                
                memset(sendbuf, 0x00, sizeof(sendbuf)); // I added this to package the data, 
                strcpy(sendbuf, "");
                sprintf(sendbuf+strlen(sendbuf), "", strlen(sendbuf), sendbuf);
                memcpy(sendbuf+strlen(sendbuf), mac, 5); // 
                    
                iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
                if (iResult != NO\_ERROR) {
                    wprintf(L"WSAStartup failed with error: %d\\n", iResult);
                    return 1;
                }
                
                ConnectSocket = socket(AF\_INET, SOCK\_DGRAM, IPPROTO\_UDP);
                if (ConnectSocket == INVALID\_SOCKET) {
                    wprintf(L"socket failed with error: %ld\\n", WSAGetLastError());
                    WSACleanup();
                    return 1;
                }
                
                clientService.sin\_family = AF\_INET;
                clientService.sin\_addr.s\_addr = inet\_addr( "255.255.255.255" );
                clientService.sin\_port = htons( DEFAULT\_PORT );
                
                iResult = connect( ConnectSocket, (SOCKADDR\*) &clientService, sizeof(clientService) );
                if (iResult == SOCKET\_ERROR) {
                    wprintf(L"connect failed with error: %d\\n", WSAGetLastError() );
                    closesocket(ConnectSocket);
                    WSACleanup();
                    return 1;
                

                }

                iResult = send( ConnectSocket, sendbuf, 1, 0 ); // I altered it to 1 
                if (iResult == SOCKET\_ERROR) {
                    wprintf(L"send failed with error: %d\\n", WSAGetLastError());
                    closesocket(ConnectSocket);
                    WSACleanup();
                    return 1;
                }
                
                printf("Bytes Sent: %d\\n", iResult);
                    
                iResult = shutdown(ConnectSocket, SD\_SEND);
                if (iResult == SOCKET\_ERROR) {
                    wprintf(L"shutdown failed with error: %d\\n", WSAGetLastError());
                    closesocket(ConnectSocket);
                    WSACleanup();
                    return 1;
                }
                
                do {
                
                    iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
                    if ( iResult > 0 )
                        wprintf(L"Bytes received: %d\\n", iResult);
                    else if ( iResult == 0 )
                
                A 1 Reply Last reply
                0
                • J jkirkerx

                  I copied some sample code from msdn to create a socket, and modified it for testing. I want to broadcast a udp dgram, but I cant figure out what to send. I'm using ethereal to examine what SQL Management sends, and I'm trying to copy the behavior. So it looks like my first send is accurate, but I'm not sending the correct data out for a response back from sqlbrowser. I'm convinced if I send the right stuff, I will get my responses back from all the SQL servers. If i'm in the wrong forum sorry, didn't really know where to post it. I tried 0x02 in the send buffer. iResult = getaddrinfo( "255.255.255.255", "ms-sql-m", &hints, &result ); I think this is what SQL Management sent

                  0000 ff ff ff ff ff ff 00 13 72 36 67 30 08 00 45 00 ........ r6g0..E.
                  0010 00 1d 6f 9a 00 00 80 11 07 8a c0 a8 03 04 ff ff ..o..... ........
                  0020 ff ff 05 8b 05 9a 00 09 2f 0b 02 ........ /..

                  And this is what I sent

                  0000 ff ff ff ff ff ff 00 13 72 36 67 30 08 00 45 00 ........ r6g0..E.
                  0010 00 22 8c 51 00 00 80 11 ea cd c0 a8 03 04 ff ff .".Q.... ........
                  0020 ff ff 06 bc 05 9a 00 0e 9c 8f 30 78 32 66 30 62 ........ ..0x2f0b

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

                  Finally figured it out, and got back packets from all the SQL Servers on the network, complete with all the information I need. So now I know how to send a STX command to SQL Browser, to start a conversation. It's sort of like a modem command. Now I have to figure out how and when to release the receive side of the socket, and only capture the packets that I need. So the SendMessage, which is a char string, has to be set with memset, in order to send just the byte.

                  A 1 Reply Last reply
                  0
                  • J jkirkerx

                    ohhhhhhhhhhh. I think I'm closer now. I'm sending cc, but I'm suppose to send 20. I manually altered the buffer size in send to 1, instead of using (int)strlen(sendbuf) So I think I'm building the data too big. This is a lot to learn, but I need to learn it. here's what I'm using now, I think I'm on the right track

                    BOOL CA_SQLServer_Scan::_socket_Broadcast_SQLServers( void )
                    {
                    #define DEFAULT_BUFLEN 512
                    #define DEFAULT_PORT 1434

                    int iResult;
                    WSADATA wsaData;
                    
                    SOCKET ConnectSocket = INVALID\_SOCKET;
                    struct sockaddr\_in clientService; 
                    
                    int recvbuflen = DEFAULT\_BUFLEN;	
                    char recvbuf\[DEFAULT\_BUFLEN\] = "";
                    
                    char mac\[5\];
                    char sendbuf\[1\];
                    
                    memset(sendbuf, 0x00, sizeof(sendbuf)); // I added this to package the data, 
                    strcpy(sendbuf, "");
                    sprintf(sendbuf+strlen(sendbuf), "", strlen(sendbuf), sendbuf);
                    memcpy(sendbuf+strlen(sendbuf), mac, 5); // 
                        
                    iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
                    if (iResult != NO\_ERROR) {
                        wprintf(L"WSAStartup failed with error: %d\\n", iResult);
                        return 1;
                    }
                    
                    ConnectSocket = socket(AF\_INET, SOCK\_DGRAM, IPPROTO\_UDP);
                    if (ConnectSocket == INVALID\_SOCKET) {
                        wprintf(L"socket failed with error: %ld\\n", WSAGetLastError());
                        WSACleanup();
                        return 1;
                    }
                    
                    clientService.sin\_family = AF\_INET;
                    clientService.sin\_addr.s\_addr = inet\_addr( "255.255.255.255" );
                    clientService.sin\_port = htons( DEFAULT\_PORT );
                    
                    iResult = connect( ConnectSocket, (SOCKADDR\*) &clientService, sizeof(clientService) );
                    if (iResult == SOCKET\_ERROR) {
                        wprintf(L"connect failed with error: %d\\n", WSAGetLastError() );
                        closesocket(ConnectSocket);
                        WSACleanup();
                        return 1;
                    

                    }

                    iResult = send( ConnectSocket, sendbuf, 1, 0 ); // I altered it to 1 
                    if (iResult == SOCKET\_ERROR) {
                        wprintf(L"send failed with error: %d\\n", WSAGetLastError());
                        closesocket(ConnectSocket);
                        WSACleanup();
                        return 1;
                    }
                    
                    printf("Bytes Sent: %d\\n", iResult);
                        
                    iResult = shutdown(ConnectSocket, SD\_SEND);
                    if (iResult == SOCKET\_ERROR) {
                        wprintf(L"shutdown failed with error: %d\\n", WSAGetLastError());
                        closesocket(ConnectSocket);
                        WSACleanup();
                        return 1;
                    }
                    
                    do {
                    
                        iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
                        if ( iResult > 0 )
                            wprintf(L"Bytes received: %d\\n", iResult);
                        else if ( iResult == 0 )
                    
                    A Offline
                    A Offline
                    Albert Holguin
                    wrote on last edited by
                    #9

                    why not just... ???

                    char sendbuf[] = {0x02};

                    J 2 Replies Last reply
                    0
                    • J jkirkerx

                      Finally figured it out, and got back packets from all the SQL Servers on the network, complete with all the information I need. So now I know how to send a STX command to SQL Browser, to start a conversation. It's sort of like a modem command. Now I have to figure out how and when to release the receive side of the socket, and only capture the packets that I need. So the SendMessage, which is a char string, has to be set with memset, in order to send just the byte.

                      A Offline
                      A Offline
                      Albert Holguin
                      wrote on last edited by
                      #10

                      A char* is not necessarily a string... it's just a char pointer... in network programming, character buffers are commonly used to hold the data being transferred to/from a socket because of the size of a char is one byte. It's just convenient to use that since it's the smallest data element used/transferred.

                      1 Reply Last reply
                      0
                      • A Albert Holguin

                        why not just... ???

                        char sendbuf[] = {0x02};

                        J Offline
                        J Offline
                        jkirkerx
                        wrote on last edited by
                        #11

                        I tried and sent the actual string 0x02

                        1 Reply Last reply
                        0
                        • A Albert Holguin

                          why not just... ???

                          char sendbuf[] = {0x02};

                          J Offline
                          J Offline
                          jkirkerx
                          wrote on last edited by
                          #12

                          I took out the memset and did the straight char, yes it works and it's faster, OK, feel stupid but I did not fully understand the process. I tried it earlier, but got some kind of Int error. Do you know anything about the receive side of the socket? it just sits there and never closes, or transfer data to the receive buffer.

                          A 1 Reply Last reply
                          0
                          • J jkirkerx

                            I took out the memset and did the straight char, yes it works and it's faster, OK, feel stupid but I did not fully understand the process. I tried it earlier, but got some kind of Int error. Do you know anything about the receive side of the socket? it just sits there and never closes, or transfer data to the receive buffer.

                            A Offline
                            A Offline
                            Albert Holguin
                            wrote on last edited by
                            #13

                            I don't remember exactly off hand what it should do... but since UDP is essentially connectionless (meaning no hand-shaking between server and client), I imagine the socket has to be closed explicitly by the application.

                            jkirkerx wrote:

                            or transfer data to the receive buffer.

                            Don't know exactly what you mean by that.

                            J 1 Reply Last reply
                            0
                            • A Albert Holguin

                              I don't remember exactly off hand what it should do... but since UDP is essentially connectionless (meaning no hand-shaking between server and client), I imagine the socket has to be closed explicitly by the application.

                              jkirkerx wrote:

                              or transfer data to the receive buffer.

                              Don't know exactly what you mean by that.

                              J Offline
                              J Offline
                              jkirkerx
                              wrote on last edited by
                              #14

                              I took a generic winsock2 sample code and modified it for UDP broadcast. I have come to the same conclusion, in which the socket will never close, because it's not an single machine to machine connection, in which the other side will never send the symbol saying it's done, or simply close it's port. I just need to figure out how to capture the return data. Not sure if I need 2 sockets, one to just send the broadcast, and the other to just listen. If I do that, I'm afraid the listening socket will cause my code to stop, and I won't be able do anything until the socket closes.

                              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