I was searching for hours to get an answer about my question, but didnt find anything. Maybe I get some help here.
What I'm trying to do: A Java-Client sends a message to a C-Server. The message contains different types like integer, short and also a string (e.g. message = int: total_msg_length; short: operation; string: hello --> total_msg-length=4 (size of integer), operation = 2 (size of short), hello = 5 (each letter is 1 byte=5).
So, how can I receive the message in my server? The code below receives an Integer (works fine). Next step will be to receive a short and then a string (converted in US-ASCII).
int *msg;
int recv_size;
int final_msg;
if( (recv_size = recv(client_socket, &msg, sizeof(msg), 0 )) < 0 ){
error_exit("Fehler bei recv(message_len)");
}
final_msg = endian_swap(msg);
printf("Message: %d\n", final_msg);
return final_msg;
Is there a way to use a byte array instead of char buffer? Im thankful for every help. Please excuse my bad english, I'm from germany :-)
sizeof(msg)instead ofsizeof(int). Your code works 'cause both sizes are equal in most platforms, but it is not correct. You want to receive an int, not an int pointer. The size of a pointer is different, in general, to the size of the pointed value.recv()/send()and learn that at least for sockets those two functions do not necessarily receive/send as much bytes as they were told to, but few. So looping around such calls counting until all data had been received/sent is a good idea, not to say an essential necessity.