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.

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.