Networking Forums

Networking Forums > Computer Networking > Linux Networking > How Can I Get Client's IP Address in a Socket Connection?

Reply
Thread Tools Display Modes

How Can I Get Client's IP Address in a Socket Connection?

 
 
hn.ft.pris@gmail.com
Guest
Posts: n/a

 
      04-11-2006, 08:59 AM
My idea is like this:
struct sockaddr_in cli_addr;
.........

clilen = sizeof(cli_addr);
newsockfd = accept(sockfd,(struct sockaddr *) &cli_addr,
&clilen);
char addr_buf[16];

addr_buf= inet_ntop (AF_INET, ( const void
*)&cli_addr.sin_addr.s_addr,addr_buf,16);

Then I thought client's ip address is stored in addr_buf , but it
doesnn't work due to complie error ,"incompatible types in assignment"
, Is there anything wrong? I'm confused...

 
Reply With Quote
 
 
 
 
Robert Harris
Guest
Posts: n/a

 
      04-11-2006, 09:24 AM
(E-Mail Removed) wrote:
> My idea is like this:
> struct sockaddr_in cli_addr;
> .........
>
> clilen = sizeof(cli_addr);
> newsockfd = accept(sockfd,(struct sockaddr *) &cli_addr,
> &clilen);
> char addr_buf[16];
>
> addr_buf= inet_ntop (AF_INET, ( const void
> *)&cli_addr.sin_addr.s_addr,addr_buf,16);
>
> Then I thought client's ip address is stored in addr_buf , but it
> doesnn't work due to complie error ,"incompatible types in assignment"
> , Is there anything wrong? I'm confused...
>

You can't assign to an array (it isn't an lvalue). The best way to
express it is:

if (inet_ntop (AF_INET, &cli_addr.sin_addr, addr_buf,16)) {
/* Here inet_ntop has been successful */
}
else {
/* Here inet_ntop has been unsuccessful */
}

Robert
 
Reply With Quote
Reply

Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
strange behavior when the client bind its UDP socket to a local address Christophe Lohr Linux Networking 14 01-23-2009 01:03 PM
How the selection of IP address is done for a tcp socket on client side if there are multiple alias configured Rohit Linux Networking 0 01-22-2007 11:34 AM
How the selection of IP address is done for a tcp socket on client side if there are multiple alias configured Rohit Linux Networking 0 01-22-2007 11:34 AM
Hackers and "Client filtering settings blocked connection from IP address" Cris Broadband Hardware 5 07-27-2004 12:24 AM
Cannot bind name to socket! : Address already in use Ravi Linux Networking 1 12-01-2003 04:52 PM



1 2 3 4 5 6 7 8 9 10 11