title: sendto: Invalid argument
body: <p>I'm trying to get a program (client) to communicate with another (server) using sockets in C, but I'm constantly getting the error "Invalid Argument" from the server.</p>
  
  <p>I'll try to show the pieces of code that are relevant to this issue after explaining what's happening:</p>
  
  <p>When it comes to the Client, I assume that everything is working correctly: The client sends a request to server and the server receives the message correctly, but then when the server attempts to reply I get an "Invalid Argument" error.</p>
  
  <p>This is the declaration of the variables used by the main function of the server:</p>
  
  <pre><code>int port = DEFAULT_PORT;
  int sock;
  struct sockaddr_in hostAdress, clientAdress;
  socklen_t clientAdressLength;
  </code></pre>
  
  <p>Here I do the binding of the socket to the hostAdress:</p>
  
  <pre><code>sock = socket(PF_INET,SOCK_DGRAM,0);
  memset(&amp;hostAdress, 0, sizeof(struct sockaddr_in));
  memset(&amp;clientAdress, 0, sizeof(struct sockaddr_in));
  hostAdress.sin_family = AF_INET;
  hostAdress.sin_port = htons(port);
  hostAdress.sin_addr.s_addr = INADDR_ANY;
  bind(sock, (struct sockaddr*) &amp;hostAdress, sizeof(hostAdress));
  </code></pre>
  
  <p>After a bit of code, I go into a loop receiving information, using recvfrom in this manner:</p>
  
  <pre><code>recvfrom(sock, dataBuffer, sizeof(dataBuffer), 0, (struct sockaddr*)&amp;clientAdress, &amp;clientAdressLength);
  </code></pre>
  
  <p>When the server receives the request, it identifies it and jumps to this function (this behaviour is confirmed):</p>
  
  <pre><code>handle_helloRQ(sock, clientAdress, clientAdressLength);
  </code></pre>
  
  <p>While I am aware that "clientAdressLength" could be obtained doing sizeof(clientAdress), somehow doing this fixed this same issue last time I had it.
  This function is declared as:</p>
  
  <pre><code>int handle_helloRQ(int sock, struct sockaddr_in server_addr, socklen_t server_addr_length)
  </code></pre>
  
  <p>In this function, the main variables are:</p>
  
  <pre><code>char buffer[512];
  size_t bufferSize = sizeof(buffer);
  memset(&amp;buffer, 0, sizeof(buffer));
  stshort(2, buffer);
  strcat(buffer+2, "Hello World");
  </code></pre>
  
  <p>stshort is a simple macro that places a short int in the first 2 bytes of the buffer. This works as intended.</p>
  
  <p>Once that is done, it attempts to do the following:</p>
  
  <pre><code>sendto(sock, buffer, bufferSize, 0, (struct sockaddr*)&amp;server_addr, server_addr_length);
  </code></pre>
  
  <p>... And this is when things go wrong. sendto returns <code>-1</code>, and using <code>perror("sendto");</code> I get an "<code>Invalid Argument</code>" error.
  I tried a thousand different ways of imputing the parameters, but I can not find an error. I have exhausted my other options when it comes to this kind of problems (namely, asks the teachers of my university and show the code to a few more advanced colleagues), and no one seemed to be able to find the error.
  As a side note, I do use</p>
  
  <pre><code>close(sock);
  </code></pre>
  
  <p>when the server receives the signal to be closed, but I may have had to force-close it once before it managed to reach this function.</p>
  
  <p>To whoever is reading this, thank you for your time!</p>