Winsock Port Listener - c++

I've done a TCP port listener. it works when i connect to it locally using client with 127.1.1.0 and a port that listener is bound to accept connection. can you make it to listen to any IP that connects through that port and would it be possible to listen to a second client port that is accepting from server?

You need to specify INADDR_ANY to bind. You cannot bind the same socket to multiple ports, but you can certainly create multiple sockets, each listening to a different port.

Related

Access Virtualbox guest from computer that is NOT host

I have a computer, let's call it linux#home.
I have a dedicated distant linux server, let's call it linuxHost#datacenter.
I have a virtualbox virtual machine running windows 7 on the dedicated server, let's call it windowsGuest#datacenter.
I set up a NAT network interface on windowsGuest through virtualbox manager.
windowsGuest#datacenter is running a service that needs to access an sql database on linuxHost#datacenter, and that works fine. The same service on windowsHost needs to listen to connections on an udp port, let's say 12345. windowsGuest can access internet, web browsing works properly (doesn't need to, but that works fine).
So, now i need linux#home tu run a client application that connects on udp port 12345 on windowsGuest#datacenter. What i did is configure a port forward on the NAT network i use for windowsGuest :
-host ip : 127.0.0.1
-host port : 12346
-protocol : UDP
-guest ip : 10.0.2.4 (which is windowsGuest#datacenter ip address)
-guest port : 12345
I expect that any connection on linuxHost#datacenter on UDP port 12346 would be forwarded to windowsGuest#datacenter UDP port 12345, but that doesn't seem to happen.
a few more information :
- windowsGuest#datacenter's firewall is turned OFF
- linuxHost#datacenter runs iptables and is configured to allow any connection from linux#home, both tcp and udp.
- linuxHost#datacenter 's iptables doesn't log any blocked packets.
- I don't want to turn off iptables since linuxHost#datacenter is running other services and i don't want to expose that.
- I cannot test the client application on linuxHost#datacenter
What did I do wrong? Should i manually forward connections from internet to linuxHost#datacenter on port udp 12346 to 127.0.0.1:12345? should i configure the NAT port forwarding differently? Am i totally wrong and that's not the way i should manage this situation?
Thanks to this answer i could manage what i wanted. I had to modify my NAT port forward in the virtualbox manager preferences. The host ip field can be left blank, so that connections from any ip address would be forwarded to the guest.

Port and IP address usage in UDP communication

I have two clients and I want to connect them to daemon which is using UDP socket. It is using IP addr1 and Port#1 for communication. In each client, I create a socket with the same IP and port#1 that daemon is using. Is that okay to use same IP address and port# for both clients?
That depends on what you mean by "use."
It is correct for both clients to do connect() or sendto() using the IP and port the daemon is listening on.
It would not be ok for any of the clients to do bind() using the IP and port the daemon is listening on.

How to connect a server and a client that is behind router using QUdpSocket?

Assume that I have a server directly connected to internet and a client connected to a router. The client can send messages to server since it knows the server's IP address and port. But how does the server send message back to the client? It can get the client's external IP address and port but should it send message to that port? and how does the client listen to the message,which port should it listen to? assuming no port forwarding.
I assume the difficulty is that your client is behind a NAT firewall. If that's the case, then the answer is simple: you don't. That's the entire point of NAT.
But this also assumes that you're talking about the creation of a new socket from the server to the client. If the socket already exists, you just talk over it and the stateful NAT router keeps track of the things it needs to in order to get the message through.
If neither of these answers your question, you'll need to reformulate it.

Socket listening on an IP subnet in C / Unix

I'm trying to write a server-client socket program in C. The objective is for the server to listen on a specific port, but across a range of IP addresses belonging to the same IP subnet. This IP subnet is part of the 127.x.x.x range (not 127.0.0.1 of course).
Couple of points to note:
This is a stream-based socket, and not Datagram sockets.
This is not a broadcast address.
Implementation in C/C++ only on Unix/Linux platform
I do not want to open multiple sockets on the server for each IP address in the range. This is not scalable.
Any help would be ideally appreciated. Is this even feasible?
You can only bind to one address on a single socket. Why can't you bind to INADDR_ANY and simply reject any packets not bound for your target IPs? Alternatively, you could bind to an arbitrary port and use OS-level magic (e.g. iptables, bpf) to reroute packets destined for those IP/port combinations to your socket.
The socket API does not allow binding to a subnet -- you can bind to one IP or to any IP. You can listen for all inbound connections and reject those that don't apply. If you need to divvy connections out between processes on the same server, use a single listening socket, and transfer incoming connections to the worker processes.
You can use a firewall to prevent anyone from outside the desired subnet from connecting (that's at the o/s level). You can put the socket in promiscuous mode and accept all connections on a given interface. I don't know if you can do both (have a socket in promiscuous mode and run iptables on it). Essentially it's like building a packet sniffer that only listens on one port.

receiving datagrams sent by client over internet

I made two console app: Broadcasting listener and UDP writer (for practice only). Each run on different machine over the internet.
Broadcasting listener:
INADDR_ANY, port 5555
Udp writer:
Enabled Broadcasting (setsockopt, SO_BROADCAST)
Case:
The writer send some datagrams to listener server (ip: 113.169.123.138). Listener can receive those datagrams.
The writer broadcasting to 255.255.255.255. Listener can not receive anythings.
Question:
What i need to do to make case 2 work?
Your broadcasts are meant for your subnet and not the internet.
For example DHCP -- this application is meant to perform broadcasts to assign IP addresses to machines logically part of a particular subnet.
If you join the reader machines subnet via a VPN, then the reader machine will be able to receive your broadcast.