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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
| /* Program client */
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/types.h>
#define fichier "fichier_emis.txt"
int port = 12345;
long fsize(FILE*fp);
//////////////////////////////////////////////////////////////////
// //
// fonction Fsize //
// //
//////////////////////////////////////////////////////////////////
long fsize(FILE*fp)
{
long pos,size;
pos=ftell(fp); /*sauve la position courante*/
fseek(fp,0,SEEK_END);/*aller en fin*/
size=ftell(fp);/*lire la taille*/
fseek(fp,pos,SEEK_SET);/*revenir à la position initiale*/
return size;
}
//////////////////////////////////////////////////////////////////ss
// //
// Fonction Principale //
// //
//////////////////////////////////////////////////////////////////
int main(int argc,char *argv[])
{
/*DECLARATION DES STRUCTURES ET DES VARIABLES*/
struct sockaddr_in to;
int sock_descriptor;
int temp_sock_descriptor;
FILE*fp_emis;
int longueur=0;
char buf[300];
/*CREATION DE LA SOCKET*/
sock_descriptor = socket(AF_INET, SOCK_STREAM, 0);
if (sock_descriptor ==-1)
{
perror("call to socket\n");
exit(1);
}
printf("socket ok\n");
/**/
bzero(&to,sizeof(to));
to.sin_family=AF_INET;
to.sin_addr.s_addr=inet_addr("10.102.13.114");
to.sin_port=htons(port);
printf("connexion wait...\n");
/*CONNEXION*/
if (connect(sock_descriptor,
(struct sockaddr*)&to,
sizeof(to))==-1)
{
perror("call to connect\n");
exit(1);
}
printf("connect ok\n");
/*ouverture du fichier*/
fp_emis=fopen(fichier,"r");
if (fp_emis==NULL)
{
perror("call to open\n");
exit(1);
}
/*envoi de la taille du fichier*/
int taille =fsize(fp_emis);
longueur=sizeof(taille);
int env_taille=send(sock_descriptor, &taille, longueur,0);
if (env_taille==-1)
{
perror("call to send\n");
exit(1);
}
printf("envoi ok\n");
/*tant qu'on n'a pas tout envoyé, tant qu'on n'a pas atteint la fin du fichier*/
fseek(fp_emis,0,SEEK_SET);
while (fgets(buf,sizeof buf,fp_emis)!= NULL)
{
/*on envoi*/
int envoi=send(sock_descriptor, buf, strlen(buf),0);
if (envoi==-1)
{
perror("call to send\n");
exit(1);
}
printf("envoi paquet\n");
}
/*si une erreur s'est produite*/
/* if (ferror(fp_emis))
{
/* out */
/* perror (fichier);
}
/*fermeture du fichier*/
fclose(fp_emis);
/*close*/
close(temp_sock_descriptor);
exit(0);
} |
Partager