LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How can get local and remote IP address from TCP connect? (https://www.linuxquestions.org/questions/programming-9/how-can-get-local-and-remote-ip-address-from-tcp-connect-753274/)

yhus 09-07-2009 06:11 AM

How can get local and remote IP address from TCP connect?
 
Hi,

Calling TCP connect from client side resuting four elements of the association 5-tuple to assigned, local-addr, local-process, foreign-addr, foreign-process. Is there any way to get the local and foreign IP address from the system after calling TCP connect in a C++ program?

Thank you.

Kind Regards,

Jupiter

estabroo 09-07-2009 08:45 AM

dunno if c++ has native ones, but you could always extern 'C' and use getpeername and getsockname

paulsm4 09-07-2009 01:34 PM

You should also take a look at Beej's Guide. For example:

http://beej.us/guide/bgnet/output/ht...t.html#connect

Code:

    while(1) {  // main accept() loop
        sin_size = sizeof their_addr;
        new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
        if (new_fd == -1) {
            perror("accept");
            continue;
        }

        inet_ntop(their_addr.ss_family,
            get_in_addr((struct sockaddr *)&their_addr),
            s, sizeof s);

        printf("server: got connection from %s\n", s);
        ...



All times are GMT -5. The time now is 09:08 AM.