codice:
typedef struct {
char *buf;
uint32_t valore;
struct timeval sec_istcreazione;
int inviato;
struct elemento * next;
} elemento;
typedef struct {
elemento * intesta;
elemento * incoda;
} coda;
int Readn(int fd, char *ptr, int nbytes)
{
int nleft,nread;
nleft=nbytes;
while(nleft>0)
{
do {
nread=read(fd,ptr,nleft);
} while ( (nread<0) && (errno==EINTR) );
if(nread<0)
{ /* errore */
char msg[2000];
sprintf(msg,"Readn: errore in lettura [result %d] :",nread);
perror(msg);
return(-1);
}
else
{
if(nread==0) {
return(0);
break;
}
}
nleft -= nread;
ptr += nread;
}
return(nbytes);
}
void inCoda(char * pkt,uint32_t valor,struct timeval instcreaz, coda * val){
elemento * new_el = (elemento*)malloc(sizeof(elemento));
struct timeval send;
int i;
uint32_t valore;
new_el->buf = (char*)malloc(sizeof(char) * 100);
printf("%d bytes totali\n", sizeof(uint32_t)*100);
memcpy(new_el->buf,pkt,100);
printf("%d\n",sizeof(new_el->buf));
memcpy(&valore,new_el->buf,4);
new_el->valore=valor;
printf("new_el->valore=valor; %d\n",new_el->valore);
new_el->sec_istcreazione=instcreaz;
new_el->inviato = 0;
printf("new_el->inviato %d\n",new_el->inviato);
memcpy(&send,(char*)(new_el->buf + 4), sizeof(struct timeval) );
printf("leggo pkt %u e lo scrivo in memoria, sec %ld ,msec %3ld\n",valore,send.tv_sec,send.tv_usec);
new_el->next=NULL;
if (codaVuota(val)) {
val->intesta=new_el;
val->incoda=new_el;
} else {
val->incoda->next=new_el;
val->incoda=new_el;
}
}
main...
else{
char * buffer_new;
n=Readn(newsockfd,&buf,PKTSIZE);
if(n!=PKTSIZE){printf("pktsize non corretta\n"); exit(1);}
buffer_new = buf;
idletto = buf[0];
memcpy( &send, &buf[1], sizeof(struct timeval) );
printf("leggo pkt %u e lo scrivo in memoria, sec %ld ,msec %3ld\n",idletto,send.tv_sec,send.tv_usec);
fflush(0);
printf("incoda\n");
inCoda(buffer_new,idletto,send,&p);
}
....
mentre il la spedizione avviene con:
codice:
int Sendn (int fd, const void *buf, int n)
{
size_t nleft;
ssize_t nwritten;
char *ptr;
ptr = (void *)buf;
nleft = n;
while (nleft > 0)
{
if ( (nwritten = send(fd, ptr, nleft, MSG_NOSIGNAL) ) < 0) {
if (errno == EINTR) nwritten = 0; /* and call write() again*/
else {
int myerrno;
char msg[2000];
myerrno=errno;
sprintf(msg,"send failed: [nwritten %d] :",nwritten);
errno=myerrno;
perror(msg);
fflush(stderr);
return(-1); /* error */
}
}
nleft -= nwritten;
ptr += nwritten;
}
return(n);
}
main...
uint32_t buf[100],
memset((char*)buf,0,100);
buf[0]=idmsg;
gettimeofday(&sent,NULL);
memcpy( (char*)&(buf[1]), (char*)&sent, sizeof(struct timeval) );
ris=Sendn(LBmobilefd, (char*)buf, 100 );
...
nella print del main funziona tutto bene invece nella funzione mi da quel problema