Sockets with fixed IP address - Programmer

This is a discussion on Sockets with fixed IP address - Programmer ; I am developing and testing a dedicated LAN composed of just two Windows computers with Ethernet adapters. The application will deal with UDP only. No TCP. For various reasons, I would like to bypass the DNS stuff and use a ...

+ Reply to Thread
Results 1 to 2 of 2

Thread: Sockets with fixed IP address

  1. Sockets with fixed IP address

    I am developing and testing a dedicated LAN composed of just two
    Windows computers with Ethernet adapters. The application will deal
    with UDP only. No TCP. For various reasons, I would like to bypass
    the DNS stuff and use a statically defined IP address in the "server"
    side of the UDP connection. (The "server" will eventually be replaced
    with an embedded Ethernet device with limited resources.)

    The trouble is, I seem unable to use the Windows Sockets API to force
    a fixed IP address (10.10.6.100) in the "server". Evidence is that
    after calling bind and then gethostname, the sockaddr_in structure
    that receives the bound IP address is always 0.0.0.0. I have tried
    configuring my TCP/IP from the control panel to uncheck "determine IP
    address automatically" and set 10.10.6.100 as the fixed address, but
    still no luck. The socket functions all return with no errors.

    I also tried using a fixed socket address in the socket() function
    call (instead of INADDR_ANY), but then socket() returns an error
    saying that the specified IP address is not valid for my computer.
    Does anybody have an idea of what I am doing wrong? Here is the code
    with the error-checking removed for simplicity:

    struct sockaddr_in local,gaddr;
    int gaddrlen = sizeof(gaddr);
    WSAStartup(0x202,&wsaData);
    local.sin_family = AF_INET;
    local.sin_addr.s_addr = INADDR_ANY;
    local.sin_port = htons(853);
    listen_socket = socket(AF_INET, SOCK_DGRAM, 0);
    bind( s, (struct sockaddr*)&local, sizeof(local));
    getsockname( s, (sockaddr*)(&gaddr), &gaddrlen);
    printf("Assigned IP address = %08x\n", gaddr.sin_addr.s_addr);




    -Robert Scott
    Ypsilanti, Michigan
    (Reply through newsgroups, not by direct e-mail, as automatic reply address is fake.)


  2. Re: Sockets with fixed IP address

    In article <3f948c69.6249968@news.wwnet.net>, no-one@nowhere-q7x.com (Robert Scott) wrote:
    > local.sin_addr.s_addr = INADDR_ANY;
    > bind( s, (struct sockaddr*)&local, sizeof(local));
    > getsockname( s, (sockaddr*)(&gaddr), &gaddrlen);
    > printf("Assigned IP address = %08x\n", gaddr.sin_addr.s_addr);


    Yes. You told it to bind to address INADDR_ANY, so it's bound to address
    INADDR_ANY. This is very different from the case of binding to _port_ 0,
    which is asking the OS to give you a new port; in binding to an address of
    0, you're telling the OS that this is a listening socket that will accept
    packets that come in to _any_ address that this system owns.

    In your system, for instance, you have at least two interfaces - the
    loopback interface at 127.0.0.1, and the fixed IP address of 10.10.6.100.
    If you were to listen to incoming connections on address 127.0.0.1, you
    could not connect to that server at address 10.10.6.100, because the socket
    is listening at 127.0.0.1, not 10.10.6.100. But if your socket is listening
    at address 0 (= INADDR_ANY = 0.0.0.0), you will be able to connect to that
    socket at either of 127.0.0.1 or 10.10.6.100.

    Binding to address 0 does not mean "pick a random address for me".

    Alun.
    ~~~~

    [Please don't email posters, if a Usenet response is appropriate.]
    --
    Texas Imperial Software | Find us at http://www.wftpd.com or email
    1602 Harvest Moon Place | alun@texis.com.
    Cedar Park TX 78613-1419 | WFTPD, WFTPD Pro are Windows FTP servers.
    Fax/Voice +1(512)258-9858 | Try our NEW client software, WFTPD Explorer.

+ Reply to Thread