[sockets] What port

This is a discussion on [sockets] What port within the C Programming forums, part of the General Programming Boards category; So here's my problem: when I make a new socket, I want the OS to assign a port to it: ...

  1. #1
    return 0;
    Join Date
    Jan 2005
    Location
    Netherlands
    Posts
    89

    Question [sockets] What port

    So here's my problem: when I make a new socket, I want the OS to assign a port to it:

    Code:
    local.sin_port = htons(0);
    That works fine, but now I want to know what port the OS assigned to my socket... Is there a function or something to get this value? Because when I've binded the socket and do something like

    Code:
    // bind() and stuff here
    printf("Port: %d\n",local.sin_port)
    it just prints "Port: 0"

  2. #2
    Hamster without a wheel iain's Avatar
    Join Date
    Aug 2001
    Posts
    1,385
    Why does the OS need to assign it? You are actually assigning it yourself in the function call
    local.sin_port = htons(0)

    the port assigned will be zero

    local.sin_port = htons(122)
    the port will be 122

    htons is a function that converts host order to network short order which is why we pass it the the number.

    depending on what you are using the socket for and which OS you are compiling for, you may find it easier to set the type to inet

    sin.sin_family = AF_INET

    hope this helps
    Monday - what a way to spend a seventh of your life

  3. #3
    return 0;
    Join Date
    Jan 2005
    Location
    Netherlands
    Posts
    89
    Well I have a program that accepts multiple connections, and each one needs a different port. For that reason, I want the OS to assign a random port (if I do it myself, there's a chance the port is already in use and I'll get an error). When assigning port 0 to a socket, it means it will get a random port right? I only need to know what port it has...

  4. #4
    Hamster without a wheel iain's Avatar
    Join Date
    Aug 2001
    Posts
    1,385
    I would suggest using a table to maintain port information, look up the port lists on the internet first so you can avoid ranges in use. Eg: if you can use ports 1999 - 4999 then set a variable to this value and let this value be the first port you assign, assign the next one 2000 etc. Though this wouldnt reissue returned port numbers. I dont think there is a function to assign a random port number.

    You can check the return of the bind though so

    if (bind(socketDescriptor, &sin, sizeof(sin))== -1)
    {
    printf("The port is in use");
    }

    using the idea above you can work out which ports are in use

    hope this helps
    Monday - what a way to spend a seventh of your life

  5. #5
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    When assigning port 0 to a socket, it means it will get a random port right?
    Yes, that's correct and a better approach than trial and error.
    Now I want to know what port the OS assigned to my socket.
    Use the getsockname function to retrieve the local address for a socket.
    Code:
    #include <winsock2.h>
    #include <windows.h>
    #include <stdio.h>
    #pragma comment(lib, "ws2_32.lib")
    
    int main(void)
    {
        WSADATA            wsad;
        SOCKET             sock;
        struct sockaddr_in addr;
        struct sockaddr_in actual_address;
        int                addr_size = sizeof(actual_address);
    
        WSAStartup(MAKEWORD(2, 2), &wsad);
    
        sock = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
        
        addr.sin_family           = AF_INET;
        addr.sin_port             = htons(0);
        addr.sin_addr.S_un.S_addr = INADDR_ANY;
    
        bind(sock, (struct sockaddr*) &addr, sizeof(addr));
        listen(sock, SOMAXCONN);
    
        getsockname(sock, (struct sockaddr*) &actual_address, &addr_size);
        printf("The selected port is %d.\n", ntohs(actual_address.sin_port));
    
        getchar();
        return 0;
    }
    Of course, the typical server accepts multiple connections on the one listening port, so I assume you have a good reason for using a multiple port solution. FTP uses a multiple port solution and it causes a lot of problems. With this model, you will have to inform the client of which port to use.
    Last edited by anonytmouse; 01-30-2005 at 09:12 AM.

  6. #6
    return 0;
    Join Date
    Jan 2005
    Location
    Netherlands
    Posts
    89
    Thanks! It works perfectly

    Quote Originally Posted by anonytmouse
    I assume you have a good reason for using a multiple port solution.
    I'm trying to program a SOCKS V5 proxy... So I need different ports and I have to inform the client to which port they need to send their data.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. FTP program
    By jakemott in forum Linux Programming
    Replies: 14
    Last Post: 10-06-2008, 02:58 PM
  2. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 06:49 PM
  3. Segmentation Fault - Trying to access parallel port
    By tvsinesperanto in forum C Programming
    Replies: 3
    Last Post: 05-24-2006, 04:28 AM
  4. Basic port scanner code .. pls help ???
    By intruder in forum C Programming
    Replies: 18
    Last Post: 03-13-2003, 08:47 AM
  5. DOS, Serial, and Touch Screen
    By jon_nc17 in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 01-08-2003, 04:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21