how to write code in C to read client IP (browser) address - Sajha Mobile
SAJHA MOBILE
how to write code in C to read client IP (browser) address
Posts 8 · Viewed 5032 · Go to Last Post
i_my
· Snapshot
Like · Likedby · 0
need help with the C code.
designing a proxy server.
can't find a function that would return client IP address.
help please.
sedif
· Snapshot
Like · Liked by · 0
let me google that for you
http://unixwiz.net/tools/whoamip.html
is this what you had in mind?

some codes here:
http://forums.debian.net/viewtopic.php?t=31673&sid=77365c8c16168343211ec674e17826cc



i_my
· Snapshot
Like · Liked by · 0
Thanks Sedif. :-)
techGuy
· Snapshot
Like · Liked by · 0

wow sedif..didnt know abt let me google that for you, that was nice

http://tinyurl.com/6p2z7d (This just for test)

Dont know much abt C, but got following from http://www.cygwin.com/ml/cygwin/2004-04/msg00894.html, might be of some help.

/* little utility to get the IP address of the machine

- prints the IP address in triple-dotted form xxx.yyy.zzz.www,

or nothing if can't get it.

- this code is a fragment from EpidEm code.

*/

#include <stdio.h>

#include <arpa/inet.h>

#include <netinet/in.h>

#include <unistd.h>

#include <netdb.h>

/*

Returns the IP address of this host.

- if host have more than 1 IP, only 1 (the first) is returned.

- return is in network byte order

- thanks to Doct. Ghini (www.cs.unibo.it/~ghini)

return: 0 if unsuccessful, the IP otherwise

*/

in_addr_t get_my_IP()

{

/* have you ever seen a hostname longer than a screen (80cols)?*/

char name[80]; /*store my hostname*/

struct hostent * hostent_ptr;

int ret;

ret = gethostname (name, 80);

if(ret == -1) {

/*printf ("ERROR gethostname() failed, Errno=%d \nDescription: %s\n", errno,

strerror(errno));*/

return 0;

}


hostent_ptr = gethostbyname(name);


if(hostent_ptr == NULL)

{

/*printf ("ERROR gethostbyname() failed, h_errno=%d \nDescription: %s\n",
h_errno, hstrerror(h_errno));*/

return 0;

}

/*h_addr_list contains IPs of this host in network byte order */

return ((struct in_addr *)hostent_ptr->h_addr_list[0])->s_addr; /*get the
first IP.*/

}

int main()

{

in_addr_t my_ip = get_my_IP();

if (my_ip != 0)

{

struct in_addr temp;

temp.s_addr = my_ip;

printf("%s\n", inet_ntoa(temp));

}

return 0;

}

sedif
· Snapshot
Like · Liked by · 0
np
@techGuy: ya read a Digg post on it, cleaver idea.
Sleepless
· Snapshot
Like · Liked by · 0
Yes , gethostbyname( ) does it.

But werent you looking for Client's IP Address? And if you are coding for a Proxy Server, and all you can do is code in that server? I'd suggest you use connection request headers from the clients to find its IP Address.
sajhakhohero
· Snapshot
Like · Liked by · 0

Can someone know how to crack these array problems please.

 1. Write a program to double the array elements with odd indices?

Eg. Element 1, 2,3,4,5,6,7,8

Soln: 1,6,10,14 <program only double the element with odd indices>

 

2. Find the first element larger than 100.

 

    

thank you

Sakura_Blossom
· Snapshot
Like · Liked by · 0
;
Please log in to reply to this post

You can also log in using your Facebook
View in Desktop
What people are reading
You might like these other discussions...
· Posts 9 · Viewed 12564 · Likes 1
· Posts 7 · Viewed 3979 · Likes 1
· Posts 1 · Viewed 1091
· Posts 1 · Viewed 1072
· Posts 1 · Viewed 980
· Posts 1 · Viewed 1074
· Posts 1 · Viewed 1045
· Posts 21 · Viewed 9984 · Likes 1
· Posts 3 · Viewed 2588
· Posts 5 · Viewed 7781 · Likes 1



Your Banner Here
Travel Partners
Travel House Nepal