· 7 years ago · Mar 01, 2018, 12:34 PM
1#define EXIT_FAILURE 1
2#define PORT 1337
3#include <stdio.h>
4#include <stdlib.h>
5#include <string.h>
6#include <pthread.h>
7
8#ifdef WIN32 /* si vous êtes sous Windows */
9
10#include <winsock2.h>
11
12#elif defined (linux) /* si vous êtes sous Linux */
13
14#include <sys/types.h>
15#include <sys/socket.h>
16#include <netinet/in.h>
17#include <arpa/inet.h>
18#include <unistd.h> /* close */
19#include <netdb.h> /* gethostbyname */
20#define INVALID_SOCKET -1
21#define SOCKET_ERROR -1
22#define closesocket(s) close(s)
23typedef int SOCKET;
24typedef struct sockaddr_in SOCKADDR_IN;
25typedef struct sockaddr SOCKADDR;
26typedef struct in_addr IN_ADDR;
27
28#else /* sinon vous êtes sur une plateforme non supportée */
29
30#error not defined for this platform
31
32#endif
33
34
35int read_in(char *chaine, int longueur);
36void *ext_data(int *id_sock);
37
38int main()
39{
40
41 int nbr_char; // Indique le nombre de caractères qui a été reçu ou envoyé
42 char buffer[65535]; // Tampon contenant les données reçues ou envoyées
43 int loop = 1;
44 SOCKET sock = socket(AF_INET, SOCK_STREAM, 0);
45 if(sock == INVALID_SOCKET)
46 {
47 printf("[ERROR] Can't create socket. (->");
48 perror("socket()");
49 printf(")\n[INFO] Exiting ...\n\n");
50 exit(EXIT_FAILURE);
51 }
52
53 struct hostent *hostinfo = NULL;
54 SOCKADDR_IN sin = { 0 }; /* initialise la structure avec des 0 */
55 const char *hostname = "192.168.1.12";
56
57 hostinfo = gethostbyname(hostname); /* on récupère les informations de l'hôte auquel on veut se connecter */
58 if (hostinfo == NULL) /* l'hôte n'existe pas */
59 {
60 printf ("[ERROR] Unknown host %s.\n", hostname);
61 exit(EXIT_FAILURE);
62 }
63 else
64 {
65 printf("[INFO] Host : %s:%s\n", hostname,PORT);
66 }
67
68 sin.sin_addr = *(IN_ADDR *) hostinfo->h_addr; /* l'adresse se trouve dans le champ h_addr de la structure hostinfo */
69 sin.sin_port = htons(PORT); /* on utilise htons pour le port */
70 sin.sin_family = AF_INET;
71
72 if(connect(sock,(SOCKADDR *) &sin, sizeof(SOCKADDR)) < 0)
73 {
74 perror("connect()");
75 exit(EXIT_FAILURE);
76 }
77 else
78 {
79 printf("[INFO] Connected !\n");
80 }
81
82 pthread_t thread;
83 pthread_create(&thread,NULL,ext_data,&sock);
84
85 while (loop)
86 {
87 //printf("\n\n");
88 read_in(buffer,1000); // Lit l'entrée clavier (1000 char max)
89 if(strcmp(buffer, "quit") == 0)
90 {
91 loop = 0;
92 printf("Exiting program ...\n");
93 sleep(5000);
94 }
95 //envoi des données
96 printf("\t\t\t\tSending...");
97 nbr_char = send(sock,buffer,strlen(buffer),0);
98 if (nbr_char==SOCKET_ERROR){printf("\t[FAIL]\n");}else{printf("\t: [OK]\n");}
99
100
101
102 }
103
104}
105
106
107
108
109void *ext_data(int *id_sock)
110{
111 int ret = 0;
112 int loop_recv = 1;
113 int nbr_char = 0;
114 char response[65535];
115 printf("[INFO] Socket descriptor in thread : %d\n",*id_sock);
116 loop_recv = 1;
117 while (loop_recv)
118 {
119 nbr_char = recv(*id_sock,response,65535,0);
120 if (nbr_char == SOCKET_ERROR)
121 {
122 printf("[ERROR] Recv error. Server seems to be down\n");
123 loop_recv = 0;
124 }
125 else
126 {
127 response[nbr_char] = '\0'; // ferme le tableau après reception des données
128 printf("%s\n",response); //affiche les données recues.
129 }
130 }
131 printf("[INFO] Exiting thread.\n");
132}
133
134
135int read_in(char *chaine, int longueur)
136{
137 char *positionEntree = NULL;
138 if (fgets(chaine, longueur, stdin) != NULL)
139 {
140 positionEntree = strchr(chaine, '\n');
141 if (positionEntree != NULL)
142 {
143 *positionEntree = '\0';
144 }
145 return 1;
146 }
147 else
148 {
149 return 0;
150 }
151}