I have a Java client and a C server that communicate over a unix socket.
I want to send an ip address from the client to the server. On the server side when i try to interprete the bytes i receive from the client i get [ 01 01 01 01 ]
which is correct, but then when i print the ip_address to video i get 0.0.0.0
. What am i doing wrong? I think i'm casting the address in a wrong manner somehow. I need to implement IPv4-Mapped IPv6 Addresses that's why im using sockaddr_storage
which is the most general case.
Here is my some code extracts:
Java client does:
OutputStream out;
byte[] ipAddress = Inet6Address.getByName("1.1.1.1").getAddress();
out.write(ipAddress);
C server does:
struct sockaddr_storage * ip_address;
n = read(rcv_sock, msg, PSIZE);
printf("I recevied n bytes: %d\n", n);// i get 4 bytes
print_bytes(msg, n);
ip_address = (union sockaddr_storage *)msg;
char str[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &(ip_address->__ss_padding), str, INET_ADDRSTRLEN);
printf("ip_address = %s\n", str); //i get 0.0.0.0
In particular the server expects a message containing a 28 byte long header + a socket address structure. I've managed to decode the header properly but not the socket address structure.