File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Sockets and Internet Protocols and the fly likes Change socket sender IP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Change socket sender IP" Watch "Change socket sender IP" New topic

Change socket sender IP

Jochem Gruter

Joined: Aug 16, 2011
Posts: 9
I've a VPS with 2 IP addresses (ip a and ip b). When I send packets with a socket, it sends allways with ip a. How can I change this so that it send with ip b?
Marco Ehrentreich
best scout

Joined: Mar 07, 2007
Posts: 1294

Hi Jochem,

the sender IP usually depends on the routing table(s) and network configuration of your OS. The network stack decides which interface to use for sending your packets based on the routing table and the IP address of this interface is automatically used as sender IP. This can be changed though. With Linux for example you can use an iptables postrouting rule to modify the sender IP just before the network packets leave your box. There are surely similar approaches for any modern OS.

I agree. Here's the link:
subject: Change socket sender IP
It's not a secret anymore!