"IP is 0.0.0.0". I can't ">

A Problem Extend From My Daemon-Problem.

  • 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,'',1024); setvbuf(stdout,NULL,_IOLBF,0); while(fgets(ap_command,sizeof(ap_command),stdin) != NULL) { printf("ECHO:%snr",ap_command); memset(ap_command,'',1024); } return(0); } ---------------------------------------------------------------------------------------------------------------------


  • Actually,at Solaris: 1. inetd call accept and then return the socket's descriptor and the ip-address of the peer. 2. then call fork and create the child-process of the inetd. Since the child-process can see his father's socket,so the child-process also can use the socket's descriptor . 3. when the child-process call exec to run the real server.The memory of the real server's process will replace the memory of the child-process.he sockethe server process to tNow,we lose the ip-address of the peer.But the socket that have connected alaways open after exec. 4. for my daemon-program,I want to know how to get the socket's descriptor that we lose. 5. someone tell me the following things: "Getpeername is called using file 0(stdin) as its first parameter. This is because inetd connect I/O to the socket" I think what he say is correct.


  • Hello Adamhsu, I would be glad to answer your question without further payment. To do that, I suggest you - please close this question - make a request for clarification at http://answers.google.com/answers/main?cmd=threadview&id=254937 so I can respond without further fee. The fix may be as simple as replacing the fprintf statement with fprintf(stdout, "IP is %sn", inet_ntoa(mysock.sin_addr); but if that does not work - I have some other suggestions that can be made. --Maniac