I'm writing a simple UDP server, and right now as placeholder code I'm simply echoing back input. But sendto() is returning -1 and failing to send. On Debian GNU/Linux with GCC 4.4, this happens once and errno is set to EINVAL, then subsequent messages are sent successfully without errors. On SunOS 5.10 (a.k.a. Solaris 10) with GCC 3.4.6, sendto() fails to send, returns -1, and sets errno to 0 on every attempt. I've also tried to do this on Windows, but Winsock is a pain to work with and is throwing some other weird poorly documented error on sendto(), so I gave up on that.

Here is the full function in which this error occurs; the sendto() call is near the end.
Code:
void *server_thread_main(void *argv) {
	
	struct protoent *udp_protocol;
	int socket_in;
	struct sockaddr_in local_addr, remote_addr;
	char recv_buf[128];
	socklen_t recv_len;
	uint16_t local_port = 9000;
	int recv_buf_len;
	
	if ((udp_protocol = getprotobyname("udp")) == NULL)
		server_error("Unable to read UDP protocol information from database.");
	
	if ((socket_in = socket(AF_INET, SOCK_DGRAM, udp_protocol->p_proto)) < 0)
		server_error("Unable to create socket.");
	
	// ARPANET family: Internet.
	local_addr.sin_family = AF_INET;
	// Accept datagrams coming to any address.
	local_addr.sin_addr.s_addr = INADDR_ANY;
	// Accept datagrams coming to specified port (in network byte order!).
	local_addr.sin_port = htons(local_port);
	
	if (bind(socket_in, (struct sockaddr *) &local_addr, sizeof(local_addr)) < 0)
		server_error("Unable to bind to address.");
	
	while (true) {
		
		recv_buf_len = recvfrom(socket_in, recv_buf, sizeof(recv_buf), 0, (struct sockaddr *) &remote_addr, &recv_len);
		if (recv_buf_len < 0)
			server_error("Cannot read socket.");
		
		// Someone sent a datagram. We r dumb. Should do better than this.
		puts("Received datagram.");
		
		// We copycats. Very annoying and dumb. This should be different thread, but we dumb.
		// THIS FAILS.
		if (sendto(socket_in, recv_buf, recv_buf_len, 0, (struct sockaddr *) &remote_addr, recv_len) < 0)
			server_error("Cannot send to client.");
		printf("recv_buf: '%s'\nrecv_len: %d\nrecv_buf_len: %d\n", recv_buf, recv_len, recv_buf_len);
		printf("sizeof(struct sockaddr_storage): %ld\n", sizeof(struct sockaddr_storage));
		
	}
	
}
I run this program and send it the message "test" through netcat. This is the output of my program on my Debian system:
Code:
Received datagram.
ERROR:  Cannot send to client.
        Invalid argument (22)
recv_buf: 'test
'
recv_len: 16
recv_buf_len: 5
sizeof(struct sockaddr_storage): 128
This is the output on the SunOS system:
Code:
Received datagram.
ERROR:  Cannot send to client.
        Error 0 (0)
recv_buf: 'test
'
recv_len: 0
recv_buf_len: 5
sizeof(struct sockaddr_storage): 256
I've spent days trying to figure out this odd error, searching online, trying different arguments, etc. I've even tried looking in the Linux kernel source to try to find what could possibly be setting this errno, but to no avail. Does anyone have any idea why this function call is failing? Any help would be appreciated.