Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. INADDR_ANY with second socket?

INADDR_ANY with second socket?

Scheduled Pinned Locked Moved C / C++ / MFC
question
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dave_
    wrote on last edited by
    #1

    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

    M M 2 Replies Last reply
    0
    • D 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

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      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.

      D 1 Reply Last reply
      0
      • M Mark Salsbery

        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.

        D Offline
        D Offline
        Dave_
        wrote on last edited by
        #3

        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)?

        M 1 Reply Last reply
        0
        • D Dave_

          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)?

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • D 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

            M Offline
            M Offline
            malaugh
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups