使用和UDP套接字发送时如何设置源地址 [英] How to set source address when sending using and UDP socket
问题描述
我有两台电脑使用 VRRP 进行冗余.因此,每台 PC (Linux) 都有一个物理 IP 地址和一个虚拟 IP 地址.
I've two pc using VRRP for redundancy. So every PC (Linux) has a physical and a Virtual IP address.
我有一个带有 UDP 协议的客户端/服务器架构的软件 (C++).软件将监听套接字绑定在0.0.0.0"上,每次需要向对方发送一些数据时使用一个新的套接字.使用wireshark,我看到当它发送数据时,源IP是物理IP...如何将发送套接字的源地址设置为虚拟的??
I've a software (C++) with a client/server architecture with UDP protocol. The software bind the listen socket on "0.0.0.0" and use a new socket every time it needs to send some data to the other party. With wireshark I saw that when it sends data the source IP is the phisycal one... How can I set the source address of the sending socket to the Virtual one??
注意:使用 ifconfig 我只看到带有物理地址的 eth0...
NOTE: Whit ifconfig I see only eth0 with the physical address...
推荐答案
当内核需要通过套接字发送一些东西时,它会执行这些步骤
When the kernel needs to send something through a socket it performs these steps
- 如果套接字已绑定,则使用该源地址
- 是socket没有绑定,它四处寻找接口并选择一个源地址
所以你需要bind(2)
你的套接字到你想要的地址.更多信息:"源地址选择"在IP 路由"一章中《Linux IP 层网络管理指南》.
So you need to bind(2)
your socket to your desired address. For more information: "Source Address Selection" in chapter "IP Routing" of "Guide to IP Layer Network Administration with Linux".
这篇关于使用和UDP套接字发送时如何设置源地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!