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
| #include <math.h>
#include <pthread.h>
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h> //socket
#include <netinet/in.h>
#include <netdb.h>
#define REMOTE_ADDR "10.0.2.15"
#define REMOTE_PORT 7777
int i=0;
void my_pthread_create(pthread_t *tid, const pthread_attr_t *attr, void*(*start)(void*), void *arg)
{
if(pthread_create(tid, attr, start, arg))
{
perror("pthread_create ");
exit(EXIT_FAILURE);
}
}
void my_pthread_join(pthread_t thread, void **value_ptr)
{
if(pthread_join(thread, value_ptr))
{
perror("pthread_join ");
exit(EXIT_FAILURE);
}
}
void *thread_cpt(void *p)
{
int *cpt = p;
*cpt = *cpt +1;
printf("Thread : %d\n", *cpt);
pthread_exit(NULL);
}
void Choix()
{
i=i+1;
printf("1.Consultation\n"
"2.Réservation\n"
"3.Quitter\n");
}
void Consultation(void s)
{
int *socket = s;
char message[2048] = "1";
send(*socket , message , strlen(message) , 0);
char server_reply[2048];
recv(*socket, server_reply, 2048, 0);
puts(server_reply);
printf("Afin de réserver reconnecter vous puis choisissez \"réserver\"\n");
close(*socket);
}
int main(void)
{
/*int cpt = 0;
pthread_t thread1, thread2;
printf("Creation threads\n");
pthread_create(&thread1, NULL, thread_cpt, &cpt);
pthread_create(&thread2, NULL, thread_cpt, &cpt);
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
printf("Execution terminee\n");*/
pthread_t thread1, thread2;
struct sockaddr_in sa;
int s;
sa.sin_family = AF_INET;
sa.sin_addr.s_addr = inet_addr(REMOTE_ADDR);
sa.sin_port = htons(REMOTE_PORT);
s = socket(AF_INET, SOCK_STREAM, 0);
connect(s, (struct sockaddr *)&sa, sizeof(sa));
int c;
if (pthread_create(&thread1, NULL, (void *(*)())Choix, NULL) == -1)
perror ("pb pthread_create\n");
printf("Entrez votre choix:");
c = getchar();
if(c != '\n' && c != EOF)
{
int d;
while((d = getchar()) != '\n' && d != EOF);
}
switch(c)
{
case '1':
printf("ok");
pthread_create(&thread1, NULL, Consultation(), &s);
break;
case '2':
printf("ok");
//Reservation(s);
break;
case '3':
exit(EXIT_SUCCESS);
break;
default:
printf("Choix errone\n");
}
return 0;
} |
Partager