Salve a tutti! Per un progetto di Sistemi Operativi dobbiamo implementare una chat in Linux e di conseguenza un marshaller del buffer approppriato.
Ora questo sembra lavorare abbastanza bene, ma tende a tagliare gli ultimi 1-2 caratteri del messaggio, indifferentemente da quanto sia lungo questo. La struttura da marshallare comprende il tipo di messaggio da inviare, il mittente, il destinatario, la lunghezza del messaggio e il messaggio stesso. Specifico che il marshalling funziona perfettamente come si può vedere dalla stampa che il programma esegue (noterete che a numero uguale corrisponde carattere uguale). Quindi il problema è da ricercarsi nel demarshalling.
Includo sia la libreria che il relativo client.
LIBRERIA PROTOCOL.H
codice:
#ifndef _PROTOCOL
#define _PROTOCOL
#include <sys/types.h>
#include <netinet/in.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#define MSG_LOGIN 'L' /* login */
#define MSG_REGLOG 'R' /* registrazione e login */
#define MSG_OK 'O' /* ok */
#define MSG_ERROR 'E' /* errore */
#define MSG_SINGLE 'S' /* messaggio a singolo utente */ //ok
#define MSG_BRDCAST 'B' /* messaggio a tutti */ //ok
#define MSG_LIST 'I' /* lista */ //ok
#define MSG_LOGOUT 'X' /* logout */ //ok
#define MAX_LUNGHEZZA 256
typedef struct{
char type; /* tipo di messaggio */
char *sender; /* mittente */
char *receiver; /* destinatario */
unsigned int msglen; /* lunghezza */
char *msg; /* buffer */
} msg_t;
u_short * MARSHALLING(msg_t);
msg_t DEMARSHALLING(u_short *);
u_short * MARSHALLING(msg_t msg){
u_short * buffer;
int c=0;
int i=0;
buffer = (u_short *) malloc(sizeof(u_short)*(msg.msglen+strlen(msg.sender)+strlen(msg.receiver)));
buffer[c] = htons(msg.type);
c++;
buffer[c] = htons(strlen(msg.sender));
c++;
for(i=0;i<strlen(msg.sender);i++, c++) buffer[c] = htons(msg.sender[i]);
buffer[c] = htons(strlen(msg.receiver));
c++;
for(i=0;i<strlen(msg.receiver);i++, c++) buffer[c] = htons(msg.receiver[i]);
buffer[c] = htons(msg.msglen);
printf("La lunghezza del messaggio è %d\n", msg.msglen);
c++;
for(i=0;i<msg.msglen;i++, c++) buffer[c] = htons(msg.msg[i]);
return buffer;
}
msg_t DEMARSHALLING(u_short * buffer){
msg_t mex;
int i=0, c=0;
mex.type=ntohs(buffer[c]);
c++;
int lunghezza_sender=ntohs(buffer[c]);
mex.sender = (char *) malloc(sizeof(char)*lunghezza_sender);
c++;
for(i=0;i<lunghezza_sender;i++,c++) mex.sender[i] = ntohs(buffer[c]);
int lunghezza_receiver=ntohs(buffer[c]);
mex.receiver=(char *) malloc(sizeof(char)*lunghezza_receiver);
c++;
for(i=0;i<lunghezza_receiver;i++,c++) mex.receiver[i] = ntohs(buffer[c]);
mex.msglen = ntohs(buffer[c]);
printf("La lunghezza del messaggio è %d\n", mex.msglen);
mex.msg = (char*) malloc(sizeof(char)*mex.msglen);
c++;
for(i=0;i<mex.msglen;i++,c++) mex.msg[i] = ntohs(buffer[c]);
return mex;
}
#endif
CLIENT PROTOCOL_CLIENT.C
codice:
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <stdlib.h>
#include <netinet/in.h>
#include "protocol.h"
int main(){
int i;
msg_t mex;
char * mittente=(char*)malloc(sizeof(char)*256);
char * destinatario=(char*)malloc(sizeof(char)*256);
char * messaggio=(char*)malloc(sizeof(char)*256);
strcpy(mittente, "bimbo1989");
strcpy(destinatario, "tromb35");
strcpy(messaggio, "Ciao Mattia! Sono bello e sexy! LOL!");
mex.sender = (char *) malloc(sizeof(char)*strlen(mittente));
mex.receiver = (char*) malloc(sizeof(char)*strlen(destinatario));
mex.msg = (char *) malloc(sizeof(char)*strlen(messaggio));
mex.type = MSG_SINGLE;
strcpy(mex.sender, mittente);
strcpy(mex.receiver, destinatario);
strcpy(mex.msg, messaggio);
mex.msglen = strlen(mex.msg);
u_short * marshallato;
marshallato = MARSHALLING(mex);
for(i=0;marshallato[i]!=0;i++) printf("%d\n", marshallato[i]);
msg_t prova = DEMARSHALLING(marshallato);
printf("%s\n", prova.sender);
printf("%s\n", prova.receiver);
printf("%s\n", prova.msg);
return 0;
}