title: Receiving UDP message but recv returns 0
body: <p><strong>Problem:</strong> I have two c programs, one sends a UDP message, then gets a response and prints it. The other receives a UDP message, modifies it, and sends it back. I receive the message correctly, modify correctly (prints ok) and send it. I am receiving the message on the other end but with a recv return value of 0. </p>
  
  <p><strong>Question:</strong> Why do I get the proper message, but don't have the length of the message returned? This is hard to check that I received the proper length message as my goal is to access the 320th byte of a status message</p>
  
  <p>I found <a href="https://stackoverflow.com/questions/39516683/recv-returns-always-0">this</a> but I am not using strlen(), plus the buffer actually contains the proper message. Also i am not disconnecting the socket, as I am using UDP. </p>
  
  <p>Here is the code for the client:</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;netdb.h&gt;
  #include &lt;sys/socket.h&gt;
  #define SERVICE_PORT    9090
  
  #define BUFLEN 2048
  #define MSGS 5
  
  int main(void)
  {
      struct sockaddr_in myaddr, remaddr;
      int fd, i, slen=sizeof(remaddr);
      char *server = "127.0.0.1"; 
      char buf[BUFLEN];
      ssize_t msglen;
  
  
      if ((fd=socket(AF_INET, SOCK_DGRAM, 0))==-1)
          printf("socket not created\n");
  
  
      memset((char *)&amp;myaddr, 0, sizeof(myaddr));
      myaddr.sin_family = AF_INET;
      myaddr.sin_addr.s_addr = htonl(INADDR_ANY);
      myaddr.sin_port = htons(7096);
  
      if (bind(fd, (struct sockaddr *)&amp;myaddr, sizeof(myaddr)) &lt; 0) {
          perror("bind failed");
          return 0;
      }
  
      memset((char *) &amp;remaddr, 0, sizeof(remaddr));
      remaddr.sin_family = AF_INET;
      remaddr.sin_port = htons(SERVICE_PORT);
      if (inet_aton(server, &amp;remaddr.sin_addr)==0) {
          fprintf(stderr, "inet_aton() failed\n");
          exit(1);
      }
  
      char msg[4096];
      memset(msg, 0, 4096);
      for (i=0; i &lt; MSGS; i++) {
          printf("Sending packet %d to %s port %d\n", i, server, SERVICE_PORT);
          sprintf(buf, "This is packet %d", i);
          if (sendto(fd, buf, sizeof(buf), 0, (struct sockaddr *)&amp;remaddr, slen)==-1)
              perror("sendto");
          memset(msg, 0, 4096);
          if(msglen = recv(fd, msg, sizeof(msg), MSG_WAITALL) == -1){
              printf("Bad recv\n");
          }
          printf("recieved %d bytes: %s\n", msglen, msg);
      }
      close(fd);
      return 0;
  }
  </code></pre>
  
  <p>here is the code for the server:</p>
  
  <pre><code>#include &lt;arpa/inet.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;unistd.h&gt;
  
  #include &lt;linux/ip.h&gt; /* for ipv4 header */
  #include &lt;linux/udp.h&gt; /* for upd header */
  
  #define ADDR_TO_BIND "127.0.0.1"
  #define PORT_TO_BIND 9090
  #define SERVICE_PORT 7096
  #define MSG_SIZE 256
  #define HEADER_SIZE (sizeof(struct iphdr) + sizeof(struct udphdr))
  
  int main(void) {
      int raw_socket, fd;
      struct sockaddr_in sockstr, remaddr;
      socklen_t socklen;
  
      int retval = 0; 
  
      char msg[MSG_SIZE];
      ssize_t msglen; 
  
  
      if ((raw_socket = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
          perror("socket");
          return 1; 
      }
      if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
          perror("socket");
          return 1;
      }
      sockstr.sin_family = AF_INET;
      sockstr.sin_port = htons(PORT_TO_BIND);
      sockstr.sin_addr.s_addr = inet_addr(ADDR_TO_BIND);
      socklen = (socklen_t) sizeof(sockstr);
      if (bind(raw_socket, (struct sockaddr*) &amp;sockstr, socklen) == -1) {
          perror("bind");
          retval = 1; 
          goto _go_close_socket;
      }
      if(bind(fd, (struct sockaddr*) &amp;remaddr, socklen) == -1){
          perror("bind");
      }
      memset(msg, 0, MSG_SIZE);
  while(1){
      if ((msglen = recv(raw_socket, msg, MSG_SIZE, 0)) == -1) {
          perror("recv");
          retval = 1;
          goto _go_close_socket;
      }
      memset((char *) &amp;remaddr, 0, sizeof(remaddr));
      remaddr.sin_family = AF_INET;
      remaddr.sin_port = htons(SERVICE_PORT);
      if (inet_aton(ADDR_TO_BIND, &amp;remaddr.sin_addr)==0) {
          fprintf(stderr, "inet_aton() failed\n");
          exit(1);
      }
      unsigned char buf[4096];
      msg[msglen] = '\0';
      printf("recieve %d bytes: %s\n", msglen, msg);
      sprintf(buf, "Your msg is: %s\n",msg );
      int slen=sizeof(remaddr);
      if (sendto(fd, buf, sizeof(buf), 0, (struct sockaddr *)&amp;remaddr, slen)==-1){
          printf("send failed");    
      }
      printf("sent: %s\n", buf);
  }
  _go_close_socket:
      close(raw_socket);
  
      return retval;
  }
  </code></pre>
  
  <p>The output of running the client is:</p>
  
  <pre><code>Sending packet 0 to 127.0.0.1 port 9090
  recieved 0 bytes: Your msg is: This is packet 0
  
  Sending packet 1 to 127.0.0.1 port 9090
  recieved 0 bytes: Your msg is: This is packet 1
  
  Sending packet 2 to 127.0.0.1 port 9090
  recieved 0 bytes: Your msg is: This is packet 2
  
  Sending packet 3 to 127.0.0.1 port 9090
  recieved 0 bytes: Your msg is: This is packet 3
  
  Sending packet 4 to 127.0.0.1 port 9090
  recieved 0 bytes: Your msg is: This is packet 4
  </code></pre>
  
  <p>The output of running the server is:</p>
  
  <pre><code>recieve 256 bytes: This is packet 0
  sent: Your msg is: This is packet 0
  
  recieve 256 bytes: This is packet 1
  sent: Your msg is: This is packet 1
  
  recieve 256 bytes: This is packet 2
  sent: Your msg is: This is packet 2
  
  recieve 256 bytes: This is packet 3
  sent: Your msg is: This is packet 3
  
  recieve 256 bytes: This is packet 4
  sent: Your msg is: This is packet 4
  </code></pre>
  
  <p>As you see, the message I get on the server is changed to have "your msg is: " at the beginning, and I am seeing that on the client side, but the return value is 0 for the length. Why is this?</p>
  
  <p>This code are just slightly modified code samples of example UDP server client, so I won't be offended if the code quality is pointed out to be bad!</p>
  
  <p><strong>EDIT</strong> This was resolved, it was an issue with wrapping the recv() call in parentheses so the conditional was stored in msglen rather than the actual return value. See my answer below</p>