Re: How to listen to more a specific IP and specific ports



You need 4 sockets bound to each combination of IP address
and port (2 IP addresses times 2 ports amounts to 4 sockets
necessary). If the computer only has these two IP addresses
and you don't mind listening to the loopback address, you
can use two sockets with INADDR_ANY and the two specific
port numbers.

--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD
email: agnickolov@xxxxxxxx
MVP VC FAQ: http://www.mvps.org/vcfaq
=====================================

"Bassam" <Bassam@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:77BD5E60-9D57-484E-830E-E5456812B472@xxxxxxxxxxxxxxxx
Dear all,
I am writing a network application that requires me to bind it to
specific network IP addresses and specific ports.

The normal binding resides in filling up the SOCKADDR_IN structure with
any
IP "INADDR_ANY" or a particular IP "192.168.1.148" and a single port as
follows:

SOCKADDR_IN tcpaddr;
int port = 1812;

tcpaddr.sin_family = AF_INET;
tcpaddr.sin_port = htons(port); //Single port
tcpaddr.sin_addr.s_addr = htonl(INADDR_ANY); //Any IP
or
tcpaddr.sin_addr.s_addr = htonl("192.168.1.148"); //A particular IP

Also this can be achieved by filling up the ADDRINFO structure of the
hints
parameter of the address resolution function "getaddrinfo()" as follows:

ADDRINFO hints;
LPADDRINFO resultPtr = NULL;

memset(&hints, 0, sizeof(hints));
hints.ai_flags = AI_PASSIVE; //to determin any IP
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;

getaddrinfo(NULL, "1812", &hints, &resultPtr);

The first argument of "getaddrinfo()" is NULL to determin that we are
willing to bind to any IP, and the second argument is a single port.

NOW: my question is how to listen to traffic coming through particular IPs
(i.e., "192.168.1.148" and "192.168.10.30") on the server machin and
particular ports 1812 and 1813 at the same time?

Do I need to bind my single socket handler to different SOCKADDR_IN
structures each has its own IP address and port no.. However, I find this
way
is not feasible because the particular IPs are not fixed. I mean sometime
I
need to listen to one or two or more particular IPs.

I would appreciate if you shed light on this matter.

Regards
Bassam


.



Relevant Pages

  • Re: How to listen to more a specific IP and specific ports
    ... bind my socket to specific unlimited and selective IP addresses. ... port numbers. ... is not feasible because the particular IPs are not fixed. ...
    (microsoft.public.win32.programmer.networks)
  • Re: network programming: how does s.accept() work?
    ... The program you contact at Google is a server. ... so, the server will usually assign a new port, say 56399, specifically ... connections to a server remain on the same port, ... sockets is what identifies them. ...
    (comp.lang.python)
  • Re: Sockets: connection refused
    ... data from 3 equipments using sockets. ... Several hours later, when restarting the application, I get a ... check to make sure that the program that listens on that port is ... make sure the application will not start up if it fails to bind ...
    (comp.unix.programmer)
  • Re: Binding multiple udp sockets to same local port???
    ... Is it possible to bind multiple udp sockets to same local port but each ... By bind means calling bind() function and by connect means calling ...
    (microsoft.public.win32.programmer.networks)
  • Re: Listening and Establish TCP connections on/from the same IP end po
    ... and accept client connections as well as be able to establish connections to ... The application needs to listen on a specific port because that's where the ... But it does not allow both sockets to actually _use_ that IP/port at the same time, nor would there be any reliable way to let that happen. ... Client ports are pretty much _never_ assigned according to protocol, and firewalls don't block inbound traffic based on the sender's port at all. ...
    (microsoft.public.dotnet.framework)