perform connect() on specific network adapter - network-programming

I'm programming with Winsock2. I have two network adapters in system, one for local connections (LAN), one for outer connections (PPPOE). When I perform a connect() call to connect to local address, it uses PPPOE adapter instead of LAN. I know I could tweak this using metrics, but can't I just use some hard-code to forcibly use LAN adapter? Thanks in advance!

Before you connect(), you need to bind() to the IP address of the LAN adapter. Just specify the IP address, and leave the port open (i.e. 0), so that the system can still chose one.

Related

Dart check if an IP is outside your local network IPv4 or IPv6

I can't figure out how to know if an IP, IPv4 or either IPv6, is in my local network.
I'm coding a program LAN and Internet based and I want to prevent it to call some endpoints from outside the network.
So I thought that before do the call just check if the address is local or not.
Thanks.

How to connect using specific network adapter?

We are using Delphi Berlin 10.2
We have an application that runs on devices with 2 or more ethernet adapters. It's mandatory for us to choose a specific network adapter.
I researched and failed to find a way to do this using Indy, Synapse, or even Winsock.
The connect methods don't offer this option, they offer only destination IP and port.
Is there a way to do this?
You need to bind() the client socket to the local IP address of the desired network interface, then connect() will reach out to the server's IP:port using the network that the interface is attached to.
Indy has properties for that purpose, for instance the BoundIP property of TIdTCPClient.
I'm sure Synapse (and most other networking libraries) has its own equivalent for this, too.

Can I broadcast small data between ESP8266s without a need to connect to any network?

I want to send small (a few bytes) data from one ESP8266 to another over long distances.
I believe the signal is too weak to connect one ESP8266 to another but maybe they can receive small messages with some delay?
How can I achieve that using arduino-esp8266 library (or whatever)?
If ESP8266 boards can not connect directly to each other because of the weak signal (approximately more than 50m between them) you can still connect them to each other (indirectly) if they are connected to the same WiFi network created by external router or internet. But you can connect them to each other without need to connect to any external network only if they are in range of each other.
It both of them are connected to same local WiFi network router you can communicate between them as if they are connected directly to each other. Just in this case router option "WiFi Client isolation" must be disabled in router settings page for boards to see each other(in case that router has this or similar named option).
Otherwise if ESP boards are connected to totally different networks, but both of these networks have access to internet, you can still connect them to each other but then you must use port forwarding on each router, so both of the ESP8266 boards are visible form internet. Google a bit about it, it is not so complicated. In this case you can have boards on any two locations in the world and make them communicate to each other as long as they are accessible from internet.
Maybe it is hard to test it when your code do not work out of the box, so I would recommend that you use some TCP or UDP terminal to test connectivity between two WiFi clients on different or same network. There are buch of such terminals available online.
For example for sending and receiving UDP packets same way as you would send/receive data from serial port, I use Docklight Scripting.
https://docklight.de/downloads/
I actually use it also for other Serial port communication so no big difference there. Don't let description text on their page fool you. It can also send/receive TCP/UDP data :)
Just when you create new project go to tools->project settings and type IP and port of another device you want to connect with (this can be another PC running docklight scripting or your ESP board). For example in my case I type in
Send/Receive Comm.: UDP:192.168.0.154:9761
Because that is the IP of my ESP board on local WiFi network and press "play button"or F5. Now you can send receive data to your board using UDP in same way that you communicate to basic serial port and in such way test basic connectivity. Or even use two laptops just to confirm connectivity between two WiFi clients on network.
And third hardest option must most versatile is needed if you do not have access to routers setup page (they are password protected) and cannot set port forwarding, then you must create server application on some server that is visible from internet and has public IP. So both of your ESP8266 boards are connected to this server as clients (for client port forwarding do not need to be set but in this case you must have access to internet by both clients) and server can forward their messages to each other. But this is way out of this topics scope. Maybe for start you use TCP/UDP terminal just to check connectivity between two WiFi clients and see if ESP8266 can communicate directly or not. Docklight scripting is just suggestion because it is my personal choice for TCP/UDP/RS232/RS485/MODBUS/USB HID, but you can use other software for this.
I hope this helps a bit about ways to connect two ESP8266 boards indirectly.

How/where is port-forwarding needed to establish a server-client connection

I am attempting to establish a connection between two computers using MSDN codes:
Client code:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms737591(v=vs.85).aspx
Server code:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms737593(v=vs.85).aspx
These codes run perfectly on a local network, but my intention is to establish a connection over the internet, where both computers are behind routers. I know port-forwarding is a viable solution. But I just don't know how.
For example, does even the client need port-forwarding, not just the server?
Further more, I am unable to properly configure my router to port-forward. Its model is Level One WBR-3407a. I tried this guide:
http://portforward.com/english/routers/port_forwarding/LevelOne/WBR-3407a/defaultguide.htm
But it didn't work. As you may note, this guide doesn't anywhere in it specify what port to use? is it possible to port-forward without specifying which port to forward?!
You need to configure port forwarding on the router to which the "server" is connected, and yes, you do need to specify a port (there's an exception, but let's not worry about it now). You typically wouldn't need port forwarding on the router to which the client computer connects.
Looking at the MSDN code you linked, you need to port forward port 27015, so on the router for your server, you want to add port forwarding entries for incoming TCP connections on port 27015 and to forward them to the IP address of the computer running the server, also on port 27015.
For future reference, this question is more appropriate for SuperUser or, less likely, for ServerFault.

bind and ip aliasing

Ip Aliasing allows us to associate more than one ip address to the same interface. My question is how can one bind and hence receive the data from all of the addresses associated to an interface? Is there some option like INADDR_ANY available for the purpose?
Always remember that bind(2) does not bind to interfaces, but addresses.
Each socket generally only stores exactly one binding, even if that is 0.0.0.0 port 0. It is just that 0.0.0.0, :: and port 0 are taken as wildcards when checking incoming requests. As such, you will need one socket for non-wildcard addresses.
See setsockopt SO_BINDTODEVICE if you really need interface binding, but that is in general not the right thing to do in programs other than e.g. tcpdump, as it will inhibit contacting over different interfaces even if they allow reception. For example, if your own host has 192.168.0.1 as an address on a private LAN, binding to eth1 will make it impossible to connect to 192.168.0.1 from .0.1 itself over lo. Hence, device binding is usually undesired.
Try binding to the interface, using setsockopt and SO_BINDTODEVICE.

Resources