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. working with byte arrays

working with byte arrays

Scheduled Pinned Locked Moved C / C++ / MFC
data-structurestutorialquestion
29 Posts 7 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 Albert Holguin

    Don't convert it to a string... that's a bad idea... extract whatever data bytes according to whatever API you happen to be using, then cast the converted bytes to whatever type they're supposed to be. As far as whether you need to copy the data before passing it, it depends on where you defined your recvbuff[] and whether it'll be used again, if the buffer is a local variable within your receive method, then it'll automatically go out of scope when the method is finished, so you should copy the data... also, if the buffer is to be used repeatedly (receiving multiple packets), then the data should be copied over to another buffer before moving on, otherwise it might be overwritten before it's used downstream.

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

    The research I did made it sound like std:string was the swiss army knife or manipulation. I used memset to copy the data, because I could not figure out how to use strcpy, the copy has the first 2 bytes, and stopped. I still can't figure out why when I pass the data, I only get the first 2 bytes. You wisdom gives me a map to use, so I can focus on the copy and pass, then I have to tackle the data extraction. Thank you very much jkirkerx

    1 Reply Last reply
    0
    • J jkirkerx

      I understand now that the data I receive from a socket is a byte array. But I don't understand how to pass the byte array to a function for further processing. Plus I'm not sure if I should leave it as a byte array, or try to convert it to a string, so I can extract data from it. I'm confused as to whether this buffer is a pointer to data somewhere else, and If I need to make a copy of it, before passing it to a function. When I pass it to a function, the data is lost. Kind of tired of going around in circles here on this, and I need road map to head in the right direction. I make my receive buffer char recvbuf[256]; I get data 0x05 '' 0x54 'T' 0x53 'S' 0x65 'e' And I try to pass it to a function for processing. Unless just extracting the info I need does not require a function. Suggestion? void _process(char *pbuffer) { }

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #6

      You seem to be working at this from the wrong direction. The first thing you need to do is work out (or ask the sender) the exact format of the data you are receiving. All data read from sockets is presented as a stream of bytes, and without knowing the significance, format and values of each byte or group of bytes, there is no way you can do anything with the information you receive. Never assume that data is presented as a string unless you are certain that is how it has been sent.

      Unrequited desire is character building. OriginalGriff

      J 1 Reply Last reply
      0
      • J jkirkerx

        I understand now that the data I receive from a socket is a byte array. But I don't understand how to pass the byte array to a function for further processing. Plus I'm not sure if I should leave it as a byte array, or try to convert it to a string, so I can extract data from it. I'm confused as to whether this buffer is a pointer to data somewhere else, and If I need to make a copy of it, before passing it to a function. When I pass it to a function, the data is lost. Kind of tired of going around in circles here on this, and I need road map to head in the right direction. I make my receive buffer char recvbuf[256]; I get data 0x05 '' 0x54 'T' 0x53 'S' 0x65 'e' And I try to pass it to a function for processing. Unless just extracting the info I need does not require a function. Suggestion? void _process(char *pbuffer) { }

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #7

        Yeah, if you know the data is a char array, pass it to the func as you outineline in your post, and let it do the processing. It is bette to do this, than to proces the data at the level of the code that manages socket communication, it is a more logical split to treat communication at one level, and data processing at another.

        ============================== Nothing to say.

        J 1 Reply Last reply
        0
        • L Lost User

          You seem to be working at this from the wrong direction. The first thing you need to do is work out (or ask the sender) the exact format of the data you are receiving. All data read from sockets is presented as a stream of bytes, and without knowing the significance, format and values of each byte or group of bytes, there is no way you can do anything with the information you receive. Never assume that data is presented as a string unless you are certain that is how it has been sent.

          Unrequited desire is character building. OriginalGriff

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

          The sender is microsoft's sql browser. I discovered the return packet data using etherreal, and sending various request to it, until I got the reply I was looking for. Then I wrote the socket program. I have the data packets coming back from all the sql servers on the lan, and they all send the same format back to me. I just need to extract the server name and version number of sql out of the byte array.

          L 1 Reply Last reply
          0
          • L Lost User

            Yeah, if you know the data is a char array, pass it to the func as you outineline in your post, and let it do the processing. It is bette to do this, than to proces the data at the level of the code that manages socket communication, it is a more logical split to treat communication at one level, and data processing at another.

            ============================== Nothing to say.

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

            I write in vb, and without a doubt, I would pass the data to another function for processing, or just store the data, and process it later. In straight c++, most of the stuff seems pretty easy, but there are some really low level stuff you can get into that gets tricky. I've never asked anyone to write code for me, but I'm really stuck trying to figure out how to pass the data to a another function. I will post my code, and the byte stream I get back, and perhaps someone could can spot the mistake I made, it might be something really small that I didn't get.

            BOOL CA_SQLServer_Scan::_socket_Enumerate_SQLServers( void )
            {
            int iResult;
            WSADATA wsaData;

            char recvbuf\[256\];
            int rv = 0; 
            int recvbuflen = 256;
            int bytesReceived = 0;
            				    
            iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
            if (iResult != NO\_ERROR) {
                return FALSE;
            }		
            //////////////////////////////////////////////////////////////////////////////////////
            // Send Data Socket
            SOCKET sUDPSocket = INVALID\_SOCKET;
            struct sockaddr\_in sTargetDevice;
            char cBuffer\[\] = {0x02};
            
            sUDPSocket = socket(AF\_INET, SOCK\_DGRAM, IPPROTO\_UDP);
            if (sUDPSocket == INVALID\_SOCKET) {
                int iSocketError = WSAGetLastError();
            	goto wsaCleanup;
            }
            
            ZeroMemory( &sTargetDevice, sizeof(sTargetDevice));
            sTargetDevice.sin\_family = AF\_INET;
            sTargetDevice.sin\_addr.s\_addr = inet\_addr( "192.168.3.255" );
            sTargetDevice.sin\_port = htons( BROADCAST\_SND\_PORT );
            
            // Try a Connectionless Send Data first
            iResult = sendto(sUDPSocket, cBuffer, 1, 0, (SOCKADDR \*) & sTargetDevice, sizeof (sTargetDevice));
            if (iResult == SOCKET\_ERROR) {
                goto closeSocket;        
            }
                
            // Shutdown the send socket for some reason
            iResult = shutdown(sUDPSocket, SD\_SEND);
            if (iResult == SOCKET\_ERROR) {
                goto closeSocket;
            }
            // End of Send Data Connection
            //////////////////////////////////////////////////////////////////////////////////////
            // Enumerate Return Packets	
            do {
                bytesReceived = recv(sUDPSocket, recvbuf, recvbuflen, 0);
                if ( bytesReceived > 0 ) {            
            		char buf\[256\];
            		recvbuf\[bytesReceived\] = '\\0';
            		memcpy(buf, recvbuf, bytesReceived);			
            		\_process\_SQL\_BufferData(buf, bytesReceived);						
            	}
                else if ( bytesReceived == 0 ) {
                    printf("Connection closed\\n");
            		goto closeSocket;
            	}		
            	else {
                    printf("recv failed: %d\\n", WSAGetLastError());
            		goto closeSocket;
            	}
            
            G L A 3 Replies Last reply
            0
            • J jkirkerx

              The sender is microsoft's sql browser. I discovered the return packet data using etherreal, and sending various request to it, until I got the reply I was looking for. Then I wrote the socket program. I have the data packets coming back from all the sql servers on the lan, and they all send the same format back to me. I just need to extract the server name and version number of sql out of the byte array.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #10

              jkirkerx wrote:

              I just need to extract the server name and version number of sql out of the byte array.

              Given that you have figured out the format and content of the messages, what is the problem?

              Unrequited desire is character building. OriginalGriff

              1 Reply Last reply
              0
              • J jkirkerx

                I write in vb, and without a doubt, I would pass the data to another function for processing, or just store the data, and process it later. In straight c++, most of the stuff seems pretty easy, but there are some really low level stuff you can get into that gets tricky. I've never asked anyone to write code for me, but I'm really stuck trying to figure out how to pass the data to a another function. I will post my code, and the byte stream I get back, and perhaps someone could can spot the mistake I made, it might be something really small that I didn't get.

                BOOL CA_SQLServer_Scan::_socket_Enumerate_SQLServers( void )
                {
                int iResult;
                WSADATA wsaData;

                char recvbuf\[256\];
                int rv = 0; 
                int recvbuflen = 256;
                int bytesReceived = 0;
                				    
                iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
                if (iResult != NO\_ERROR) {
                    return FALSE;
                }		
                //////////////////////////////////////////////////////////////////////////////////////
                // Send Data Socket
                SOCKET sUDPSocket = INVALID\_SOCKET;
                struct sockaddr\_in sTargetDevice;
                char cBuffer\[\] = {0x02};
                
                sUDPSocket = socket(AF\_INET, SOCK\_DGRAM, IPPROTO\_UDP);
                if (sUDPSocket == INVALID\_SOCKET) {
                    int iSocketError = WSAGetLastError();
                	goto wsaCleanup;
                }
                
                ZeroMemory( &sTargetDevice, sizeof(sTargetDevice));
                sTargetDevice.sin\_family = AF\_INET;
                sTargetDevice.sin\_addr.s\_addr = inet\_addr( "192.168.3.255" );
                sTargetDevice.sin\_port = htons( BROADCAST\_SND\_PORT );
                
                // Try a Connectionless Send Data first
                iResult = sendto(sUDPSocket, cBuffer, 1, 0, (SOCKADDR \*) & sTargetDevice, sizeof (sTargetDevice));
                if (iResult == SOCKET\_ERROR) {
                    goto closeSocket;        
                }
                    
                // Shutdown the send socket for some reason
                iResult = shutdown(sUDPSocket, SD\_SEND);
                if (iResult == SOCKET\_ERROR) {
                    goto closeSocket;
                }
                // End of Send Data Connection
                //////////////////////////////////////////////////////////////////////////////////////
                // Enumerate Return Packets	
                do {
                    bytesReceived = recv(sUDPSocket, recvbuf, recvbuflen, 0);
                    if ( bytesReceived > 0 ) {            
                		char buf\[256\];
                		recvbuf\[bytesReceived\] = '\\0';
                		memcpy(buf, recvbuf, bytesReceived);			
                		\_process\_SQL\_BufferData(buf, bytesReceived);						
                	}
                    else if ( bytesReceived == 0 ) {
                        printf("Connection closed\\n");
                		goto closeSocket;
                	}		
                	else {
                        printf("recv failed: %d\\n", WSAGetLastError());
                		goto closeSocket;
                	}
                
                G Offline
                G Offline
                Goto_Label_
                wrote on last edited by
                #11

                Does this code help point you in the right direction? rcvBuffer is passed to the function processData

                #include #include void processData(char &c)
                {
                char* array = &c;
                std::cout << array << std::endl;
                }

                int main()
                {

                char\* rcvBuffer = "Hello world";
                char& c = rcvBuffer\[0\];
                processData(c);
                

                return 0;
                }

                1 Reply Last reply
                0
                • J jkirkerx

                  I write in vb, and without a doubt, I would pass the data to another function for processing, or just store the data, and process it later. In straight c++, most of the stuff seems pretty easy, but there are some really low level stuff you can get into that gets tricky. I've never asked anyone to write code for me, but I'm really stuck trying to figure out how to pass the data to a another function. I will post my code, and the byte stream I get back, and perhaps someone could can spot the mistake I made, it might be something really small that I didn't get.

                  BOOL CA_SQLServer_Scan::_socket_Enumerate_SQLServers( void )
                  {
                  int iResult;
                  WSADATA wsaData;

                  char recvbuf\[256\];
                  int rv = 0; 
                  int recvbuflen = 256;
                  int bytesReceived = 0;
                  				    
                  iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
                  if (iResult != NO\_ERROR) {
                      return FALSE;
                  }		
                  //////////////////////////////////////////////////////////////////////////////////////
                  // Send Data Socket
                  SOCKET sUDPSocket = INVALID\_SOCKET;
                  struct sockaddr\_in sTargetDevice;
                  char cBuffer\[\] = {0x02};
                  
                  sUDPSocket = socket(AF\_INET, SOCK\_DGRAM, IPPROTO\_UDP);
                  if (sUDPSocket == INVALID\_SOCKET) {
                      int iSocketError = WSAGetLastError();
                  	goto wsaCleanup;
                  }
                  
                  ZeroMemory( &sTargetDevice, sizeof(sTargetDevice));
                  sTargetDevice.sin\_family = AF\_INET;
                  sTargetDevice.sin\_addr.s\_addr = inet\_addr( "192.168.3.255" );
                  sTargetDevice.sin\_port = htons( BROADCAST\_SND\_PORT );
                  
                  // Try a Connectionless Send Data first
                  iResult = sendto(sUDPSocket, cBuffer, 1, 0, (SOCKADDR \*) & sTargetDevice, sizeof (sTargetDevice));
                  if (iResult == SOCKET\_ERROR) {
                      goto closeSocket;        
                  }
                      
                  // Shutdown the send socket for some reason
                  iResult = shutdown(sUDPSocket, SD\_SEND);
                  if (iResult == SOCKET\_ERROR) {
                      goto closeSocket;
                  }
                  // End of Send Data Connection
                  //////////////////////////////////////////////////////////////////////////////////////
                  // Enumerate Return Packets	
                  do {
                      bytesReceived = recv(sUDPSocket, recvbuf, recvbuflen, 0);
                      if ( bytesReceived > 0 ) {            
                  		char buf\[256\];
                  		recvbuf\[bytesReceived\] = '\\0';
                  		memcpy(buf, recvbuf, bytesReceived);			
                  		\_process\_SQL\_BufferData(buf, bytesReceived);						
                  	}
                      else if ( bytesReceived == 0 ) {
                          printf("Connection closed\\n");
                  		goto closeSocket;
                  	}		
                  	else {
                          printf("recv failed: %d\\n", WSAGetLastError());
                  		goto closeSocket;
                  	}
                  
                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #12

                  assuming this is the format of all messages that you need to process, it looks like the message format is :

                  Addr Content
                  0 05
                  1 0084 (as bytes 8400)
                  3 "ServerName;DELLC521-01;InstanceName;SQLEXPRESS;IsClustered;No;Version;10.50.1600.1;;"

                  Byte zero contains some flag or identifier (no idea what)
                  Bytes 1 & 2 contain a 16-bit integer giving the length of the following data
                  Bytes 3 - n contain the message data, which in this case is a semi-colon delimited string.

                  So given the above information your message processor can copy the characters (using the length information) into a proper null-terminated string buffer. That string may then be split into tokens and each required value can be found by comparing the keywords with known tokens. Something like:

                  int msgLength = buffer[2] << 8 + buffer[1];
                  char* szWords = new char[msgLength + 1];
                  strncpy(szWords, buffer + 3, msgLength);
                  // remainder of code left as an exercise for the reader
                  // use strtok() to extract each token
                  // strcmp() to look for keywords
                  //
                  // alternatively use std::string to parse and extract

                  Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

                  A J 3 Replies Last reply
                  0
                  • L Lost User

                    assuming this is the format of all messages that you need to process, it looks like the message format is :

                    Addr Content
                    0 05
                    1 0084 (as bytes 8400)
                    3 "ServerName;DELLC521-01;InstanceName;SQLEXPRESS;IsClustered;No;Version;10.50.1600.1;;"

                    Byte zero contains some flag or identifier (no idea what)
                    Bytes 1 & 2 contain a 16-bit integer giving the length of the following data
                    Bytes 3 - n contain the message data, which in this case is a semi-colon delimited string.

                    So given the above information your message processor can copy the characters (using the length information) into a proper null-terminated string buffer. That string may then be split into tokens and each required value can be found by comparing the keywords with known tokens. Something like:

                    int msgLength = buffer[2] << 8 + buffer[1];
                    char* szWords = new char[msgLength + 1];
                    strncpy(szWords, buffer + 3, msgLength);
                    // remainder of code left as an exercise for the reader
                    // use strtok() to extract each token
                    // strcmp() to look for keywords
                    //
                    // alternatively use std::string to parse and extract

                    Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

                    A Offline
                    A Offline
                    App_
                    wrote on last edited by
                    #13

                    :thumbsup:

                    1 Reply Last reply
                    0
                    • J jkirkerx

                      I write in vb, and without a doubt, I would pass the data to another function for processing, or just store the data, and process it later. In straight c++, most of the stuff seems pretty easy, but there are some really low level stuff you can get into that gets tricky. I've never asked anyone to write code for me, but I'm really stuck trying to figure out how to pass the data to a another function. I will post my code, and the byte stream I get back, and perhaps someone could can spot the mistake I made, it might be something really small that I didn't get.

                      BOOL CA_SQLServer_Scan::_socket_Enumerate_SQLServers( void )
                      {
                      int iResult;
                      WSADATA wsaData;

                      char recvbuf\[256\];
                      int rv = 0; 
                      int recvbuflen = 256;
                      int bytesReceived = 0;
                      				    
                      iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
                      if (iResult != NO\_ERROR) {
                          return FALSE;
                      }		
                      //////////////////////////////////////////////////////////////////////////////////////
                      // Send Data Socket
                      SOCKET sUDPSocket = INVALID\_SOCKET;
                      struct sockaddr\_in sTargetDevice;
                      char cBuffer\[\] = {0x02};
                      
                      sUDPSocket = socket(AF\_INET, SOCK\_DGRAM, IPPROTO\_UDP);
                      if (sUDPSocket == INVALID\_SOCKET) {
                          int iSocketError = WSAGetLastError();
                      	goto wsaCleanup;
                      }
                      
                      ZeroMemory( &sTargetDevice, sizeof(sTargetDevice));
                      sTargetDevice.sin\_family = AF\_INET;
                      sTargetDevice.sin\_addr.s\_addr = inet\_addr( "192.168.3.255" );
                      sTargetDevice.sin\_port = htons( BROADCAST\_SND\_PORT );
                      
                      // Try a Connectionless Send Data first
                      iResult = sendto(sUDPSocket, cBuffer, 1, 0, (SOCKADDR \*) & sTargetDevice, sizeof (sTargetDevice));
                      if (iResult == SOCKET\_ERROR) {
                          goto closeSocket;        
                      }
                          
                      // Shutdown the send socket for some reason
                      iResult = shutdown(sUDPSocket, SD\_SEND);
                      if (iResult == SOCKET\_ERROR) {
                          goto closeSocket;
                      }
                      // End of Send Data Connection
                      //////////////////////////////////////////////////////////////////////////////////////
                      // Enumerate Return Packets	
                      do {
                          bytesReceived = recv(sUDPSocket, recvbuf, recvbuflen, 0);
                          if ( bytesReceived > 0 ) {            
                      		char buf\[256\];
                      		recvbuf\[bytesReceived\] = '\\0';
                      		memcpy(buf, recvbuf, bytesReceived);			
                      		\_process\_SQL\_BufferData(buf, bytesReceived);						
                      	}
                          else if ( bytesReceived == 0 ) {
                              printf("Connection closed\\n");
                      		goto closeSocket;
                      	}		
                      	else {
                              printf("recv failed: %d\\n", WSAGetLastError());
                      		goto closeSocket;
                      	}
                      
                      A Offline
                      A Offline
                      App_
                      wrote on last edited by
                      #14

                      is done like this:

                      #include <iostream>
                      using namespace std;

                      void charray(char test[]) {
                      strncpy(test,"test",sizeof(test));
                      }

                      int main() {
                      char test[256]={' '};
                      charray(test);
                      printf("Text in %s!\n", test);
                      return 0;
                      }

                      J L 2 Replies Last reply
                      0
                      • A App_

                        is done like this:

                        #include <iostream>
                        using namespace std;

                        void charray(char test[]) {
                        strncpy(test,"test",sizeof(test));
                        }

                        int main() {
                        char test[256]={' '};
                        charray(test);
                        printf("Text in %s!\n", test);
                        return 0;
                        }

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

                        I experimented with that Friday night, and only the first byte copied over in strncpy. I think the 2nd byte bombed in the copy. The same thing happens when I pass the byte array to a function. It must be the 2nd byte, so perhaps I need to cut off the header of the byte array, and then do the copy and pass.

                        1 Reply Last reply
                        0
                        • L Lost User

                          assuming this is the format of all messages that you need to process, it looks like the message format is :

                          Addr Content
                          0 05
                          1 0084 (as bytes 8400)
                          3 "ServerName;DELLC521-01;InstanceName;SQLEXPRESS;IsClustered;No;Version;10.50.1600.1;;"

                          Byte zero contains some flag or identifier (no idea what)
                          Bytes 1 & 2 contain a 16-bit integer giving the length of the following data
                          Bytes 3 - n contain the message data, which in this case is a semi-colon delimited string.

                          So given the above information your message processor can copy the characters (using the length information) into a proper null-terminated string buffer. That string may then be split into tokens and each required value can be found by comparing the keywords with known tokens. Something like:

                          int msgLength = buffer[2] << 8 + buffer[1];
                          char* szWords = new char[msgLength + 1];
                          strncpy(szWords, buffer + 3, msgLength);
                          // remainder of code left as an exercise for the reader
                          // use strtok() to extract each token
                          // strcmp() to look for keywords
                          //
                          // alternatively use std::string to parse and extract

                          Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

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

                          Give me an hour to digest the knowledge you presented, looks very helpful and promising. perhaps in words, get the message length the packet declared make a new byte array copy the message only to the new byte array, leaving the packet header behind then pass the message to the function, and extract the ; delimited data.

                          1 Reply Last reply
                          0
                          • L Lost User

                            assuming this is the format of all messages that you need to process, it looks like the message format is :

                            Addr Content
                            0 05
                            1 0084 (as bytes 8400)
                            3 "ServerName;DELLC521-01;InstanceName;SQLEXPRESS;IsClustered;No;Version;10.50.1600.1;;"

                            Byte zero contains some flag or identifier (no idea what)
                            Bytes 1 & 2 contain a 16-bit integer giving the length of the following data
                            Bytes 3 - n contain the message data, which in this case is a semi-colon delimited string.

                            So given the above information your message processor can copy the characters (using the length information) into a proper null-terminated string buffer. That string may then be split into tokens and each required value can be found by comparing the keywords with known tokens. Something like:

                            int msgLength = buffer[2] << 8 + buffer[1];
                            char* szWords = new char[msgLength + 1];
                            strncpy(szWords, buffer + 3, msgLength);
                            // remainder of code left as an exercise for the reader
                            // use strtok() to extract each token
                            // strcmp() to look for keywords
                            //
                            // alternatively use std::string to parse and extract

                            Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

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

                            Didn't work at first, then I hardcoded the message length just for testing, and everything worked, including passing the string to the function. I need to figure out the length calculation. Working on that now, almost there, wow

                            int msgLength = 80;
                            char* szWords = new char[msgLength + 1];
                            strncpy(szWords, recvbuf + 3, msgLength);

                            This is the szWords,

                            szWords 0x003e8f10 "ServerName;DELLC521-01;InstanceName;SQLEXPRESS;IsClustered;No;Version;10.50.1600Íýýýý««««««««þîþ"

                            L 1 Reply Last reply
                            0
                            • A App_

                              is done like this:

                              #include <iostream>
                              using namespace std;

                              void charray(char test[]) {
                              strncpy(test,"test",sizeof(test));
                              }

                              int main() {
                              char test[256]={' '};
                              charray(test);
                              printf("Text in %s!\n", test);
                              return 0;
                              }

                              L Offline
                              L Offline
                              Lost User
                              wrote on last edited by
                              #18

                              You cannot use sizeof() in the function above, because the array size is not known at that point.

                              Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

                              1 Reply Last reply
                              0
                              • J jkirkerx

                                Didn't work at first, then I hardcoded the message length just for testing, and everything worked, including passing the string to the function. I need to figure out the length calculation. Working on that now, almost there, wow

                                int msgLength = 80;
                                char* szWords = new char[msgLength + 1];
                                strncpy(szWords, recvbuf + 3, msgLength);

                                This is the szWords,

                                szWords 0x003e8f10 "ServerName;DELLC521-01;InstanceName;SQLEXPRESS;IsClustered;No;Version;10.50.1600Íýýýý««««««««þîþ"

                                L Offline
                                L Offline
                                Lost User
                                wrote on last edited by
                                #19

                                jkirkerx wrote:

                                I need to figure out the length calculation.

                                I gave you the code for that in my previous message; and I missed a line of code, so it should read:

                                int msgLength = buffer[2] << 8 + buffer[1];
                                char* szWords = new char[msgLength + 1];
                                strncpy(szWords, buffer + 3, msgLength);
                                szWords[msgLength] = '\0';

                                Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

                                J 1 Reply Last reply
                                0
                                • L Lost User

                                  jkirkerx wrote:

                                  I need to figure out the length calculation.

                                  I gave you the code for that in my previous message; and I missed a line of code, so it should read:

                                  int msgLength = buffer[2] << 8 + buffer[1];
                                  char* szWords = new char[msgLength + 1];
                                  strncpy(szWords, buffer + 3, msgLength);
                                  szWords[msgLength] = '\0';

                                  Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

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

                                  So I still need to terminate the string with \0 Having trouble with the length, I did quick cheat with

                                  int msgLength = bytesReceived - 3;
                                  //int msgLength = recvbuf[2] << 8 + recvbuf[1];
                                  char* szWords = new char[msgLength + 1];
                                  strncpy(szWords, recvbuf + 3, msgLength);

                                  int msgLength with is an integer, I keep getting 0, is it because recvbuf[2] is returning the string, but wait, it's a byte representing a number. hmm

                                  L 1 Reply Last reply
                                  0
                                  • J jkirkerx

                                    So I still need to terminate the string with \0 Having trouble with the length, I did quick cheat with

                                    int msgLength = bytesReceived - 3;
                                    //int msgLength = recvbuf[2] << 8 + recvbuf[1];
                                    char* szWords = new char[msgLength + 1];
                                    strncpy(szWords, recvbuf + 3, msgLength);

                                    int msgLength with is an integer, I keep getting 0, is it because recvbuf[2] is returning the string, but wait, it's a byte representing a number. hmm

                                    L Offline
                                    L Offline
                                    Lost User
                                    wrote on last edited by
                                    #21

                                    Sorry, I really need to test my code before posting here, and check my operator precedence rules. In my defence, it is getting late and I'm half way through a nice bottle of Merlot :-D Try:

                                    int msgLength = (recvbuf[2] << 8) + recvbuf[1];

                                    Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

                                    J 1 Reply Last reply
                                    0
                                    • L Lost User

                                      Sorry, I really need to test my code before posting here, and check my operator precedence rules. In my defence, it is getting late and I'm half way through a nice bottle of Merlot :-D Try:

                                      int msgLength = (recvbuf[2] << 8) + recvbuf[1];

                                      Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

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

                                      Hey no problem, I thought perhaps I had to solve it as a test of some sort. I answer asp.net server control questions on my last beer before I turn off the computer at night. I want to thank you for helping me. It was really bring me down, and everyone just sort of gave me a blank look, "Its so easy, come on, that c++ 101". Words cannot express the gratitude for the help. I really appreciate it, and yes, code project pulls through with expert help.

                                      G L 2 Replies Last reply
                                      0
                                      • J jkirkerx

                                        Hey no problem, I thought perhaps I had to solve it as a test of some sort. I answer asp.net server control questions on my last beer before I turn off the computer at night. I want to thank you for helping me. It was really bring me down, and everyone just sort of gave me a blank look, "Its so easy, come on, that c++ 101". Words cannot express the gratitude for the help. I really appreciate it, and yes, code project pulls through with expert help.

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

                                        :laugh: great fun but we didnt solve the passing of byte arrays

                                        J 1 Reply Last reply
                                        0
                                        • G Goto_Label_

                                          :laugh: great fun but we didnt solve the passing of byte arrays

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

                                          oh no, it got solved! Richard wrote some code to extract the message size from byte 2, copy just the message to a new char array, termintate it with \0, and pass it to my function for processing I'm stoked, surfer happiness, yeah! I've been banging my head for days on this, feeling pretty stupid about it. Thanks for watching the show! msgLength = (recvbuf[2] << 8) + recvbuf[1]; szWords = new char[msgLength + 1]; strncpy(szWords, recvbuf + 3, msgLength); szWords[msgLength] = '\0'; _process_SQL_BufferData(szWords, bytesReceived);

                                          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