title: Sending struct over UDP c
body: <p>I'm having trouble sending a struct via UDP.  I am a newbie to networking as well as C but need to send data as a struct (not serialized).  I am getting a segmentation fault when I attempt to send.  Please excuse any newbie mistakes.  Any help would be appreciated.</p>
  
  <p>Server side:</p>
  
  <pre><code>struct TextMessage
  {
     unsigned int SenderId;                       /* unique client identifier */
     char message[100];                           /* text message */
  };
  
  int main(int argc, char *argv[])
  {
    int sock;                        /* Socket */
    struct sockaddr_in echoServAddr; /* Local address */
    struct sockaddr_in echoClntAddr; /* Client address */
    unsigned int cliAddrLen;         /* Length of incoming message */
    unsigned short echoServPort;     /* Server port */
    int recvMsgSize;                 /* Size of received message */ 
  
    struct TextMessage * temp = malloc(sizeof(struct TextMessage));
  
  
  echoServPort = atoi(argv[1]);  /* First arg:  local port *
  
    /* Create socket for sending/receiving datagrams */  
    if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) &lt; 0)
        DieWithError("socket() failed");
  
    /* Construct local address structure */
    memset(&amp;echoServAddr, 0, sizeof(echoServAddr));   /* Zero out structure */
    echoServAddr.sin_family = AF_INET;                /* Internet address family */
    echoServAddr.sin_addr.s_addr = htonl(INADDR_ANY); /* Any incoming interface */
    echoServAddr.sin_port = htons(echoServPort);      /* Local port */
  
    /* Bind to the local address */ 
    if (bind(sock, (struct sockaddr *) &amp;echoServAddr, sizeof(echoServAddr)) &lt; 0)
        DieWithError("bind() failed");
  
    for (;;) /* Run forever */
    {
        /* Set the size of the in-out parameter */
        cliAddrLen = sizeof(echoClntAddr);
  
        /* Block until receive message from  a client */
        if ((recvMsgSize = recvfrom(sock, &amp;temp, sizeof(temp), 0,
            (struct sockaddr *) &amp;echoClntAddr, &amp;cliAddrLen)) &lt; 0)
            DieWithError("recvfrom() failed");
  
            printf("Handling client %s\n", inet_ntoa(echoClntAddr.sin_addr));
            printf("Incoming Length: %s\n", cliAddrLen);
        printf("Received: %s\n", temp.message);    /* Print the string in sent struct */
  
    }
    /* NOT REACHED */
  } 
  </code></pre>
  
  <p>Client side:</p>
  
  <pre><code>struct TextMessage
  {
      unsigned int SenderId;                       /* unique client identifier */
      char message[100];                           /* text message */
  };
  
  int main(int argc, char *argv[]) 
  { 
      int sock;                        /* Socket descriptor */
      struct sockaddr_in echoServAddr; /* Echo server address */
      struct sockaddr_in fromAddr;     /* Source address of echo */
      unsigned short echoServPort;     /* Echo server port */
      unsigned int fromSize;           /* In-out of address size for recvfrom() */
      char *servIP;                    /* IP address of server */
      int structLen;                   /* Length of string to echo */
      int respStringLen;               /* Length of received response */
  
  
      struct TextMessage newMess;
  
      memset(&amp;newMess, 0, sizeof(newMess));    /* Zero out structure */
      newMess.request_Type = Send;
      newMess.SenderId = 300;
      newMess.RecipientId = 200;
      strcpy(newMess.message, argv[3]);
  
      printf("Size struct: %d\n", structLen);
  
      servIP = argv[1];           /* First arg: server IP address (dotted quad) */
      echoServPort = argv[2];  /* Use given port, if any */
  
  /* Create a datagram/UDP socket */
      if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) &lt; 0)
      DieWithError("socket() failed");
  
      /* Construct the server address structure */
      memset(&amp;echoServAddr, 0, sizeof(echoServAddr));    /* Zero out structure */
      echoServAddr.sin_family = AF_INET;                 /* Internet addr family */
      echoServAddr.sin_addr.s_addr = inet_addr(servIP);  /* Server IP address */
      echoServAddr.sin_port   = htons(echoServPort);     /* Server port */
  
  
     /* Send the string to the server */
     if (sendto(sock, (void *)&amp;newMess, sizeof(newMess), 0, (struct sockaddr *)
             &amp;echoServAddr, sizeof(echoServAddr)) != structLen)
         DieWithError("sendto() sent a different number of bytes than expected\n");
  
    close(sock);
    exit(0);
  }
  </code></pre>