LOVE85
01-04-2007, 11:13
Salve ragazzi il professore di Reti ci ha dato questo file per mail e volevo capire perchè non funziona bene. Io uso Visual Studio 6.0 c++ con MSDN Library per Visual Studio 6.0 che son due CD installato. Perchè non la trova? Ho fatto cerca ma niente risultato. Dove posso trovare questa libreria? So che è inclusa in types.h ma ce l'ho. Come faccio?
Ecco il codice
-------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <string.h>
#include <errno.h>
#include <netdb.h>
#define messaggio 51
#define sol 3
//stampa comandi.
void elencoOrdini(){
puts("");
puts("Inserire i comandi:");
puts("ADD ----> il server aggiunge la stringa.");
puts("LIST ----> il server restituisce le stringhe.");
puts("DEL ----> il server cancella la stringa.");
puts("");
return;
}
//controllo sintassi.
int fun_Ordine(char *ordine){
if( (strncmp(ordine,"ADD",3)!=0) && (strncmp(ordine,"LIST",4)!=0) && (strncmp(ordine,"DEL",3)!=0) ){
puts("\n\nComando non presente");
elencoOrdini();
return 0;
}
else return 1;
}
int main(int argc, char* argv[]){
struct sockaddr_in S;
struct hostent *h;
int returnSocket,returnC,returnW,returnR,numStringhe;
int nwrite,nread,len,k,porta=6660;
char buffer[messaggio];
char ordine[messaggio];
memset((void *)&S,0,sizeof(S));
//controllo parametri (comando_host).
if(argc!=4){
puts("Errore, mancano dei parametri");
// puts("Usage: ./client ipServer portaServer ordine");
// puts("NB. Comando deve essere di questo tipo: ADDparola o DELparola o LIST.");
exit(EXIT_FAILURE);
}
if ((h=gethostbyname(argv[1]))==NULL){
herror("Errore in gethostbyname");
exit(1);
}
porta = atoi(argv[2]);
strncpy(ordine,argv[3],messaggio-1);
//terminatore in ultima posizione
ordine[messaggio-1] = '\0';
if(!fun_Ordine(ordine))
return 0;
//dati connessione.
S.sin_family = AF_INET;
S.sin_port = htons(porta);
S.sin_addr.s_addr =inet_addr(inet_ntoa(*((struct in_addr*)h->h_addr)));
//apertura socket
returnSocket = socket(AF_INET,SOCK_STREAM,0);
if(returnSocket==-1){
perror("\nErrore nella creazione del socket");
exit(EXIT_FAILURE);
}
puts("Socket creata");
//collegamento con server
returnC=connect(returnSocket,(struct sockaddr *)&S,sizeof(S));
if(returnC==-1){
perror("Errore di connessione");
exit(EXIT_FAILURE);
}
puts("Socket collegata");
// Lettura risposta del server --> 'OK'
nread=0;
while( (sol>nread) && ((returnR=read(returnSocket,&(buffer[nread]),(sol-1)-nread )) > 0) ){
nread += returnR;
}
buffer[sol-1] = '\0';
printf("\nRisposta del server: ");
puts(buffer);
len = strlen(ordine);
// Padding
if(len < messaggio-1){
for(k=len;k<messaggio-1;k++)
ordine[k] = '+';
}
//terminatore \0 in comando[50]
ordine[messaggio-1] = '\0';
// Scrittura
len = strlen(ordine);
nwrite=0;
printf("Stringa inviata: %s\n",ordine);
while ( (returnW=write(returnSocket,&(ordine[nwrite]),(messaggio-1)-nwrite)) > 0){
nwrite+=returnW;
}
puts("Eseguita scrittura");
//scritto comando
if(strncmp(ordine,"LIST",4)==0){
nread = 0;
//leggo numero strighe server.
while((returnR = read(returnSocket,&(buffer[nread]),(messaggio-1)-nread))>0){
nread += returnR;
}
numStringhe = atoi(buffer);
printf("Il numero di stringhe è : %d\n",numStringhe);
printf("\nSTRINGHE OTTENUTE");
//leggo le stringhe.
k=1;
//leggo le stringhe inviate dal server.
while(numStringhe > 0){
nread = 0;
while( (returnR = read(returnSocket,&(buffer[nread]),(messaggio-1)-nread)) >0 ){
nread += returnR;
}
buffer[messaggio-1] = '\0';
printf("\n%d) %s",k,buffer);
numStringhe--;
k++;
}
// printf("\n**********************************\n\n");
}
//Socket chiusa.
close(returnSocket);
return EXIT_SUCCESS;
}
----------Fine Codice--------------
Ho sbalgiato a fare qualcosa?
Ecco il codice
-------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <string.h>
#include <errno.h>
#include <netdb.h>
#define messaggio 51
#define sol 3
//stampa comandi.
void elencoOrdini(){
puts("");
puts("Inserire i comandi:");
puts("ADD ----> il server aggiunge la stringa.");
puts("LIST ----> il server restituisce le stringhe.");
puts("DEL ----> il server cancella la stringa.");
puts("");
return;
}
//controllo sintassi.
int fun_Ordine(char *ordine){
if( (strncmp(ordine,"ADD",3)!=0) && (strncmp(ordine,"LIST",4)!=0) && (strncmp(ordine,"DEL",3)!=0) ){
puts("\n\nComando non presente");
elencoOrdini();
return 0;
}
else return 1;
}
int main(int argc, char* argv[]){
struct sockaddr_in S;
struct hostent *h;
int returnSocket,returnC,returnW,returnR,numStringhe;
int nwrite,nread,len,k,porta=6660;
char buffer[messaggio];
char ordine[messaggio];
memset((void *)&S,0,sizeof(S));
//controllo parametri (comando_host).
if(argc!=4){
puts("Errore, mancano dei parametri");
// puts("Usage: ./client ipServer portaServer ordine");
// puts("NB. Comando deve essere di questo tipo: ADDparola o DELparola o LIST.");
exit(EXIT_FAILURE);
}
if ((h=gethostbyname(argv[1]))==NULL){
herror("Errore in gethostbyname");
exit(1);
}
porta = atoi(argv[2]);
strncpy(ordine,argv[3],messaggio-1);
//terminatore in ultima posizione
ordine[messaggio-1] = '\0';
if(!fun_Ordine(ordine))
return 0;
//dati connessione.
S.sin_family = AF_INET;
S.sin_port = htons(porta);
S.sin_addr.s_addr =inet_addr(inet_ntoa(*((struct in_addr*)h->h_addr)));
//apertura socket
returnSocket = socket(AF_INET,SOCK_STREAM,0);
if(returnSocket==-1){
perror("\nErrore nella creazione del socket");
exit(EXIT_FAILURE);
}
puts("Socket creata");
//collegamento con server
returnC=connect(returnSocket,(struct sockaddr *)&S,sizeof(S));
if(returnC==-1){
perror("Errore di connessione");
exit(EXIT_FAILURE);
}
puts("Socket collegata");
// Lettura risposta del server --> 'OK'
nread=0;
while( (sol>nread) && ((returnR=read(returnSocket,&(buffer[nread]),(sol-1)-nread )) > 0) ){
nread += returnR;
}
buffer[sol-1] = '\0';
printf("\nRisposta del server: ");
puts(buffer);
len = strlen(ordine);
// Padding
if(len < messaggio-1){
for(k=len;k<messaggio-1;k++)
ordine[k] = '+';
}
//terminatore \0 in comando[50]
ordine[messaggio-1] = '\0';
// Scrittura
len = strlen(ordine);
nwrite=0;
printf("Stringa inviata: %s\n",ordine);
while ( (returnW=write(returnSocket,&(ordine[nwrite]),(messaggio-1)-nwrite)) > 0){
nwrite+=returnW;
}
puts("Eseguita scrittura");
//scritto comando
if(strncmp(ordine,"LIST",4)==0){
nread = 0;
//leggo numero strighe server.
while((returnR = read(returnSocket,&(buffer[nread]),(messaggio-1)-nread))>0){
nread += returnR;
}
numStringhe = atoi(buffer);
printf("Il numero di stringhe è : %d\n",numStringhe);
printf("\nSTRINGHE OTTENUTE");
//leggo le stringhe.
k=1;
//leggo le stringhe inviate dal server.
while(numStringhe > 0){
nread = 0;
while( (returnR = read(returnSocket,&(buffer[nread]),(messaggio-1)-nread)) >0 ){
nread += returnR;
}
buffer[messaggio-1] = '\0';
printf("\n%d) %s",k,buffer);
numStringhe--;
k++;
}
// printf("\n**********************************\n\n");
}
//Socket chiusa.
close(returnSocket);
return EXIT_SUCCESS;
}
----------Fine Codice--------------
Ho sbalgiato a fare qualcosa?