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
|
int main()
{
int hSocket,ret;
FILE *P_FICHIER;
FILE *P_FICH;
char* nf = "MessagesClient.txt";
char buf[BUFSIZ],IP[BUFSIZ];
struct hostent *infosOther,*infosHost;
struct in_addr adresseIPP,adresseIP;
struct sockaddr_in adresseSocketClient,adresseSocketClientCible;
unsigned int tailleSockaddr_in;
char msgClient[MAXSTRING];
int POR;
int rep = 0;
//Création de la socket
if((hSocket = socket(AF_INET,SOCK_DGRAM,0))==-1)
{
printf("Erreur: %d",errno);
exit(1);
}
else
printf("creation de la socket reussie \n",hSocket);
//création du thread de reception de message
ret = pthread_create(&threadHandle,NULL,fctThread,(void*)hSocket);
puts("thread lance");
ret = pthread_detach(threadHandle);
//getchar();
do
{ rep = menu();
switch(rep)
{
case 1:
if((infosHost = gethostbyname("localhost"))==0)
{
printf("errreur aquisition infos du host");
}
else
printf("aquisition infos du host OK :-))\n");
memcpy(&adresseIP,infosHost->h_addr,infosHost->h_length);
//préparation de sockaddr_in
tailleSockaddr_in = sizeof(struct sockaddr_in);
memset(&adresseSocketClient,0,tailleSockaddr_in);//init
adresseSocketClient.sin_family = AF_INET;
memcpy(&adresseSocketClient.sin_addr,infosHost->h_addr,infosHost->h_length);
/*if(bind(hSocket,(struct sockaddr*)&adresseSocketClient,sizeof(struct sockaddr_in))==-1)
{
printf("Erreur au niveau du bind de la socket Client %d\n",errno);
exit(1);
}
else
printf("Bind reussi\n");
*/
printf("IP host:%s \n",inet_ntoa(adresseIP));
printf("num port %d \n",ntohs(adresseSocketClient.sin_port));
printf("inserer l'ip du destinataire:");
scanf("%s",IP);
printf("inserer son numero de port:");
scanf("%d",&POR);
// acquisition des infos du client cible;
if((infosOther = gethostbyname(IP))==0)
{
printf("errreur aquisition infos du client cible");
}
else
printf("aquisition infos du client cible OK :-))\n");
memcpy(&adresseIPP,infosOther->h_addr,infosOther->h_length);
//préparation de sockaddr_in
tailleSockaddr_in = sizeof(struct sockaddr_in);
memset(&adresseSocketClientCible,0,tailleSockaddr_in);//init
adresseSocketClientCible.sin_family = AF_INET;
adresseSocketClientCible.sin_port = htons(POR);
memcpy(&adresseSocketClientCible.sin_addr,infosOther->h_addr,infosOther->h_length);
printf("IP client cible :%s \n",inet_ntoa(adresseIPP));
//send vers le client cible
fflush(stdin);
printf("Message a envoyer:\n");
gets(msgClient);
if(sendto(hSocket,msgClient,MAXSTRING,0,&adresseSocketClientCible,tailleSockaddr_in)==-1)
{
printf("Erreur sur le send\n");
exit(1);
}else printf("send OK \n");
break; |
Partager