Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Sending Hex values in UDP Packets using C (http://www.go4expert.com/forums/sending-hex-values-udp-packets-using-c-t21679/)

hraja 6Apr2010 08:29

Sending Hex values in UDP Packets using C
 
Hello,

I am writing a C program to send data using UDP. The following
is the client-side code that I have implemented to send data to a server.

[I]#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

int main(int argc, char *argv[])
{
int sock,i;
struct sockaddr_in server_addr;
struct hostent *host;
char send_data[1024],hexstring[1024], temp[1024];
int broadcast = 1;
int numbytes;

host= (struct hostent *) gethostbyname((char *)"255.255.255.255");

if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
{
perror("socket");
exit(1);
}

if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &broadcast,
sizeof broadcast) == -1)
{
perror("setsockopt (SO_BROADCAST)");
exit(1);
}

server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(5000);
server_addr.sin_addr = *((struct in_addr *)host->h_addr);
bzero(&(server_addr.sin_zero),8);

if ((numbytes=sendto(sock, argv[2], strlen(argv[2]), 0,(struct sockaddr *)&server_addr, sizeof server_addr)) == -1)
{
perror("sendto");
exit(1);
}

while (1)
{

printf("Type Something (q or Q to quit):");
gets(send_data);

if ((strcmp(send_data , "q") == 0) || strcmp(send_data , "Q") == 0)
break;

else

Converting entered string into hexadecimal using sprintf()
hexstring[0] = '\0';
temp[0] = '\0';

for (i=0; i<strlen(send_data); i++)
{
sprintf(temp, "%X ", send_data);
strcat(hexstring, temp);
}
sendto(sock,hexstring, strlen(hexstring), 0,(struct sockaddr *)&server_addr, sizeof(struct sockaddr));
}

}


While using this program, my problem is that the converted hexadecimal when sent over to the server is being converted to ASCII again.:undecided

For example, If I sent "Hello" from the client, the server would get "48 65 6C 6C 6F" as string , but the actual datatransfer considers the hex value as ASCII. So over the ethernet,
"48 65 6C 6C 6F" is changed to its hex value, 34 38 20 36 35.

What I am actually
hoping to achieve is to connect to a device on my ethernet which received ONLY hexadecimal values. My original program contains many hex values which have to be sent to the device. But when I am sending these hexvalues, they are considered as ASCII. This is a big problem as the device cannot understand ASCII/String.

As a result the converted hex value is considered as string/ascii and it is converted again to hex and being sent on the actual network.

For example, if I convert the IP address 192.168.18.7 to its hexadecimal values, I hope to sent it to the device as hexadecimal values itself and not consider the hexadecimal of the IP add as ASCII.

It would be great if someone could help me regarding this please. Any help would me much appreciated. Thanks very much.



xpi0t0s 6Apr2010 12:41

Re: Sending Hex values in UDP Packets using C
 
This is as easy as just sending send_data instead of hexstring, isn't it?

So change this...

Code:

printf("Type Something (q or Q to quit):");
gets(send_data);

if ((strcmp(send_data , "q") == 0) || strcmp(send_data , "Q") == 0)
break;

else

// Converting entered string into hexadecimal using sprintf()
hexstring[0] = '\0';
temp[0] = '\0';

for (i=0; i<strlen(send_data); i++)
{
        sprintf(temp, "%X ", send_data);
        strcat(hexstring, temp);
}
sendto(sock,hexstring, strlen(hexstring), 0,(struct sockaddr *)&server_addr, sizeof(struct sockaddr));

- which won't work by the way, because this is equivalent to

Code:

if ((strcmp(send_data , "q") == 0) || strcmp(send_data , "Q") == 0)
break;

else
{
        // Converting entered string into hexadecimal using sprintf()
        hexstring[0] = '\0';
}
temp[0] = '\0';

for (i=0; i<strlen(send_data); i++)
{
        sprintf(temp, "%X ", send_data);
        strcat(hexstring, temp);
}
sendto(sock,hexstring, strlen(hexstring), 0,(struct sockaddr *)&server_addr, sizeof(struct sockaddr));

... to this:

Code:

printf("Type Something (q or Q to quit):");
gets(send_data);

if ((strcmp(send_data , "q") == 0) || strcmp(send_data , "Q") == 0)
break;

else
{
        sendto(sock,send_data, strlen(send_data), 0,(struct sockaddr *)&server_addr, sizeof(struct sockaddr));
}


hraja 8Apr2010 13:00

Re: Sending Hex values in UDP Packets using C
 
Thanks for your quick response and help.

But what I was hoping to achieve is to send hex values through the sendTo functions (or any C function). The above code is just a sample/trial to test sending data. I am sorry I had not explained it better. I hope this time it is clearer.

Originally, I have some hex values in an array that I hope to transmit to an electronic device, which reads only hex values. But when SendTo function is used to send this array of hex values, it treats all the hex values in the array as string and then converts these to hex values. Now, when these converted hex values reach the device, all the data is wrong because they were not the original hex values that I had hoped to send.For example, if I send 'FF' as hex in SendTo, 'FF' is treated as string and its hexadecimal values '4646' are sent to the device,instead of the 'FF' hex value. So my device gets the wrong values and performs the wrong operations.

I was wondering if there is any way of making a socket program send hex values as hex values itself without any further conversion, i.e., hexadecimal 'FF' is sent as 'FF' itself and not '4646'.

I thought the above program was simpler and easier to relate to in my case, as primarily it is sending data across the ethernet using C on linux,i.e., the user-input message is converted as hex values to emulate my SendTo function which needs to send hex values too. Once again, sorry for not being clearer.

Any help would be much appreciated.

xpi0t0s 8Apr2010 16:55

Re: Sending Hex values in UDP Packets using C
 
Well one of us doesn't understand something. The modification I made to the code would result in the value 0xff being sent as 0xff and not as 0x46 0x46. The unmodified program converts the hex values to a string with the following code:
Code:

for (i=0; i<strlen(send_data); i++)
{
        sprintf(temp, "%X ", send_data);
        strcat(hexstring, temp);
}

so if in your program you are doing this, then you need to stop doing that in order for the send to send what you want it to send.

Quote:

Originally, I have some hex values in an array...
OK, so where does it get converted from those hex values to string format? Find that code and remove it, and this will solve the problem.


All times are GMT +5.5. The time now is 02:48.