|
|
|
![]() |
|
Strumenti |
![]() |
#1 |
Senior Member
Iscritto dal: Oct 1999
Città: Milano
Messaggi: 531
|
problema socket!:(
salve ragazzi, ho scritto due semplici programmini, un client e un server:
il client deve leggere al momento della connessione una stringa da un server....solo che non riesco a connettermi!! qualcuno può aiutarmi a trovare l errore? vi posto il codice: Codice:
#include <errno.h> #include <stdio.h> #include <unistd.h> #include <sys/socket.h> #include <sys/types.h> #include </mnt/windows/progetto/netio.h> #include <string.h> #include <netdb.h> #include <arpa/inet.h> #include <signal.h> #define PORTNUM 50000 #define MAXNAME 20 #define MAXHOSTNAME 30 /*--------------------- crea un socket che accetterà la prima parte della connessione di un client Ritorna il descrittore del socket creatO ----------------------------------------*/ int creacon(u_short portnum); /*------------------------ Routine che crea un accept per avere poi il descrittore di un secondo socket sul quale verrà stabilita la connessione.la chiamata è sospensiva --------------------------*/ int attendicon(int s); /*------------------------ esempio di dialogo tra server e client ---------------------------*/ int gestisciclient(int sock_fd); /*-------------------------- gestione di stringhe ----------------------------*/ int main(){ int s,t; int tid[10]; char nome[MAXHOSTNAME+1]; char indirizzo[MAXNAME+1]; if ( (s = creacon(PORTNUM)) < 0){ perror("errore:prima connessione"); exit(1); } gethostname(nome,MAXHOSTNAME); printf("il localhost : %s ",nome); printf("tutto apposto aspetto che si connette un client\n"); while(1){ if ((t=attendicon(s))<0){ if (errno==EINTR){ continue; } perror("accept"); exit(1); } if (gestisciclient(t)<0){ perror("errore nel write"); close(t); continue; } close(t); exit(0); } } int creacon(u_short portnum){ char nome[MAXHOSTNAME+1]; //nome del socket; int s; //descrittore del socket struct sockaddr_in sa; //struttura bzero(&sa,sizeof(sa)); //AZZERO SA sa.sin_family = AF_INET; sa.sin_port = htons(portnum); if ((s=socket(AF_INET,SOCK_STREAM,0))< 0) return(-1); if (bind(s,(struct sockaddr*)&sa,sizeof(struct sockaddr))==-1){ close(s); return(-1); } listen(s,3); return(s); } int attendicon(int s){ struct sockaddr_in isa; //struttura dove andrà l indirizzo int i ; //dimensione dell'isa int t ; //socket dell'accpet i=sizeof(isa); if (( t = accept(s,(struct sockaddr *)&isa,&i)) < 0 ) return(-1); return(t); } int gestisciclient(int sock_fd){ char buffer[MAXNAME+1]; printf("sono entrato??!?\n"); strcpy(buffer,"messaggio"); if (FullWrite(sock_fd,buffer,sizeof(buffer))<0){ perror(" errore nelle write di spedizione!\n"); return(-1); } return(0); } questo è il server, il client invece...: #include <errno.h> #include <sys/types.h> /* predefined types */ #include <unistd.h> /* include unix standard library */ #include <arpa/inet.h> /* IP addresses conversion utilities */ #include <sys/socket.h> /* socket library */ #include <stdio.h> /* include standard I/O library */ #include </mnt/windows/progetto/netio.h> /*personale mia libreria*/ #include <netdb.h> #define MAXLINE 80 #define BACKLOG 10 #define PORTNUM 50000 int socket_call(char *hostname, u_short portnum); int main(int argc,char *argv[]){ int sock_fd; char buffer[MAXLINE+1]; if (argc!=2){ printf("usage: %s <hostname>\n",argv[0]); exit(-1); } printf("DEBUG: ho inserito: %s\n",argv[1]); printf("CLIENT: provo a connettermi\n"); if ((sock_fd=socket_call(argv[1],PORTNUM)) == -1){ perror("socket_call"); exit(-1); } printf("la connessione è stata effettuata ora leggo la parola inviatami dal server\n"); printf("leggo la stringa inserita nel server!!\n"); if (FullRead(sock_fd,buffer,5)<0){ perror("fullwrite"); exit(-1); } getchar(); exit(0); } int socket_call(char *hostname, u_short portnum){ int sock_fd ; struct sockaddr_in serv_add ; int temp; struct hostent *hp; /* creo il socket sul quale farò la connessione */ if ((sock_fd=socket(AF_INET,SOCK_STREAM,0))< 0){ perror(" Socket creation error "); return(-1); } /* inizializzo serv_add */ memset (( void *) & serv_add , 0 , sizeof ( serv_add )); /* clear */ serv_add . sin_family = AF_INET ; /* indirizzo type : INET */ serv_add . sin_port = htons (13); /* daytime post is 13 */ /* build address using inet_pton */ if ((temp=inet_pton (AF_INET , hostname , &serv_add.sin_addr )) <= 0){ if (temp == 0){ printf("non ho trovato una stringa valida!!\n"); } perror (" Address creation error "); return(-1); } /* extablish connection */ if (connect( sock_fd , (struct sockaddr *)&serv_add , sizeof (serv_add)) < 0){ perror (" Connection error "); return(-1); } return(sock_fd); /*effettuata la connessione!!*/ } dopo aver compilato mando il server in esecuzion che aspetta sull'accept, ma quando mando in esecuzione il client non va! come indirizzo metto quello del localhost... ![]()
__________________
Vintage signature: Abit nf7-s, Xp 2500+@2300mhz, 256 mb 418mhz cas2, geforce fx5700,S-ATA 160 Maxtor,Lg gsa 4120b...prima..ma ora..DELL INSPIRON 6400, ATI X1300... ![]() Ultima modifica di Ricky : 15-01-2005 alle 11:25. |
![]() |
![]() |
![]() |
#2 |
Senior Member
Iscritto dal: Oct 1999
Città: Milano
Messaggi: 531
|
forse ho capito dov è il problema:
nell uso dell' "INET_PTON" dovrei riuscire a convertire una stringa di caratteri che rappresenta l'hostname in un indirizzo che verrà memorizzato nella struttura sockaddr. il problema è che mi da un errore: mi dice che la stringa non è valida!! nella chiamata a: Codice:
if ((temp=inet_pton (AF_INET , hostname , &serv_add.sin_addr )) < 0){ if (temp == 0){ printf("non ho trovato una stringa valida!!\n"); } perror (" Address creation error "); return(-1); } aiutooooooo ![]()
__________________
Vintage signature: Abit nf7-s, Xp 2500+@2300mhz, 256 mb 418mhz cas2, geforce fx5700,S-ATA 160 Maxtor,Lg gsa 4120b...prima..ma ora..DELL INSPIRON 6400, ATI X1300... ![]() |
![]() |
![]() |
![]() |
Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 16:58.