1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
| /* By: *censored* Created: 04/03/05 10:39:32 */
/* client2.c */
/* Exemple 2 Client s'appuyant sur TCP */
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <unistd.h>
// DE : il manque des include pour les sockets et pour sleep.
#define DATA "hello, je suis ton premier client, teste si tu me copies correctement !"
#define MESS "\n reading message"
/* Usage: pgm host port nbmss pause*/
//DE : le code date de 2005 ...
main( argc,argv )
int argc;
char *argv[];
{
int sock;
struct sockaddr_in server;
struct hostent *hp;
int rval,nbmss;
char buf[292],c='-',i;
if (argc != 5){
printf("usage: client2 host port nbmss pause \n");
exit(1);
}
/* Create Socket */
//DE: on confond allegrement AF_INET et PF_INET
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == -1) {
perror("opening stream socket");
exit(1);
}
/* Connect socket */
//bzero : fonction deprecié.
bzero(&server, sizeof(server));
server.sin_family = AF_INET;
hp = gethostbyname(argv[1]);
if (hp == (struct hostent*) 0) {
//DE: pour gethostbyname, on utilise herror generalement.
//DE: semblerait que herror existe pas sous windows. j'ai rien dis :p
fprintf(stderr, "%s: unknown host\n", argv[1]);
exit(2);
}
//DE: bcopy: fonction depreciée non?
bcopy(hp->h_addr,&server.sin_addr.s_addr,hp->h_length);
//DE: atoi c'est pas deprecié? (ironique)
server.sin_port = htons(atoi(argv[2]));
if (connect(sock, (struct sockaddr*) &server, sizeof(server)) == -1) {
perror ("connecting stream socket");
exit(1);
}
//DE: on utilise write: pas portable ... send ca aurait ete tellement plus elegant !
if (write(sock, DATA, sizeof(DATA)) == -1)
perror("writing on stream socket");
memset(buf, 0, sizeof (buf));
//DE: meme remarque que pour write.
rval=read(sock, buf, sizeof (buf));
if (rval == -1)
perror("reading string message");
write(1,buf,rval);
//DE: *edit* j'ai rien dis :X
nbmss=atoi(argv[3]);
write(sock, &nbmss, sizeof(nbmss));
write(1,MESS,sizeof(MESS));
do {
rval=read(sock, buf, sizeof (buf));
if (rval == -1)
perror("reading string message");
for (i=1;i<=atoi(argv[4]);i++){
//DE: ca semble etre la maniere la plus elegante qu'il ait trouvé pour afficher des '-' sur la console.
sleep(1);
write(1,&c,1);
}
}while(rval>0);
printf("\n closing socket \n");
close(sock);
exit(0); //DE: j'aurais aimé un beau return EXIT_SUCCESS;
} |
Partager