nntp2http.com
Posting
Suche
Optionen
Hilfe & Kontakt

Re: read & write interi da socket TCP

Von: ale.ber (ale.beer@gmail.com) [Profil]
Datum: 02.03.2008 17:22
Message-ID: <af30e569-38b1-4ff9-bf6b-97f646d2fd42@c33g2000hsd.googlegroups.com>
Newsgroup: it.comp.os.linux.development
Ora ho sistemato un po i puntatori.. cosa ne pensi? Ora il tutto
funziona.
Immagino che in condizioni normali una cosa del genere non serva, ma
il codice che scrivo è per un corso di Network Programming, quindi mi
devo fare le paranoie anche su queste cose. ^^

/*
* Try to write the entire message on the socket
*/
int sockwrite(int sock, int mess) {
int written = 0;
int just_written = 0;

char *p = (char *) &mess;

do {
just_written = write(sock, p + written, sizeof(int));

if (just_written == -1) {
return -1;
}

written += just_written;

printf("Written: %d/%d\n", just_written, written);

} while ((just_written > 0) && (written < sizeof(int)));

return written;
}

/*
* Try to read the entire message from the socket
*/
int sockread(int sock, int *mess) {
int readed = 0;
int just_readed = 0;

char *p = (char *) mess;

do {
just_readed = read(sock, p + readed, sizeof(int));

if (just_readed == -1) {
return -1;
}

readed += just_readed;

printf("Readed: %d/%d\n", just_readed, readed);

} while ((just_readed > 0) && (readed < sizeof(int)));

return readed;
}

[ Auf dieses Posting antworten ]

Antworten