The destination recives the correct ammount of bytes but the string recived is trash.
Auxiliar function:
ssize_t send_all(int socket, const void *buffer, size_t length, int flags) {
ssize_t n;
const char *p = buffer;
while (length > 0)
{
n = send(socket, p, length, flags);
if (n <= 0) break;
p += n;
length -= n;
}
return (n <= 0) ? -1 : 0;
}
This is my sender:
p_status_t aviso_gestion_tema(struct sockaddr_in id, char* tema, int tema_name_length, tipo_msg_intermediario precedente) {
//...
int cd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(connect(cd, (struct sockaddr*) &id, sizeof(id)) == -1) {
#ifdef DEBUG_ERR
fprintf(stderr, "connect: %s\n", strerror(errno));
#endif
op_result = CALLBACK_TRANSM_ERROR;
}
else if(send(cd, &tipo, 1, 0) == -1) { op_result = CALLBACK_TRANSM_ERROR; }
else if(send_all(cd, &tema, tema_name_length, 0) == -1) { op_result = CALLBACK_TRANSM_ERROR; }
#ifdef DEBUG_MSG
fprintf(stderr, "aviso-gestion-gema (%d bytes): %s\n", tema_name_length, tema);
#endif
close(cd);
This is a simplifcation of what I do on the reciver:
int cd;
char tipo_msg;
struct sockaddr_in client_ain;
socklen_t c_ain_size;
char buff[BUFFER_SIZE];
ssize_t buff_readed_aux;
unsigned int tema_name_length;
c_ain_size = sizeof(client_ain);
cd = accept(socket_recepcion, (struct sockaddr*)&client_ain, &c_ain_size);
if(cd == -1) {...}
tipo_msg = (char) 0;
if(recv(cd, &tipo_msg, 1, 0) == -1) {...}
buff_readed_aux = recv(cd, &buff, sizeof(buff), 0)));
printf("\n-> Recibida alta tema %s\n", buff);
If I inspect the memory buff_readed_aux
value is correct but the buffer is filled with trash.
Example of values I get on the prints:
Client: aviso-gestion-gema (7 bytes): nombre1.
Server: Recibida alta tema P�`
Client: aviso-gestion-gema (5 bytes): nom#2
Server: Recibida alta tema ��`
I don't understand whats happening, I have tried to use 'bzero' to initialize the buffer with no luck. I have confirmed with wireshark that the message is not being sending correctly from the server.
Tema is allocated in a hash table like this:
tema_name_length = strlen(utstring_body(readed));
char* allocated = malloc(tema_name_length+1); // 1+ for nul termination
strcpy(allocated, utstring_body(readed));
// store allocated in the hash-table
buff_readed_aux = recv(cd, &buff, sizeof(buff), 0)));
printf("\n-> Recibida alta tema %s\n", buff);
How are you expecting this printf
to know how many characters to print? Magic?
Try, for example:
if (buff_readed_aux > 0)
{
printf("\n-> Recibida alta tema ");
for (int i = 0; i < buff_readed_aux; ++i) putchar(buff[i]);
printf("\n");
}
Also:
else if(send_all(cd, &tema, tema_name_length, 0) == -1) { op_result = CALLBACK_TRANSM_ERROR; }
#ifdef DEBUG_MSG
fprintf(stderr, "aviso-gestion-gema (%d bytes): %s\n", tema_name_length, tema);
#endif
If tema
holds the address of what you want to send (as the fprintf
suggests), why are you passing the address of tema
to send_all
? You're supposed to pass send_all
the address of what you want to send, not the address of the thing that holds the address of what you want to send!