title: How to solve: sending UDP packet using Sendto() got "message too long"
body: <p>I want to use the sendto() API to send video and audio data  through UDP packet. The sending buffer size I got using getsockopt() is 114688, however, sendto() returned -1 when the data packet less than 65536 not 114688. And the error message is Message too long.</p>
  
  <p>When I used setsockopt() to adjust the sending buffer size as 200000, I used getsockopt() and found the sending buffer size was not 200000 but 262142. So I still got the same error when I sent data packet with a size bigger than 65536. </p>
  
  <p>I am confused about this situation. I want to know what the reason is and how to solve this problem.</p>
  
  <p>When I used FFMPEG library to send the video and audio packet, there is no error. So I am sure there is a solution for this problem and I missed something.</p>
  
  <p>Is there anyone can help me about this problem? I really can not understand what the reason is.</p>
  
  <p>The OS I used is ubuntu 11.04,I got the same results in ubuntu 11.10.</p>
  
  <p>That is the code I used to create socket and configure the parameter：</p>
  
  <pre><code>unsigned char *output_buffer = (unsigned char*)av_malloc(IO_BUFFER_SIZE);
  if (NULL == output_buffer) {
      printf("Couldn't allocate input buffer.\n");
      return NULL;
  }
  
  output_context_data_t *context_data = (output_context_data_t *)malloc(sizeof(output_context_data_t));
  if (NULL == context_data) {
      printf("Could not allocate output context data.\n");
      av_free(output_buffer);
      return NULL;
  }
  
  context_data-&gt;socket = socket(AF_INET, SOCK_DGRAM, 0);
  if(context_data-&gt;socket &lt; 0) {
      printf("socket creating fail!\n");
      return NULL;    
  }
  
  context_data-&gt;socket_addr-&gt;sin_family = AF_INET;
  context_data-&gt;socket_addr-&gt;sin_port = htons(output_port);
  ret = inet_pton(AF_INET, output_ip, &amp;(context_data-&gt;socket_addr-&gt;sin_addr));
  if(0 == ret) {
      printf("inet_pton fail!\n");
      return NULL;
  }
  
  ret = setsockopt(context_data-&gt;socket, IPPROTO_IP, IP_MULTICAST_TTL,
                      &amp;option_ttl, sizeof(int));
  if(ret &lt; 0) {
      printf("ttl configuration fail!\n");
      return NULL;
  }
  
  ret = setsockopt(context_data-&gt;socket, SOL_SOCKET, SO_REUSEADDR, &amp;reuse, sizeof(int));
  if(ret &lt; 0) {
      printf("resue configuration fail!\n");
      return NULL;
  }   
  </code></pre>
  
  <p>That is the code to send UDP packet：</p>
  
  <pre><code>int send_size = sendto(context_data-&gt;socket, buf, buf_size, 0,
                        (struct sockaddr *)context_data-&gt;socket_addr, sizeof(*context_data-&gt;socket_addr)));
  //the video or audio data is in buf and its size is buf_size.
  </code></pre>
  
  <p>That is the code I used to get the sending buffer size：</p>
  
  <pre><code>int bufsize; 
  int size = sizeof(bufsize);
  getsockopt(context_data-&gt;socket,SOL_SOCKET, SO_SNDBUF, &amp;bufsize, &amp;size);
  </code></pre>
  
  <p>That is the code I used to configure the sending buffer size：</p>
  
  <pre><code>tmp = 200000;
  ret = setsockopt(context_data-&gt;socket, SOL_SOCKET, SO_SNDBUF, &amp;tmp, sizeof(tmp));
  if(ret &lt; 0) {
      printf("sending buffer size configuration fail!\n");
      return NULL;
  }
  </code></pre>