nodus | C'est un problème bête mais je suis en train de faire un petit prog réseau et j'obtiens cette erreur:
Service 20000 demande a debian
Type d adresse 2 ; descripteur de socket 3
Reponse : message recu$ûÿ¿1 Mon J
|
Pourquoi ne reçois-je pas simplement "message reçu" ?
Code :
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <netdb.h>
- #include <stdlib.h>
- #define BUFSIZE 200
- #define NORMAL 0
- int main(int argc, char ** argv)
- {
- int s;
- int len;
- struct sockaddr_in sa;
- struct hostent * hp;
- struct servent * sp;
- char * myname;
- char buf[BUFSIZE];
- char *host;
- myname=argv[0];
- if(argc!=2){
- fprintf(stderr,"Usage : %s serveur\n",myname);
- exit(EXIT_FAILURE);
- }
- host=argv[1];
- if((hp = gethostbyname(host))==NULL){
- fprintf(stderr,"%s : %s serveur inconnu\n",myname,host);
- exit(EXIT_FAILURE);
- }
- bcopy((char*)hp->h_addr,(char*)&sa.sin_addr,hp->h_length);
- sa.sin_family = hp->h_addrtype;
- sa.sin_port=20000;
- if((s=socket(hp->h_addrtype,SOCK_STREAM,0))<0){
- perror("socket" );
- exit(EXIT_FAILURE);
- }
- fprintf(stdout,"Service %d demande a %s\n",sa.sin_port,host);
- fprintf(stdout,"Type d adresse %d ; descripteur de socket %d \n",sa.sin_family,s);
- if(connect(s,(struct sockaddr*)&sa,sizeof(sa))<0){
- perror("connect" );
- exit(EXIT_FAILURE);
- }
- recv(s,buf,BUFSIZE,NORMAL);
- fprintf(stdout,"Reponse : %s\n",buf);
- close(s);
- exit(EXIT_SUCCESS);
- }
|
Code :
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <netdb.h>
- #include <stdlib.h>
- #define BUFSIZE 200
- #define NORMAL 0
- #define BACKLOG 1
- #define MAXHOSTNAME 32
- #define MAXREQ 5
- void sendmess(int sock);
- int main(int argc, char ** argv)
- {
- int s,t;
- int i, count_req;
- struct sockaddr_in sa;
- struct sockaddr_in isa;
- struct hostent *hp;
- struct hostent *sp;
- char localhost[MAXHOSTNAME+1];
- sa.sin_port=20000;
- gethostname(localhost,MAXHOSTNAME);
- hp=gethostbyname(localhost);
- bcopy(hp->h_addr,(char*)&sa.sin_addr,hp->h_length);
- sa.sin_family=hp->h_addrtype;
- if((s = socket(hp->h_addrtype,SOCK_STREAM,0))<0){
- perror("Serveur : probleme creation socket\n" );
- exit(EXIT_FAILURE);
- }
- if(bind(s,(struct sockaddr *)&sa,sizeof(sa))<0){
- fprintf(stderr,"Serveur : probleme creation lien\n" );
- exit(EXIT_FAILURE);
- }
- listen(s,BACKLOG);
- fprintf(stdout,"Service %d sur %s en attente\n", sa.sin_port,localhost);
- fprintf(stdout,"type d adresse : %d\n",sa.sin_family);
- for(count_req = 0; count_req <= MAXREQ; count_req++){
- t=accept(s,(struct sockaddr *)&isa,&i);
- fprintf(stdout,"Requete %d\n",count_req);
- sendmess(t);
- close(t);
- }
- close(s);
- fprintf(stdout,"in du service pour %s\n", localhost);
- return(EXIT_SUCCESS);
- }
- void sendmess(int sock)
- {
- char * buf = "message recu";
- send(sock,buf,strlen(buf),NORMAL);
- }
|
|