Bonjour à tous,

encore une fois, pour ceux qui ne me connaissent pas, je ne porte pas le C++ dans mon coeur, je préfère (et de très loin) le Java.

Mon problème est le suivant:
On m'a demandé d'écrire une méthode qui va se connecter à un démon, lire les données de ce dernier, les parser, et ensuite les afficher/appeler une méthode tierce.
Ca, c'est fait.

Mais maintenant, on me dit que ce doit être avec des thread. Et là, c'est le drame. Je ne sais pas comment faire, et la doc m'est incompréhensible.
Le programme devra tourner sous Windows.
J'ai regardé du côté de boost, mais j'ai l'impression qu'il faut tout installer pour que ça marche, et c'est la galère. (sauf si je me trompe, et qu'il suffit d'ajouter quelques fichiers pour la librairie)

Bref. Voici mon code:

Interpreter.cpp
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
#include <WINSOCK.H>
#include "Interpreter.h"
using namespace std;
 
void Interpreter::Read()
{
    // Call reader connection method
    string beaconID = "";
    string beaconDist = "";
    unsigned int posSP = 0;
    unsigned int posDB = 0;
    unsigned int positionComma = 0;
    string line;
	WSADATA wsdata;
 
 
if (WSAStartup(WSVERS, &wsdata))   // open window dll
		printf("WSAStartup failed\n");
 
	/* put the server information into the server structure */
	/* The port must be put into network byte order         */
	strcpy(svc_address, DEFAULTSERVADDR);
	port = DEFAULTPORT;
	strcpy(buf1024, DEFAULTMSG);
 
	server.sin_family = AF_INET;
	server.sin_port = htons(port);
	server.sin_addr.s_addr = inet_addr(svc_address);
 
    printf("Server Address: %s\n", svc_address);
	printf("Port Client will connect to: %d\n", port);
 
    CRICKET_client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if (CRICKET_client == INVALID_SOCKET)
	{
		printf("Error\n");
		exit(INVALID_SOCKET);
	} else if ((int)CRICKET_client == -1)
    {
		printf("Fails to open a socket\n");
		exit(-1);
	}
 
	printf("CRICKET Client opens a socket successfully...\n");
 
 
    /* Connect to CRICKET daemon */
	status = connect(CRICKET_client, (struct sockaddr *) &server, sizeof(server));
    printf("Status of connection: %d\n", status);
    if (status == -1)
	{
      printf("CRICKET Client fails to connect to the CRICKET daemon.....\n");
	  printf("The CRICKET daemon may not be active\n");
	  exit(status);
	}
	printf("CRICKET Client connects to the CRICKET daemon successfully...\n");
 
	memset(buf1,0,16);
    strcpy(buf1, "r\r\n");
	if ((status = send(CRICKET_client, buf1, sizeof(buf1), 0)) < 0)
	{
	 printf("%d\n", status);
     printf("CRICKET Client fails to send Username to the CRICKET daemon.....\n");
	 exit(status);
	}
 
 
	/* receive the results of the command */
	while (1)
	{
	 memset(buf1, 0, 2);
	 if (recv(CRICKET_client, buf1, sizeof(buf1), 0) < 0)
	 {
	  printf("CRICKET Client fails to receive the message from the CRICKET daemon successfully.....\n");
	  exit(status);
	 }
	 else
	 {
	     line.assign(buf1);
	     //printf(buf1);
         posSP = line.find("SP=");
         posDB = line.find("DB=");
 
            if(posSP != string::npos && posDB != string::npos)
            {
                positionComma = line.find(",",posSP);
                if(positionComma != string::npos)
                    beaconID = line.substr(posSP+3, (positionComma-posSP)-3);
 
                positionComma = line.find(",",posDB);
                if(positionComma != string::npos)
                    beaconDist = line.substr(posDB+3, (positionComma-posDB)-3);
 
 
                cout << beaconID << "," << beaconDist << endl;
 
            }
            else
            {
                if(line == "")
                {
                    emptyLines++;
                    if(emptyLines > 10)
                    {
                        cout << "No data received from the cricket client." << endl;
                        //Game.over();
 
                    }
                }
                else
                {
                    cout << "Line ignored" << endl;
                }
            }
     }
 
 
	}
 
 
    /* close the socket */
	closesocket(CRICKET_client);
 
 
 
 
 
}
Interpreter.h
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
#ifndef INTERPRETER_H_INCLUDED
#define INTERPRETER_H_INCLUDED
 
#include <WINSOCK.H>
#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <fstream>
#include <iomanip>
 
#define WSVERS              MAKEWORD(2,0)
#define DEFAULTSERVADDR	    "127.0.0.1" //IP address of CRICKET daemon
#define DEFAULTPORT         2947 //Telnet port
#define DEFAULTMSG          "Hello World!"
 
class Interpreter
{
    int emptyLines;
    SOCKET CRICKET_client;
    int status;         /* connect function return code */
    struct sockaddr_in server; /* server address */
	char   svc_address[15];    /* server address */
	int    port;               /* port client will connect to */
	char   buf1024[1024];      /* buffer */
	char   buf1[1];
public:
 
void Read();
 
};
 
#endif
main.cpp
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
#include "Interpreter.h"
using namespace std;
 
int main()
{
Interpreter inter;
inter.Read();
}

Quelqu'un aurait t-il le temps (et la gentillesse !!) de me dire comment faire pour que la méthode Read soit multithreadée.

Merci d'avance !!

Philippe