Search code examples
windowssocketsudploopback-address

Can't Send data to loopback address from loopback address


Is it possible to send data from 127.0.0.1:7000 to 127.0.0.1:8000 ? I am getting socket error 10049 which is invalid address.

sockaddr_in sin;
memset((char*)&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr = 16777343; //127.0.0.1
sin.sin_port = 16415;//8000
int _ret = ::sendto(sock, Buff, Len, 0, (sockaddr*)&sin, sizeof(sin));

Solution

  • sin.sin_addr = 16777343; //127.0.0.1
    

    I think you might want to check that.

    Here 127×255×255×255 + 1 is 2105834626.

    Use the right functions to perform IP address decoding, don't do it yourself: there are too many edge cases (eg. signed/unsigned) to get caught on.