LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-07-2009, 06:11 AM   #1
yhus
Member
 
Registered: May 2004
Posts: 75

Rep: Reputation: 15
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

Last edited by yhus; 09-07-2009 at 06:13 AM.
 
Old 09-07-2009, 08:45 AM   #2
estabroo
Senior Member
 
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,126
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
dunno if c++ has native ones, but you could always extern 'C' and use getpeername and getsockname
 
Old 09-07-2009, 01:34 PM   #3
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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);
        ...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Dovecot imap will not connect from remote machine but will connect local charlesnorman Linux - Server 4 03-28-2019 11:20 AM
i want to connect mysql server from remote through TCP/IP not by unix socket dheerajjss Linux - Newbie 1 11-10-2008 06:32 AM
can't ping local IP address but can ping remote hosts rob_xx17 Linux - Networking 4 12-02-2006 08:39 AM
3C509B can not connect over TCP/IP (gets DHCP address though) RSpendl Linux - Hardware 4 10-31-2005 06:56 AM
remote administrating modems via MAC address instead of TCP/IP ID10T_error Linux - Networking 0 06-28-2004 11:27 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:20 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration