title: C++ sockets sending structures
body: <p>I can send/receive data over sockets using a char array but can't figure out a way to send structures . I found a lot of articles but they are to hard to understand .. As far as I know we have to use a function called snprintf to make packets of data and then some other to revive it . Please I am looking for a very reliable but simple way of data transfer .. Here is some code I wrote to send via char array</p>
  
  <pre><code>int main()
  {
      int listenfd = 0, connfd = 0;
      struct sockaddr_in serv_addr; 
  
      char sendBuff[1025];
      time_t ticks;
      printf("\n\n...Server is starting up...\n\n");
      listenfd = socket(AF_INET, SOCK_STREAM, 0);
  
      memset(&amp;serv_addr, '0', sizeof(serv_addr));
      memset(sendBuff, '0', sizeof(sendBuff)); 
  
      serv_addr.sin_family = AF_INET;
      serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
      serv_addr.sin_port = htons(4567);
  
      bind(listenfd, (struct sockaddr*)&amp;serv_addr, sizeof(serv_addr));
      printf("Bind successful\n");
      listen(listenfd, 10);
      printf("Ready: Waiting for clients\n");
      while(1)
      {
          connfd = accept(listenfd, (struct sockaddr*)NULL, NULL); 
          ticks = time(NULL);
          snprintf(sendBuff, sizeof(sendBuff), "%.24s\r\n", ctime(&amp;ticks));
          write(connfd, sendBuff, strlen(sendBuff));
          close(connfd);
          sleep(1);
      }
  </code></pre>
  
  <p>Client</p>
  
  <pre><code>int main(int argc, char *argv[])
  {
      int sockfd = 0 /*Socket Descriptor*/, n = 0;
      char recvBuff[1024];
      struct sockaddr_in serv_addr; 
  
      if(argc != 2)  {
          printf("\n Usage: %s &lt;ip of server&gt; \n",argv[0]);
          return 1;
      } 
  
      memset(recvBuff, '0',sizeof(recvBuff));
      if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) &lt; 0) {
          printf("\n Error : Could not create socket \n");
          return 1;
      } 
  
      memset(&amp;serv_addr, '0', sizeof(serv_addr)); 
      serv_addr.sin_family = AF_INET;
      serv_addr.sin_port = htons(4567); 
      if(inet_pton(AF_INET, argv[1], &amp;serv_addr.sin_addr)&lt;=0) {
          printf("\n inet_pton error occured\n");
          return 1;
      } 
  
      if( connect(sockfd, (struct sockaddr *)&amp;serv_addr, sizeof(serv_addr)) &lt; 0) {
         printf("\n Error : Connect Failed \n");
         return 1;
      } 
  
      while ( (n = read(sockfd, recvBuff, sizeof(recvBuff)-1)) &gt; 0) {
          recvBuff[n] = 0;
          if(fputs(recvBuff, stdout) == EOF) {
              printf("\n Error : Fputs error\n");
          }
      } 
  
      if(n &lt; 0) {
          printf("\n Read error \n");
      } 
  
      return 0;
  }
  </code></pre>