title: Sockets in C(Linux) - How do I get client IP & Port
body: <p>OK so I have a server &amp; client using sockets and threading.</p>
  
  <p>Server:</p>
  
  <pre><code>   &lt;Includes&gt;...
  
  
      struct sockaddr_in server;
      int sock,rvsock;
      int port,max,k = 0, len;
      char buff[1024];
      pthread_t thr[100];
  
    void handler(int sig)
    {
    if(close(rvsock) &lt; 0)
    {
      perror("Close.");
      return;
    }
   }
  
  void *worker (void* a)
  {
      k=0;
      int ar[3];
      int i=0;
      char Smax[1024];
      char *token;
      recv(sock,&amp;buff,sizeof(buff),0);
          token = strtok(buff," ");
  
          while(token &amp;&amp; token!=" ")
  
          {
          int nr = atoi(token);
          ar[i]=nr;
          k=k+nr;
          i++;
          token = strtok(NULL," ");
      }
      if (k &gt; max)
          {
              printf ("%d is indeed &gt; than %d\n",k,max);
              max=k;
          }
  
  printf("Current max is %d\n",max);
  char temp[1024];
  sprintf(temp,"%d",max);
  strcpy(Smax,"The current maximum is: ");
  strcat(Smax,temp);
  strcat(Smax," and the numbers are: ");
  for (i=0;i&lt;3;i++)
  {
      sprintf(temp,"%d",ar[i]);
      strcat(Smax,temp);
      strcat(Smax," ");
  }
  printf ("%s\n",Smax);
  send(sock, &amp;Smax, sizeof(Smax), 0);
  close(sock);
  }
  
  int main (int argc,char* argv[])
  {   
      int i=0;
      if (argc &lt; 2)
      {
          perror ("No port assigned.");
          exit(0);
  }
  else
  {
      sscanf (argv[1],"%d",&amp;port);
  }
  /* Creating socket (AF_INET - TCP/IP) */
  rvsock = socket (AF_INET,SOCK_STREAM,0);
  if (rvsock&lt;0)
  {
      perror("Socket was not created.");
  } 
  
  
  memset(&amp;server,0,sizeof(server));
  server.sin_family=AF_INET;
  server.sin_addr.s_addr=INADDR_ANY;
  server.sin_port=htons(port);
  
  /* Binding socket */
  
  if( bind(rvsock,(struct sockaddr*)&amp;server,sizeof(server))&lt;0)
      perror("Error in binding socket");
  else
      {
          printf("Server established.\nAwaying clients.\n");
      }
  /* Listening */
  
  if (listen(rvsock,5)&lt;0)
      perror("Error in listening");
  len = sizeof(server);
  
  signal(SIGINT,handler);
  
  /* Accepting and threading */
  
  while(1 &amp;&amp; i&lt;100){
      sock = accept (rvsock,(struct sockaddr*)&amp;server,(socklen_t * __restrict__) &amp;len);
      if (sock&lt;0)
      {
          perror("Error in accepting socket");
          break;
      }
      pthread_create(&amp;thr,NULL,worker,i);
      i++;
  }
  int j;
  /* Joining threads */
  for (j=0;j&lt;100;j++)
      pthread_join(thr,NULL);
  return 0;
  </code></pre>
  
  <p>ClientSide:</p>
  
  <p><code>int main(int argc,char* argv[])</code>
  {</p>
  
  <pre><code>int sock;
  
  int k,port;
  
  char len[1024];
  
  char buff[1024];
  
  
  struct sockaddr_in server;
  if (argc&lt;2)
  {
      perror ("Invalid number of args");
      exit(0);
  }
  else
  {
      sscanf(argv[1],"%d",&amp;port);
      randomize(buff);
  }
  
  sock = socket(AF_INET,SOCK_STREAM,0);
  if(sock&lt;0)
      perror("Socket creation failure");
  
  memset(&amp;server,0,sizeof(server));
  server.sin_family=AF_INET;
  server.sin_addr.s_addr=inet_addr("127.0.0.1");
  server.sin_port=htons(port);
  
  if (connect(sock,(struct sockaddr*)&amp;server,sizeof(server))&lt;0)
      perror("Connection error");
  printf("Sending input: %s\n",buff);
  send(sock,buff,sizeof(buff),0);
  while(1)
  {
      k = recv(sock,&amp;len, sizeof(len),0);
      if(k&lt;=0)
          break;
      printf("Got back %s \n",len);
  }
  close(sock);
  return 0;
  </code></pre>
  
  <p>The server has a worker function that sends back data to the client. I need to include in that data the IP and Port of the client.</p>
  
  <p>How can I get that ?</p>