title: getpeername() from listening(server) socket
body: <p>I'm trying to do socket programming.
  I made some example, which is getting ip and port from sockets.</p>
  
  <p>I tried to get IP and port in server and client both.</p>
  
  <p>server has two sockets, listening socket and socket for data transfer with client(I named it "Client socket"). and client has one socket.
  I wanted these socket's local ip&amp;port and peer's ip&amp;port.so I used getsockname() and getpeername().</p>
  
  <p>code is below.</p>
  
  <p>** server **</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;arpa/inet.h&gt;
  #include &lt;sys/socket.h&gt;
  
  void error_handling(char *message);
  
  int main(int argc, char *argv[])
  {
      int serv_sock;
      int clnt_sock;
  
      struct sockaddr_in serv_addr;
      struct sockaddr_in clnt_addr;
      socklen_t clnt_addr_size;
  
      char message[]="Hello World!";
  
      if(argc!=2){
          printf("Usage : %s &lt;port&gt;\n", argv[0]);
          exit(1);
      }
  
      serv_sock=socket(PF_INET, SOCK_STREAM, 0);
      if(serv_sock == -1)
          error_handling("socket() error");
  
      memset(&amp;serv_addr, 0, sizeof(serv_addr));
      serv_addr.sin_family=AF_INET;
      serv_addr.sin_addr.s_addr=htonl(INADDR_ANY);
      serv_addr.sin_port=htons(atoi(argv[1]));
  
  
  
      if(bind(serv_sock, (struct sockaddr*) &amp;serv_addr, sizeof(serv_addr))==-1 )
          error_handling("bind() error"); 
  
      if(listen(serv_sock, 5)==-1)
          error_handling("listen() error");
  
  
  
      clnt_addr_size=sizeof(clnt_addr);  
      clnt_sock=accept(serv_sock, (struct sockaddr*)&amp;clnt_addr,&amp;clnt_addr_size);
      if(clnt_sock==-1)
          error_handling("accept() error");  
  
      write(clnt_sock, message, sizeof(message));
  
      struct sockaddr_in addr1;
      struct sockaddr_in addr2;
  
      struct sockaddr_in addr3;
      struct sockaddr_in addr4;
  
      socklen_t serv_len = sizeof(serv_addr);
      int serv_peer_err = getpeername(serv_sock, (struct sockaddr *)&amp;addr1, &amp;serv_len);
      int clnt_peer_err = getpeername(clnt_sock, (struct sockaddr *)&amp;addr2, &amp;clnt_addr_size);
  
      int serv_sock_err = getsockname(serv_sock, (struct sockaddr *)&amp;addr3, &amp;serv_len);
      int clnt_sock_err = getsockname(clnt_sock, (struct sockaddr *)&amp;addr4, &amp;clnt_addr_size);
  
      printf("Server socket's ip : %s\n", inet_ntoa(addr3.sin_addr));
      printf("Server socket's port : %d\n", ntohs(addr3.sin_port));
      printf("Server socket's peer ip : %s\n", inet_ntoa(addr1.sin_addr));
      printf("Server socket's peer port : %d\n\n\n\n\n\n", ntohs(addr1.sin_port));
  
  
  
      printf("Client socket's ip : %s\n", inet_ntoa(addr4.sin_addr));
      printf("Client socket's port : %d\n", ntohs(addr4.sin_port));
      printf("Client socket's peer ip : %s\n", inet_ntoa(addr2.sin_addr));
      printf("client socket's peer port %d\n", ntohs(addr2.sin_port));
  
      printf("%d %d %d %d\n", serv_peer_err, clnt_peer_err, serv_sock_err, clnt_sock_err);
  
  
      close(clnt_sock);   
      close(serv_sock);
      return 0;
  }
  
  void error_handling(char *message)
  {
      fputs(message, stderr);
      fputc('\n', stderr);
      exit(1);
  }
  </code></pre>
  
  <p>** client **</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;arpa/inet.h&gt;
  #include &lt;sys/socket.h&gt;
  
  void error_handling(char *message);
  
  int main(int argc, char* argv[])
  {
      int sock;
      struct sockaddr_in serv_addr;
      char message[30];
      int str_len;
  
      if(argc!=3){
          printf("Usage : %s &lt;IP&gt; &lt;port&gt;\n", argv[0]);
          exit(1);
      }
  
      sock=socket(PF_INET, SOCK_STREAM, 0);
      if(sock == -1)
          error_handling("socket() error");
  
      memset(&amp;serv_addr, 0, sizeof(serv_addr));
      serv_addr.sin_family=AF_INET;
      serv_addr.sin_addr.s_addr=inet_addr(argv[1]);
      serv_addr.sin_port=htons(atoi(argv[2]));
  
      if(connect(sock, (struct sockaddr*)&amp;serv_addr, sizeof(serv_addr))==-1) 
          error_handling("connect() error!");
  
      str_len=read(sock, message, sizeof(message)-1);
      if(str_len==-1)
          error_handling("read() error!");
  
      struct sockaddr_in addr1;
      struct sockaddr_in addr2;
  
      int clnt_sock_err, clnt_peer_err;
      socklen_t serv_len = sizeof(serv_addr);
      clnt_peer_err = getpeername(sock, (struct sockaddr *)&amp;addr1, &amp;serv_len);
      clnt_sock_err = getsockname(sock, (struct sockaddr *)&amp;addr2, &amp;serv_len);
  
      printf("Client socket's ip : %s\n", inet_ntoa(addr2.sin_addr));
      printf("client socket's port %d\n", ntohs(addr2.sin_port));
      printf("Client socket's peer ip : %s\n", inet_ntoa(addr1.sin_addr));
      printf("Client socket's peer port : %d\n", ntohs(addr1.sin_port));
  
      printf("%d %d\n", clnt_sock_err, clnt_peer_err);
  
  
  
      printf("Message from server: %s \n", message);  
      close(sock);
      return 0;
  }
  
  void error_handling(char *message)
  {
      fputs(message, stderr);
      fputc('\n', stderr);
      exit(1);
  }
  </code></pre>
  
  <p>and I run it, ip is loopback(127.0.0.1) and port is 7755. </p>
  
  <p>the result is :</p>
  
  <p><strong>server</strong></p>
  
  <p>Server socket's ip : 0.0.0.0</p>
  
  <p>Server socket's port : 7755</p>
  
  <p>Server socket's peer ip : 1.0.0.0</p>
  
  <p>Server socket's peer port : 4992</p>
  
  <p>Client socket's ip : 127.0.0.1</p>
  
  <p>Client socket's port : 7755</p>
  
  <p>Client socket's peer ip : 127.0.0.1</p>
  
  <p>client socket's peer port 6311</p>
  
  <p>-1 0 0 0</p>
  
  <p><strong>client</strong></p>
  
  <p>Client socket's ip : 127.0.0.1</p>
  
  <p>client socket's port 6311</p>
  
  <p>Client socket's peer ip : 127.0.0.1</p>
  
  <p>Client socket's peer port : 7755</p>
  
  <p>0 0</p>
  
  <p>Message from server: Hello World!</p>
  
  <p>I noticed getpeername() failed with server socket's peer, then returned -1. but it showed IP and port : 1.0.0.0 and 4992. I don't know what these mean. and I think another results make sense.
  I know listening(server) socket only deal with connection, not data transfer,
  server create another socket for data transfer by accept().</p>
  
  <p>my question is ㅡ if listening(server) socket only deal with client's connection, then its peer should not be exist? what's that wrong IP and Port? just garbage value?</p>