views:

61

answers:

1

I have a service which listens for incoming connections on a TCP\IP port number say 7000. Also my machine is having more than 1 NIC cards and more than 1 IP address.( in other words i am having 2 LANs, LAN and LAN2 and 2 Ips).

Now I have configured my client application(in another machine with only 1 IP) to establish a connection to my server and i give the port number as 7000 and IP to which it must try connecting as IP1 of LAN of the server.

However I notice that the client is unable to make a connection, but when I disable LAN2 I notice that the client is able to make a connection with the server.

What could be wrong?

+3  A: 

when you bind the port you have to specify the ip or you can use INADDR_ANY for all interfaces

i.e.

memset(&myname, 0, sizeof(myname));
myname.sin_family      = AF_INET;
myname.sin_port        = 7000;
myname.sin_addr.s_addr = INADDR_ANY; /* all interfaces */
rc = bind(s, (struct sockaddr *) &myname, sizeof(myname));

from the MSDN

If an application does not care what local address is assigned, specify the constant value INADDR_ANY for an IPv4 local address or the constant value in6addr_any for an IPv6 local address in the sa_data member of the name parameter. This allows the underlying service provider to use any appropriate network address, potentially simplifying application programming in the presence of multihomed hosts (that is, hosts that have more than one network interface and address).

RageZ
thanks let me see if this helps.Currently what i am doing is thislocalIP = inet_ntoa (*(struct in_addr *)*localHost->h_addr_list);//To Set up the sockaddr structureServerSock.sin_family = AF_INET;ServerSock.sin_addr.s_addr = inet_addr(localIP);ServerSock.sin_port = htons(pLantronics->m_wRIPortNo)So in this case what IP would it take IP of LAN or LAN2
ckv
if you need the service to work on both interface you have to as per my example if you want only one interface you have to pass the ip of that interface.
RageZ
ok Thanks for the information
ckv