I'm running a server an a client on the sam开发者_运维知识库e machine(linux). How do I force the packets to go through the network(switch) and not through the loopback?
Thanks,
Michael
Since you're asking this on a programming site, I'll assume you have source code.
When you create the client-side socket, you can limit it to a specific interface. Usually you don't (you just call connect()
without bind()
ing it first) , and let the OS figure out the best outgoing interface, but this is not mandatory.
You can try setting the SO_BINDTODEVICE socket option on both the client and the server sockets and give it the external NIC interface as parameter.
See: http://codingrelic.geekhold.com/2009/10/code-snippet-sobindtodevice.html for an example
I am not sure this is enough - there might be a sanity check in the kernel IP stack to drop packets whose Ethernet destination and source are both you. There might be a sysctl to disable this check or you can compile your own kernel without the check for this specific test.
Maybe you should try connecting via a proxy server?
You can't, unless you have some device out there on a network whose job it is to send the data back to you. Normally, there is nothing that would do that. If you send the data out onto the network, you won't get it back.
If you have set something up to return the data to you, send the data to that, following whatever mechanism it supports.
精彩评论