I need to send a packet via UDP socket in which there will be a six digit number written as ASCII string and after a new line character, same number but as int in bytes, like this: 31 32 33 34 35 36 0A 00 01 E2 40 which corresponds to 123456\n(123456) - integer is in brackets. So far I can send my string and my newline char, but when it comes to an int I get that 0 bytes were written into the buffer and nothing from my int buffer array is sent. Additionally, if I check text content of int buffer array it has weird amount of "Fs": 00 01 FFFFFFE2 40
This is my UDP client:
#include <stdio.h>
#include <string.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
char buf[11];
int main(int argc, char **argv)
{
struct sockaddr_in adr;
int sock, r;
unsigned int port;
char abcd[512];
char intBuff[4];
unsigned int index = 123456;
unsigned int networkHost;
networkHost = htonl(index);
memcpy(intBuff, (char*)&networkHost,sizeof(uint32_t));
printf("Int buffer: ");
for(int i=0; i<sizeof(uint32_t); i++){
printf("%02X ", intBuff[i]); //Output is 00 01 FFFFFFE2 40
}
int x1 = sprintf(buf,"%s\n","123456");
printf("\n1st data write: %i\n", x1); //7 bytes are written
int x2 = sprintf(&buf[x1], "%s",intBuff);
printf("2nd data write: %i\n", x2); //0 bytes are written
//SERVER
printf("IP: ");
scanf("%s", abcd);
printf("PORT: ");
scanf("%u", &port);
sock = socket(AF_INET, SOCK_DGRAM, 0);
adr.sin_family = AF_INET;
adr.sin_port = htons(port);
adr.sin_addr.s_addr = inet_addr(abcd);
r = sendto(sock,
buf,
11,
0,
(struct sockaddr*) &adr,
sizeof(adr));
close(sock);
return 0;
}