Bonjour à tous,
ça fait un petit moment que je galère et je n'arrive pas à m'en sortir. Je dois
écrire un client qui reçoit des données en udp. La connexion se fait en point à
point. Mon pc à une certaine IP qui permet au serveur de me reconnaître. Les
données sont envoyées sur 5 ports différents
- 50000/54000 début/fin de daq
- 51000/53000 début/fin de tranche
- 52000 données utiles du système

J'ai besoin de récupérer toutes les données et de les écrire dans le fichier.

Préalablement j'avais fait un petit client sur un seul port et j'avais testé
avec netcat; ça fonctionnait plutôt bien. Au passage de 1 vers 5 sockets… J'y
arrive plus. L'envoie des requêtes vers le serveur fonctionne toujours
correctement mais la réception ne fonctionne pas; je n'entre jamais dans les
handlers…

Ce que je sais par contre c'est que les données sont bien envoyées car ce que je
vois ensuite à partir du paquet 6 est exactement ce que je dois recevoir.

Je n'y arrive pas, je suis perdu… Est-ce qu'une ame charitable pourrait me
mettre sur la voie ?

Merci d'avance pour votre aide.

Olivier

Ci-dessous, un extrait de ce que wireshark voit

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
1 0.000000000 192.168.0.254 192.168.0.1 UDP 50 498905 Len=8 1
2 0.000010876 192.168.0.254 192.168.0.1 UDP 50 498905 Len=8 2
3 0.000013583 192.168.0.254 192.168.0.1 UDP 50 498905 Len=8 3
4 0.000015884 192.168.0.254 192.168.0.1 UDP 50 498905 Len=8 4
5 0.000017992 192.168.0.254 192.168.0.1 UDP 50 498905 Len=8 5
6 0.000045684 192.168.0.1 192.168.0.254 UDP 60 5000032776 Len=8 6
7 0.000045781 192.168.0.1 192.168.0.254 UDP 74 5100032776 Len=32 7
8 0.000087986 192.168.0.254 192.168.0.1 ICMP 102 Destination unreachable (Communication administratively filtered) 8
9 0.000107264 192.168.0.1 192.168.0.254 UDP 486 5200032776 Len=444 9
10 0.000115677 192.168.0.254 192.168.0.1 ICMP 514 Destination unreachable (Communication administratively filtered) 10
12 0.014089529 192.168.0.1 192.168.0.254 UDP 466 5200032776 Len=424 12
13 0.014131458 192.168.0.254 192.168.0.1 ICMP 494 Destination unreachable (Communication administratively filtered) 13
14 0.034794761 192.168.0.1 192.168.0.254 UDP 474 5200032776 Len=432 14
15 0.034836852 192.168.0.254 192.168.0.1 ICMP 502 Destination unreachable (Communication administratively filtered) 15
17 0.071217607 192.168.0.1 192.168.0.254 UDP 474 5200032776 Len=432 17
18 0.071260595 192.168.0.254 192.168.0.1 ICMP 502 Destination unreachable (Communication administratively filtered) 18
19 0.080098375 192.168.0.1 192.168.0.254 UDP 190 5200032776 Len=148 19
20 0.080098482 192.168.0.1 192.168.0.254 UDP 74 5300032776 Len=32 20
21 0.080098503 192.168.0.1 192.168.0.254 UDP 74 5100032776 Len=32 21
22 0.080149978 192.168.0.254 192.168.0.1 ICMP 218 Destination unreachable (Communication administratively filtered) 22
24 0.160146554 192.168.0.1 192.168.0.254 UDP 486 5200032776 Len=444 24

et ici le code

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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#include "udp_client.hxx"
#include <iostream>
#include <fstream>
#include <functional>
#include <vector>
//#include <thread>
#include <boost/bind/bind.hpp>
#include <boost/asio.hpp>
#include <ctime>
#include <boost/date_time/posix_time/posix_time.hpp>
#include "udp_constants.hxx"
using  boost::asio::ip::udp;
 
        udp_client::udp_client(boost::asio::io_context &io) :
          strand(boost::asio::make_strand(io)),
          socket_data(io, udp::endpoint(udp::v4(), udp_constants::ports::data)),
          socket_daq_start(io, udp::endpoint(udp::v4(), udp_constants::ports::daq_start)),
          socket_daq_stop(io, udp::endpoint(udp::v4(), udp_constants::ports::daq_stop)),
          socket_slice_start(io, udp::endpoint(udp::v4(), udp_constants::ports::slice_start)),
          socket_slice_stop(io, udp::endpoint(udp::v4(), udp_constants::ports::slice_stop)),
          socket_tx(io),
          timer{strand}
        {
 
          socket_daq_start.connect(udp::endpoint(boost::asio::ip::address::from_string("192.168.0.1"), 50000));
          socket_daq_stop.connect(udp::endpoint(boost::asio::ip::address::from_string("192.168.0.1"), 54000));
          socket_slice_start.connect(udp::endpoint(boost::asio::ip::address::from_string("192.168.0.1"), 51000));
          socket_slice_stop.connect(udp::endpoint(boost::asio::ip::address::from_string("192.168.0.1"), 53000));
          socket_data.connect(udp::endpoint(boost::asio::ip::address::from_string("192.168.0.1"), 52000));
 
          datagrams.push_back(std::vector<unsigned char>{0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00});
          datagrams.push_back(std::vector<unsigned char>{0x00, 0x01, 0x00, 0x01, 0x00, 0x7a, 0x11, 0xfe});
          datagrams.push_back(std::vector<unsigned char>{0x00, 0x01, 0x00, 0x02, 0x00, 0x7a, 0x11, 0xfe});
          datagrams.push_back(std::vector<unsigned char>{0x00, 0x01, 0x00, 0x03, 0x00, 0x00, 0x00, 0x05});
          datagrams.push_back(std::vector<unsigned char>{0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01});
          set_acquisition_and_get_data(datagrams);
          filename="/home/lemaire/data.dat";
        }
 
 
        udp_client::~udp_client()
        {
          socket_daq_start.close();
          socket_slice_start.close();
          socket_slice_stop.close();
          socket_daq_stop.close();
          socket_data.close();
          // close file
          output_stream.close();
          // inform user
          std::cout << "udp server destructor" << std::endl;
        }
 
 
        /**
         * @brief udp_client::get_time_stamp
         * @return
         *
         * @note je ne sais pas pour l'instant si c'est bien pertinent mais
         * l'objet c'était d'avoir un getter...
         */
        uint64_t udp_client::get_timestamp()
        {
          return create_timestamp();
        }
 
        std::vector<unsigned char> udp_client::ui64_to_vector(uint64_t w)
        {
          std::vector<unsigned char> vec(8, 0x0);
          char c;
          size_t d{0};
          for(int i{0}; i<8; i++)
          {
            d = (8-1-i)*8;
            c = static_cast<unsigned char>(w >> d);
            vec[i] = c;
          }
          return vec;
        }
 
        bool udp_client::send_data(std::vector<std::vector<unsigned char>>& datagrams)
        {
          std::cout << __PRETTY_FUNCTION__ << std::endl;
          using udp = boost::asio::ip::udp;
          auto endpoint = udp::endpoint(
                boost::asio::ip::address::from_string(udp_constants::ip::fpga),
                udp_constants::ports::configuration);
          try
          {
            socket_tx.open(boost::asio::ip::udp::v4());
            for(auto datagram: datagrams)
              socket_tx.send_to(boost::asio::buffer(datagram), endpoint);
            socket_tx.close();
          }
          catch (const boost::system::system_error& e)
          {
            std::cout << e.what() << std::endl;
          }
 
          return true;
        }
 
        bool udp_client::set_acquisition_and_get_data(std::vector<std::vector<unsigned char>>& datagrams)
        {
          std::cout << __PRETTY_FUNCTION__ << " begin" << std::endl;
          start_receive();
          std::cout << __PRETTY_FUNCTION__ << " start receive launched" << std::endl;
          send_data(datagrams);
          std::cout << __PRETTY_FUNCTION__ << " instructions sent" << std::endl;
          return true;
        }
 
        void udp_client::start_receive()
        {
          std::cout << __PRETTY_FUNCTION__ << std::endl;
          output_stream.open(filename,
                             std::ofstream::out |
                             std::ofstream::trunc |
                             std::ofstream::binary);
 
          std::vector<udp::endpoint> endpoints(5);
          socket_daq_start.async_receive
              (boost::asio::buffer(buffer_daq_start.data(), 16),
               boost::asio::bind_executor
               (strand,
                boost::bind
                (&udp_client::handle_daq_start,
                 this,
                 boost::asio::placeholders::error,
                 boost::asio::placeholders::bytes_transferred
                 )
                )
               );
          std::cout << __PRETTY_FUNCTION__ << " socket daq start started" << std::endl;
          // --
          socket_slice_start.async_receive
              (boost::asio::buffer(buffer_slice_start.data(), 64),
               boost::asio::bind_executor
               (strand,
                boost::bind
                (&udp_client::handle_slice_start,
                 this,
                 boost::asio::placeholders::error,
                 boost::asio::placeholders::bytes_transferred
                 )
                )
               );
          std::cout << __PRETTY_FUNCTION__ << " socket slice start started" << std::endl;
          // --
          socket_slice_stop.async_receive
              (boost::asio::buffer(buffer_slice_stop.data(), 64),
               boost::asio::bind_executor
               (strand,
                boost::bind
                (&udp_client::handle_slice_stop,
                 this,
                 boost::asio::placeholders::error,
                 boost::asio::placeholders::bytes_transferred
                 )
                )
               );
          std::cout << __PRETTY_FUNCTION__ << " socket slice stop started" << std::endl;
          // --
          socket_daq_stop.async_receive
              (boost::asio::buffer(buffer_daq_stop.data(), 16),
               boost::asio::bind_executor
               (strand,
                boost::bind
                (&udp_client::handle_daq_stop,
                 this,
                 boost::asio::placeholders::error,
                 boost::asio::placeholders::bytes_transferred
                 )
                )
               );
          std::cout << __PRETTY_FUNCTION__ << " socket slice stop started" << std::endl;
          // --
          socket_data.async_receive
              (boost::asio::buffer(buffer_data_1.data(), 16),
               boost::asio::bind_executor
               (strand,
                boost::bind
                (&udp_client::handle_data,
                 this,
                 boost::asio::placeholders::error,
                 boost::asio::placeholders::bytes_transferred
                 )
                )
               );
          std::cout << __PRETTY_FUNCTION__ << " socket data started" << std::endl;
 
          return;
        }
 
        void udp_client::handle_daq_start(const boost::system::error_code& error, // Result of operation.
                                          std::size_t bytes_transferred)
        {
          std::cout << __PRETTY_FUNCTION__ << std::endl;
          if(!error)
          {
            // write the data
            if(!output_stream.is_open())
              output_stream.open(filename,
                                 std::ofstream::out |
                                 std::ofstream::trunc |
                                 std::ofstream::binary);
            output_stream.write(buffer_daq_start.data(), bytes_transferred);
            timer.expires_after(boost::asio::chrono::milliseconds(timeout));
            timer.async_wait(boost::bind(&udp_client::handle_timeout, this,
                                         boost::asio::placeholders::error));
          }
          else
          {
            std::cerr << "handle daq start error";
            std::cerr << "message : " << error.message() << std::endl;
          }
          return;
        }
 
        void udp_client::handle_slice_start(const boost::system::error_code &error,
                                            std::size_t bytes_transferred)
        {
          if(!error)
          {
            // add time to the timeout timer
            timer.expires_after(boost::asio::chrono::milliseconds(timeout));
            timer.async_wait(boost::bind(&udp_client::handle_timeout, this,
                                         boost::asio::placeholders::error));
            // write the data
            output_stream.write(buffer_slice_start.data(), bytes_transferred);
      }
      else
      {
        std::cerr << "handle daq start error" << std::endl;
        std::cerr << error.message() << std::endl;
      }
      return;
    }
 
    void udp_client::handle_slice_stop(const boost::system::error_code &error,
                                       std::size_t bytes_transferred)
    {
      if(!error)
      {
        // add time to the timeout timer
        timer.expires_after(boost::asio::chrono::milliseconds(timeout));
        timer.async_wait(boost::bind(&udp_client::handle_timeout, this,
                                     boost::asio::placeholders::error));
        // write the data
        output_stream.write(buffer_slice_start.data(), bytes_transferred);
      }
      else
      {
        std::cerr << "handle daq start error" << std::endl;
        std::cerr << error.message() << std::endl;
      }
      return;
    }
 
    void udp_client::handle_data(const boost::system::error_code& error,
                                 std::size_t bytes_transferred)
    {
      if(!error)
      {
        // add time to the timeout timer
        timer.expires_after(boost::asio::chrono::milliseconds(timeout));
        timer.async_wait(boost::bind(&udp_client::handle_timeout, this,
                                     boost::asio::placeholders::error));
        // write the data
        output_stream.write(buffer_slice_start.data(), bytes_transferred);
 
        //    receive_data();
      }
      else
      {
        std::cerr << "handle daq start error" << std::endl;
        std::cerr << error.message() << std::endl;
      }
      receive_data();
      return;
    }
 
    void udp_client::receive_data()
    {
      socket_data.async_receive
          (boost::asio::buffer(buffer_data_1.data(), buffer_data_1.size()),
           boost::asio::bind_executor
           (strand,
            boost::bind
            (&udp_client::handle_data,
             this,
             boost::asio::placeholders::error,
             boost::asio::placeholders::bytes_transferred
             )
            )
           );
      return;
    }
 
    void udp_client::handle_daq_stop(const boost::system::error_code &error,
                                     std::size_t bytes_transferred)
    {
      if(!error)
      {
        // add time to the timeout timer
        timer.expires_after(boost::asio::chrono::milliseconds(timeout));
        timer.async_wait(boost::bind(&udp_client::handle_timeout, this,
                                     boost::asio::placeholders::error));
        // write the data
        output_stream.write(buffer_daq_stop.data(), bytes_transferred);
      }
      output_stream.close();
      return;
    }
 
    const std::string udp_client::get_data_file_name()
    {
      return filename+std::to_string(file_index)+".dat";
    }
 
    void udp_client::handle_timeout(const boost::system::error_code& e)
    {
      if (e != boost::asio::error::operation_aborted)
      {
        // close sockets
        socket_daq_start.shutdown(udp::socket::shutdown_both);
        socket_slice_start.shutdown(udp::socket::shutdown_both);
        socket_slice_stop.shutdown(udp::socket::shutdown_both);
        socket_daq_stop.shutdown(udp::socket::shutdown_both);
        socket_data.shutdown(udp::socket::shutdown_both);
        output_stream.close();
        std::cout << "file written and closed, socket closed" << std::endl;
      }
    }

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
    #include "udp_client.hxx"
    #include <boost/asio.hpp>
    #include <signal.h>
    #include <chrono>
    #include <boost/thread/thread.hpp>
    #include <boost/asio/thread_pool.hpp>
 
    int main()
    {
      try
      {
        boost::asio::io_context io;
        udp_client client(io);
        boost::thread t{boost::bind(&boost::asio::io_context::run, &io)};
        io.run();
        t.join();
      }
      catch (std::exception& e)
      {
        std::cout << "erreur" << std::endl;
        std::cerr << e.what() << std::endl;
      }
      return 0;
    }