Posted in xn--kfs74mzzid01b.com edit by admin on November 25th, 2008
I ever ask a question about daemon.
The Title is
"How can I get the ip of a machine that connect to my machine(daemon)"
I think the answer is correct after I read the answer.
But I can't get the same result with the same code on my solaris 5.8.
When I telnet 127.0.0.1 at port = 9999,I always get the message => "IP
is 0.0.0.0".
I can't get a message like "IP is 127.0.0.1".
Now,I want to know how to let the server response a message like "IP
is 192.168.166.11",
when I telnet to the server.
example :
The Solaris' ip is 192.168.166.123 And The port is 9999
My ip is 192.168.166.11
At my machine,I use "telnet 192.168.166.123 9999"
I wish the solaris server can response the message "IP is
192.168.166.11"
I supply the code for the daemon program
---------------------------------------------------------------------------------------------------------------------
Other information for this.
After I read the answer,I can't see any question about it.
So,I think "The code is correct,But I ignore something then let the
program can't get the ip of the peer".
And If you want to know why I ask the problem,you can see the
question-answer.
"How can I get the ip of a machine that connect to my
machine(daemon)",
Question ID: 254937
---------------------------------------------------------------------------------------------------------------------
the following is the sample code(I have modify one line for
sun-solaris).
// Sample Code
#include
#include
#include
#include
#include
#include
struct sockaddr_in mysock;
socklen_t namelen; // <= I change int type to socklen_t type for
sun-solaris
int main(void)
{
char ap_command[1024];
int errcode;
char myname="mydaemon";
namelen = sizeof(struct sockaddr_in);
if (getpeername(0, (struct sockaddr *)&mysock, &namelen)) {
fprintf(stderr, "%s : Failed to get peer namen", myname);
perror(myname);
}
else
fprintf(stdout, "IP is %d.%d.%d.%dn",
(mysock.sin_addr.s_addr>>24) & 255,
(mysock.sin_addr.s_addr>>16) & 255,
(mysock.sin_addr.s_addr>>8) & 255,
mysock.sin_addr.s_addr & 255);
memset(ap_command,'