INADDR_ANY with second socket?
-
Does anyone know what happens when you have a socket bound to INADDR_ANY and then have a second socket (both TCP or both UDP sockets) bound to a specific address? I am trying to add something to an existing program that has a TCP socket bound to INADDR_ANY. I need to add a TCP socket that listens to 127.5.0.1 (within the same executable). Will messages sent to 127.5.0.1 be handled by my new socket or will everything go through the existing socket bound to INADDR_ANY? I guess I can check the address on the existing socket and handle the messages based on that but I'd still like to understand how this works. Thanks, Dave
-
Does anyone know what happens when you have a socket bound to INADDR_ANY and then have a second socket (both TCP or both UDP sockets) bound to a specific address? I am trying to add something to an existing program that has a TCP socket bound to INADDR_ANY. I need to add a TCP socket that listens to 127.5.0.1 (within the same executable). Will messages sent to 127.5.0.1 be handled by my new socket or will everything go through the existing socket bound to INADDR_ANY? I guess I can check the address on the existing socket and handle the messages based on that but I'd still like to understand how this works. Thanks, Dave
INADDR_ANY indicates that the caller to bind() doesn't care what address the socket is bound to. Typically this is used on client ends of TCP connections to let the system select an unused port. If you want to listen on a specific address/port then you have to bind to that specific address.
Dave_ wrote:
I need to add a TCP socket that listens to
What do you mean by "listens to"? This is very different on TCP and UDP.
-
INADDR_ANY indicates that the caller to bind() doesn't care what address the socket is bound to. Typically this is used on client ends of TCP connections to let the system select an unused port. If you want to listen on a specific address/port then you have to bind to that specific address.
Dave_ wrote:
I need to add a TCP socket that listens to
What do you mean by "listens to"? This is very different on TCP and UDP.
When I use the listen() call, which socket (or will it be both?) will get the attempt to connect from the client? If I try to recv(), will the socket that is bound to the specific address get the data or will there be a race condition (first socket to read gets the data)?
-
When I use the listen() call, which socket (or will it be both?) will get the attempt to connect from the client? If I try to recv(), will the socket that is bound to the specific address get the data or will there be a race condition (first socket to read gets the data)?
A socket address consists of an address and a port. listen() would be used on a connection-oriented protocol (like TCP) socket. You need to create a socket to listen on and bind it to a specific address to listen on. Once a socket is placed in the listen state you don't recv data on it. You receive connection attempts on it. Accepting a connection creates a new, connected socket which will be used for send/recv of data. For UDP, which is connectionless, you'd create a socket and bind it to an address. Then you can either use connect to set a default destination address on it and/or use sendto/recvfrom to send and receive datagrams to/from multiple destination addresses. Only one socket per protocol can be bound to a specific address so there's never a race condition.
-
Does anyone know what happens when you have a socket bound to INADDR_ANY and then have a second socket (both TCP or both UDP sockets) bound to a specific address? I am trying to add something to an existing program that has a TCP socket bound to INADDR_ANY. I need to add a TCP socket that listens to 127.5.0.1 (within the same executable). Will messages sent to 127.5.0.1 be handled by my new socket or will everything go through the existing socket bound to INADDR_ANY? I guess I can check the address on the existing socket and handle the messages based on that but I'd still like to understand how this works. Thanks, Dave
Not sure what your trying to do. Normally you use INADDR_ANY to bind the socket to the IP address of your PC, which normally only has one IP address. If you want multiple server sockets, you normally use INADDR_ANY for the IP and a different address for the port of each server. For example Server 1 ServerAddress.sin_family = AF_INET; ServerAddress.sin_addr.s_addr = INADDR_ANY; ServerAddress.sin_port = htons((u_short)27015); if(bind(ServerSocket, (struct sockaddr*)&ServerAddress, sizeof(ServerAddress)) != 0) ......... Server 2 ServerAddress.sin_family = AF_INET; ServerAddress.sin_addr.s_addr = INADDR_ANY; ServerAddress.sin_port = htons((u_short)27016); if(bind(ServerSocket, (struct sockaddr*)&ServerAddress, sizeof(ServerAddress)) != 0) .......... Then the clients will send to port 27015 for server 1 and port 27016 for server 2. You can definitely substitute service.sin_addr.s_addr = inet_addr("127.5.0.1"); if you want to specify and exact IP address