Thread: Choose a particular source port when connecting.

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    21

    Choose a particular source port when connecting.

    Hello!

    The following code does work (the most minimalistic version chosen for clarity) as I connect using port 2100, not a random one:

    Code:
    #include <arpa/inet.h>
    
    int main(void) {
    
            int sock;
            struct sockaddr_in myaddr, remaddr;
            socklen_t addrlen;
    
            sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    
            remaddr.sin_family = AF_INET;
            remaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
            remaddr.sin_port = htons(20000);
    
            myaddr.sin_family = AF_INET;
            myaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
            myaddr.sin_port = htons(21000);
    
            bind(sock, (struct sockaddr *) &myaddr, sizeof(struct sockaddr_in));
    
            addrlen = sizeof(myaddr);
    
            connect(sock, (struct sockaddr *) &remaddr, sizeof(remaddr));
    
            return 0;
    }
    BUT it doesn't use getaddrinfo() which I assume is the preferred way nowadays. The following code:

    Code:
    #include <unistd.h>
    #include <string.h>
    #include <netdb.h>
    
    int main(void) {
    
            int sfd, addr;
            struct addrinfo hints, *servinfo, *sp;
    
            memset(&hints, 0, sizeof(struct addrinfo));
    
            hints.ai_family = AF_INET;
            hints.ai_socktype = SOCK_STREAM;
            hints.ai_protocol = IPPROTO_TCP;
    
            addr = getaddrinfo("127.0.0.1", "20000", &hints, &servinfo);
            for(sp = servinfo; sp != NULL; sp = sp->ai_next) {
                    if ((sfd = socket(sp->ai_family, sp->ai_socktype, sp->ai_protocol)) == -1) {
                            continue;
                    }
                    if ((connect(sfd, sp->ai_addr, sp->ai_addrlen) == -1)) {
                            continue;
                    }
                    break;
            }
            freeaddrinfo(servinfo);
    
            return 0;
    }
    works but it doesn't bind() first to force the code to use the source port of my choosing and not the random one.

    Question: Where does bind() fit in in the above implementation? Can it be done at all with getaddrinfo()?

    Cheers.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    21
    Quote Originally Posted by Salem View Post
    First of all, 1337 answer on your part. There's ZERO information there about changing your source port when connecting to a server. Read it yourself.

    Secondly, the answer is:

    Code:
    #include <string.h>
    #include <netdb.h>
    
    void binding(int sock);
    
    int main(void) {
    
            int sfd, addr;
            struct addrinfo hints, *servinfo, *sp;
    
            memset(&hints, 0, sizeof(struct addrinfo));
    
            hints.ai_family = AF_INET;
            hints.ai_socktype = SOCK_STREAM;
            hints.ai_protocol = IPPROTO_TCP;
    
            addr = getaddrinfo("127.0.0.1", "20000", &hints, &servinfo);
            for(sp = servinfo; sp != NULL; sp = sp->ai_next) {
                    if ((sfd = socket(sp->ai_family, sp->ai_socktype, sp->ai_protocol)) == -1) {
                            continue;
                    }
                    binding(sfd);
                    if ((connect(sfd, sp->ai_addr, sp->ai_addrlen)) == -1) {
                            continue;
                    }
                    break;
            }
            freeaddrinfo(servinfo);
    
            return 0;
    }
    
    void binding(int sock) {
    
            int sfd, addr;
            struct addrinfo hints, *servinfo, *sp;
    
            memset(&hints, 0, sizeof(struct addrinfo));
    
            hints.ai_family = AF_INET;
            hints.ai_socktype = SOCK_STREAM;
            hints.ai_protocol = IPPROTO_TCP;
    
            addr = getaddrinfo("127.0.0.1", "30000", &hints, &servinfo);
            for(sp = servinfo; sp != NULL; sp = sp->ai_next) {
                    if (bind(sock, sp->ai_addr, sp->ai_addrlen) == -1) {
                            continue;
                    }
                    break;
            }
    }
    Thanks for nothing.

    Cheers.
    Last edited by assiduus; 12-19-2018 at 03:30 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    Well for such an ungrateful git, it's a good job I didn't spend any more effort on it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Connecting and dealing with source files
    By alanb in forum C Programming
    Replies: 19
    Last Post: 08-28-2009, 11:12 AM
  2. connecting to server with wrong port
    By liri in forum Networking/Device Communication
    Replies: 3
    Last Post: 11-13-2007, 03:52 AM
  3. Port formwarding: connecting with the router config page
    By Niara in forum Networking/Device Communication
    Replies: 3
    Last Post: 12-28-2006, 05:22 AM
  4. Get tcp source port?
    By hacinn in forum C Programming
    Replies: 4
    Last Post: 07-24-2005, 10:59 AM
  5. Connecting to COM port
    By Unimatrix139 in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 07-25-2002, 11:00 AM

Tags for this Thread