nntp2http.com
Posting
Suche
Optionen
Hilfe & Kontakt

Re: read & write interi da socket TCP

Von: Manlio Perillo (manlio_perillono@spamlibero.it) [Profil]
Datum: 02.03.2008 17:36
Message-ID: <VHAyj.26502$FR.128855@twister1.libero.it>
Newsgroup: it.comp.os.linux.development
Il Sun, 02 Mar 2008 08:22:33 -0800, ale.ber ha scritto:

> 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));
>

Qui c'è un errore perchè leggi sempre sizeof(int) bytes, invece
devi
leggere solo il numero di bytes restanti.

> 		if (just_written == -1) {
> 			return -1;
> 		}
>
> 		written += just_written;
>
> 		printf("Written: %d/%d\n", just_written, written);
>
> 	} while ((just_written > 0) && (written < sizeof(int)));

Il controllo just_written > 0 è inutile, dato che se è uguale a
-1, tu
esci dalla funzione.

>
> 	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));

Come per la write, qui devi leggere solo il numero di bytes che ti
mancano.

>
> 		if (just_readed == -1) {
> 			return -1;
> 		}
>
> 		readed += just_readed;
>
> 		printf("Readed: %d/%d\n", just_readed, readed);
>
> 	} while ((just_readed > 0) && (readed < sizeof(int)));

Come per la write, il controllo just_readed > 0 è inutile.

>
> 	return readed;
> }



Manlio Perillo

[ Auf dieses Posting antworten ]

Antworten