环境:Linux

协议:UDP

网络知识

UDP简介

UDP全称 User Datagram Protocol,即:用户数据报协议。是面向无连接的协议。通常,UDP 通信还会被冠以不可靠的头衔。这里的不可靠指的是:无法可靠地得知对方是否收到数据。<br />UDP有如下特征:

  • 无连接:通信双方不需要事先连接
  • 无确认:收到数据不给对方发回执确认
  • 不保证有序、丢失不重发
  • 采用帧同步的数据报通信方式(即通信双方每次的收发数据量相等)
    简单来讲,UDP 类似于寄信,如果两个人除了信件之外没有任何别的通信方式,那么信件寄出去了之后,寄件人是无法得知收件人是否收到信件或者是否已经读取内容的。UDP 的特点是无需连接、无需确认、无需缓冲区和分包序列号,因此 UDP 的效率是比较高的。

UDP通信流程

  • 发送方:

    1. 创建 UDP 套接字: int fd = socket();
    2. 准备好接收方的地址:struct sockaddr_in peerAddr;
    3. 给对方发送 UDP 数据报:sendto(fd, peerAddr);
  • 接收方:

    1. 创建 UDP 套接字:int fd = socket();
    2. 准备好自己的地址:struct sockaddr_in addr;
    3. 绑定套接字和地址:bind(fd, addr);
    4. 坐等各方发来的 UDP 数据报:recvfrom(fd);

如何理解套接字

image.png

编程实例

测试代码

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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <errno.h>
#include <netdb.h>
#include <pthread.h>
#define PORT 5000
#define SERVER_IP "192.168.6.5"
#define IVVER_LEN 17
#define PROVER_LEN 85

typedef struct{
float Ia;
float Ib;
float Ic;
uint16_t Va;
uint16_t Vb;
uint16_t Vc;
} analysis_s_part;

typedef struct{
float Ia;
float Ib;
float Ic;
uint16_t Va;
uint16_t Vb;
uint16_t Vc;
float Ca;
float Cb;
float Cc;
} analysis_l_part;

struct sense_part{
uint16_t type; //类型
uint16_t number; //编号
};

struct content_part {
uint32_t ticks; //时间戳
struct sense_part sense; //感知部分
float value; //量值
};

struct udp_request {
uint8_t forward; //前导
uint8_t reserve; //保留位,填充信息
uint16_t version; //版本
uint16_t length; //长度
uint16_t verify; //校验
uint16_t command; //指令
struct content_part content; //内容部分
};

/******************************************************************************
* Name: CRC-16/MODBUS x16+x15+x2+1
* Poly: 0x8005
* Init: 0xFFFF
* Refin: True
* Refout: True
* Xorout: 0x0000
* Note:
*****************************************************************************/
uint16_t crc16_modbus(uint8_t *data, uint16_t length)
{
uint8_t i;
uint16_t crc = 0xffff; // Initial value
while(length--)
{
crc ^= *data++; // crc ^= *data; data++;
for (i = 0; i < 8; ++i)
{
if (crc & 1)
crc = (crc >> 1) ^ 0xA001; // 0xA001 = reverse 0x8005
else
crc = (crc >> 1);
}
}
return crc;
}

//报文解析
//DianliuDianyaBan
analysis_s_part analysis_s(unsigned char *rec_data){
analysis_s_part ana_s;
uint8_t DataLength_Short = rec_data[2];
float ElectricityA1 =(rec_data[3] << 8 | rec_data[4]);
float ElectricityA = ElectricityA1/10;
float ElectricityB1 =(rec_data[5] << 8 | rec_data[6]);
float ElectricityB = ElectricityB1/10;
float ElectricityC1 =(rec_data[7] << 8 | rec_data[8]);
float ElectricityC = ElectricityC1/10;
uint16_t VoltageA = rec_data[9] << 8 | rec_data[10];
uint16_t VoltageB = rec_data[11] << 8 | rec_data[12];
uint16_t VoltageC = rec_data[13] << 8 | rec_data[14];
ana_s.Ia = ElectricityA;
ana_s.Ib = ElectricityB;
ana_s.Ic = ElectricityC;
ana_s.Va = VoltageA;
ana_s.Vb = VoltageB;
ana_s.Vc = VoltageC;
printf("DianliuDianyaBan:\n");
printf("Length: %d\n", DataLength_Short);
printf("ElectricityA: %.1f\n", ElectricityA);
printf("ElectricityB: %.1f\n", ElectricityB);
printf("ElectricityC: %.1f\n", ElectricityC);
printf("VoltageA: %d\n", VoltageA);
printf("VoltageB: %d\n", VoltageB);
printf("VoltageC: %d\n", VoltageC);
printf("\n");
return ana_s;
}

//QuannengBan
analysis_l_part analysis_l(unsigned char *rec_data){
analysis_l_part ana_l;
uint8_t DataLength_Long = rec_data[2];
float ProElectricityA1 =(rec_data[3] << 8 | rec_data[4]);
float ProElectricityA = ProElectricityA1/10;
float ProElectricityB1 =(rec_data[5] << 8 | rec_data[6]);
float ProElectricityB = ProElectricityB1/10;
float ProElectricityC1 =(rec_data[7] << 8 | rec_data[8]);
float ProElectricityC = ProElectricityC1/10;
uint16_t ProVoltageA = rec_data[9] << 8 | rec_data[10];
uint16_t ProVoltageB = rec_data[11] << 8 | rec_data[12];
uint16_t ProVoltageC = rec_data[13] << 8 | rec_data[14];
uint16_t ActivePowerA_h = rec_data[15] << 8 | rec_data[16];
int ActivePowerA;
if(ActivePowerA_h >> 12 >= 0x8){
ActivePowerA_h = ~ActivePowerA_h + 1;
ActivePowerA = -ActivePowerA_h;
}else{
ActivePowerA = ActivePowerA_h;
}
uint16_t ActivePowerB_h = rec_data[17] << 8 | rec_data[18];
int ActivePowerB;
if(ActivePowerB_h >> 12 >= 0x8){
ActivePowerB_h = ~ActivePowerB_h + 1;
ActivePowerB = -ActivePowerB_h;
}else{
ActivePowerB = ActivePowerB_h;
}
uint16_t ActivePowerC_h = rec_data[19] << 8 | rec_data[20];
int ActivePowerC;
if(ActivePowerC_h >> 12 >= 0x8){
ActivePowerC_h = ~ActivePowerC_h + 1;
ActivePowerC = -ActivePowerC_h;
}else{
ActivePowerC = ActivePowerC_h;
}
uint32_t TotalActivePower_h = rec_data[21] <<24 | rec_data[22] <<16 | rec_data[23] << 8 | rec_data[24];
int TotalActivePower;
if(TotalActivePower_h >> 28 >= 0x8){
TotalActivePower_h = ~TotalActivePower_h + 1;
TotalActivePower = -TotalActivePower_h;
}else{
TotalActivePower = TotalActivePower_h;
}
uint16_t ReactivePowerA_h = rec_data[25] << 8 | rec_data[26];
int ReactivePowerA;
if(ReactivePowerA_h >> 12 >= 0x8){
ReactivePowerA_h = ~ReactivePowerA_h + 1;
ReactivePowerA = -ReactivePowerA_h;
}else{
ReactivePowerA = ReactivePowerA_h;
}
uint16_t ReactivePowerB_h = rec_data[27] << 8 | rec_data[28];
int ReactivePowerB;
if(ReactivePowerB_h >> 12 >= 0x8){
ReactivePowerB_h = ~ReactivePowerB_h + 1;
ReactivePowerB = -ReactivePowerB_h;
}else{
ReactivePowerB = ReactivePowerB_h;
}
uint16_t ReactivePowerC_h = rec_data[29] << 8 | rec_data[30];
int ReactivePowerC;
if(ReactivePowerC_h >> 12 >= 0x8){
ReactivePowerC_h = ~ReactivePowerC_h + 1;
ReactivePowerC = -ReactivePowerC_h;
}else{
ReactivePowerC = ReactivePowerC_h;
}
uint32_t TotalReactivePower_h = rec_data[31] << 24 | rec_data[32] <<16 | rec_data[33] << 8 | rec_data[34];
int TotalReactivePower;
if(TotalReactivePower_h >> 28 >= 0x8){
TotalReactivePower_h = ~TotalReactivePower_h + 1;
TotalReactivePower = -TotalReactivePower_h;
}else{
TotalReactivePower = TotalReactivePower_h;
}
uint16_t ApparentPowerA_h = rec_data[35] << 8 | rec_data[36];
int ApparentPowerA;
if(ApparentPowerA_h >> 12 >= 0x8){
ApparentPowerA_h = ~ApparentPowerA_h + 1;
ApparentPowerA = -ApparentPowerA_h;
}else{
ApparentPowerA = ApparentPowerA_h;
}
uint16_t ApparentPowerB_h = rec_data[37] << 8 | rec_data[38];
int ApparentPowerB;
if(ApparentPowerB_h >> 12 >= 0x8){
ApparentPowerB_h = ~ApparentPowerB_h + 1;
ApparentPowerB = -ApparentPowerB_h;
}else{
ApparentPowerB = ApparentPowerB_h;
}
uint16_t ApparentPowerC_h = rec_data[39] << 8 | rec_data[40];
int ApparentPowerC;
if(ApparentPowerC_h >> 12 >= 0x8){
ApparentPowerC_h = ~ApparentPowerC_h + 1;
ApparentPowerC = -ApparentPowerC_h;
}else{
ApparentPowerC = ApparentPowerC_h;
}
uint32_t TotalApparentPower_h = rec_data[41] << 24 | rec_data[42] << 16| rec_data[43] << 8 | rec_data[44];
int TotalApparentPower;
if(TotalApparentPower_h >> 28 >= 0x8){
TotalApparentPower_h = ~TotalApparentPower_h + 1;
TotalApparentPower = -TotalApparentPower_h;
}else{
TotalApparentPower = TotalApparentPower_h;
}
float PowerFactorA1_h =(rec_data[45] << 8 | rec_data[46]);
float PowerFactorA = PowerFactorA1_h/100;
float PowerFactorB1 =(rec_data[47] << 8 | rec_data[48]);
float PowerFactorB = PowerFactorB1/100;
float PowerFactorC1 =(rec_data[49] << 8 | rec_data[50]);
float PowerFactorC = PowerFactorC1/100;
float TotalPowerFactor;
uint8_t TotalPowerFactor_h = rec_data[52];
if(TotalPowerFactor_h >> 4 >= 0x8){
TotalPowerFactor_h =~TotalPowerFactor_h + 1;
TotalPowerFactor = -(float)TotalPowerFactor_h / 100;
}else{
float TotalPowerFactor1 = rec_data[52];
TotalPowerFactor = (float)TotalPowerFactor_h / 100;
}
float APhaseElectricity1 = rec_data[54] << 24 | rec_data[54] << 16| rec_data[55] << 8 | rec_data[56];
float APhaseElectricity = APhaseElectricity1/10;
float BPhaseElectricity1 = rec_data[57] << 24 | rec_data[58] << 16| rec_data[59] << 8 | rec_data[60];
float BPhaseElectricity = BPhaseElectricity1/10;
float CPhaseElectricity1 = rec_data[61] << 24 | rec_data[62] << 16| rec_data[63] << 8 | rec_data[64];
float CPhaseElectricity = CPhaseElectricity1/10;
uint32_t TotalElectricity1_h = rec_data[65] << 16;
uint32_t TotalElectricity2_h = TotalElectricity1_h << 8;
float TotalElectricity1 = rec_data[65] << 24 | rec_data[66] << 16| rec_data[67] << 8 | rec_data[68];
float TotalElectricity = TotalElectricity1/10;
float Frequency1 =(rec_data[79] << 8 | rec_data[80]);
float Frequency = Frequency1/10;
float Temperature1 =(rec_data[81] << 8 | rec_data[82]);
float Temperature = Temperature1/10;

ana_l.Ia = ProElectricityA;
ana_l.Ib = ProElectricityB;
ana_l.Ic = ProElectricityC;
ana_l.Va = ProVoltageA;
ana_l.Vb = ProVoltageB;
ana_l.Vc = ProVoltageC;
ana_l.Ca = APhaseElectricity;
ana_l.Cb = BPhaseElectricity;
ana_l.Cc = CPhaseElectricity;

printf("QuannengBan:\n");
printf("Length: %d\n", DataLength_Long);
printf("ElectricityA: %.1f\n", ProElectricityA);
printf("ElectricityB: %.1f\n", ProElectricityB);
printf("ElectricityC: %.1f\n", ProElectricityC);
printf("VoltageA: %d\n", ProVoltageA);
printf("VoltageB: %d\n", ProVoltageB);
printf("VoltageC: %d\n", ProVoltageC);
printf("Active Power A: %d\n", ActivePowerA);
printf("Active Power B: %d\n", ActivePowerB);
printf("Active Power C: %d\n", ActivePowerC);
printf("Total Active Power: %d\n", TotalActivePower);
printf("Reactive Power A: %d\n", ReactivePowerA);
printf("Reactive Power B: %d\n", ReactivePowerB);
printf("Reactive Power C: %d\n", ReactivePowerC);
printf("Total Reactive Power: %d\n", TotalReactivePower);
printf("Apparent Power A: %d\n", ApparentPowerA);
printf("Apparent Power B: %d\n", ApparentPowerB);
printf("Apparent Power C: %d\n", ApparentPowerC);
printf("Total Apparent Power: %d\n", TotalApparentPower);
printf("Power Factor A: %.2f\n", PowerFactorA);
printf("Power Factor B: %.2f\n", PowerFactorB);
printf("Power Factor C: %.2f\n", PowerFactorC);
printf("Total Power Factor: %.2f\n", TotalPowerFactor);
printf("A Quantity of electric charge: %.1f\n", APhaseElectricity);
printf("B Quantity of electric charge: %.1f\n", BPhaseElectricity);
printf("C Quantity of electric charge: %.1f\n", CPhaseElectricity);
printf("Total Quantity of electric charge: %.1f\n", TotalElectricity);
printf("Frequency: %.1f\n", Frequency);
printf("Temperature: %.1f\n", Temperature);
return ana_l;
}



uint32_t get_seconds(void) {
time_t current_time = time(NULL);
return current_time;
}
void SendAndRec(float IV, uint16_t type, uint16_t number, unsigned char* p, struct udp_request request){
request.content.value = IV;
request.content.sense.type = htons(type);
request.content.sense.number = htons(number);
unsigned char* v = (char*)&request.command;
uint16_t crc_verify = crc16_modbus(v, sizeof(v));
request.verify = htons(crc_verify);
unsigned char tmp;
p = (char*)&request;
tmp = p[20];
p[20] = p[23];
p[23] = tmp;

tmp = p[21];
p[21] = p[22];
p[22] = tmp;
int len = sizeof(request);
for(int i =0 ;i<len; i++) {
printf("%02x ", *p);
p++;
}
printf("\n");
}

int main()
{
struct udp_request request;
struct sockaddr_in server_addr;
struct hostent *h;
unsigned char* p = (char*)&request;
unsigned char* v = (char*)&request.command;
uint32_t seconds = get_seconds();
unsigned char buf[1024] ;
char buffer[] = "hello";
char send_data[1024] = "";
char rec_data[1024] = "";
char rec_data1[17] = {0x01 ,0x03 ,0x0C ,0x00 ,0x4C ,0x00 ,0x4B ,0x00 ,0x4B ,0x00 ,0xEA ,0x00 ,0xEA ,0x00 ,0xEA ,0x93 ,0x70};
char rec_data2[85] = {0x01 ,0x03 ,0x50 ,0x00 ,0x4C ,0x00 ,0x4C ,0x00 ,0x4B ,0x00 ,0xEA ,0x00 ,0xEA ,0x00 ,0xEA ,0xFF ,0xFF ,
0xFF ,0xFF ,0x06 ,0xD3 ,0xFF ,0xFF ,0xFF ,0xFF ,0xFF ,0xFF ,0xFF ,0xFF ,0xFF ,0xFF ,0xFF ,0xFF ,0xFF ,0xFF ,0xFF ,0xFF ,0xFF
,0xFF ,0xFF ,0xFF ,0xFF ,0xFF ,0xFF ,0xFF ,0x00 ,0x77 ,0x00 ,0x64 ,0x00 ,0x64 ,0x00 ,0x8F ,0x00 ,0x00 ,0x00 ,0x02 ,0x00 ,0x00
,0x00 ,0x02 ,0x00 ,0x00 ,0x00 ,0x02 ,0x00 ,0x00 ,0x00 ,0x06 ,0x00 ,0x00 ,0x00 ,0x01 ,0x00 ,0x01 ,0x00 ,0x00 ,0x00 ,0x01 ,0x01
,0xF4 ,0x00 ,0xCF ,0x68 ,0xCD};
char *ptr1, *ptr2;
int i, j, len1, len2;
int data_len = 0;
int addr_len = sizeof(struct sockaddr_in);
char hostname[] = "www.baidu.com";
char ip[20];
printf("\n");
printf("%p\n\n", p);
//check ziduan
request.forward = 0xaa;
request.reserve = 0x00;
request.version = htons(0x0101);
request.length = htons(0x000c);
request.command = htons(0x0a01);
request.content.ticks = htonl(seconds);


uint16_t crc_verify = crc16_modbus(v, sizeof(v));
request.verify = crc_verify;

printf("Forward: 0x%04x\n", request.forward);
printf("Version: 0x%04x\n", request.version);
printf("Length: 0x%04x\n", request.length);
printf("Verify: 0x%04x\n", crc_verify);
printf("Command: 0x%04x\n", request.command);
printf("Content-Ticks: 0x%04x\n",request.content.ticks);
printf("Content-Value:%.2f\n",request.content.value);
printf("Content-Type: 0x%04x\n",request.content.sense.type);
printf("Content-Number: 0x%04x\n",request.content.sense.number);


printf("\n");

//dns to ip
h = gethostbyname(hostname);
if(h ==NULL)
{
perror("GetIP");
//printf("Get IP error\n");
exit(1);
}

strncpy(ip, inet_ntoa(*((struct in_addr *)h->h_addr)),sizeof(ip));
//strncpy(ip, SERVER_IP,sizeof(ip));
printf("HostName :%s\n",h->h_name);
printf("IP Address1 :%s\n",inet_ntoa(*((struct in_addr *)h->h_addr)));
printf("IP Address2 :%s\n",ip);
printf("\n");


//create udp socket
int sock = socket(AF_INET,SOCK_DGRAM,0);
if(sock < 0)
{
perror("socket creat");
//printf("socket create error\n");
return 0;
}
printf("socket:%d\n",sock);


//set server address
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr(ip);
server_addr.sin_port = htons(PORT);

//send data to udp server
int len = sizeof(request);
request.content.value = -50;
for(int i =0 ;i<len; i++) {
printf("%02x ", *p);
p++;
}
printf("\n");
/*
p = (char*)&request;
int sendto_len = sendto(sock, p, len, 0, (struct sockaddr *)&server_addr,sizeof(server_addr));
if(sendto_len < 0){
perror("sendto");
return 0;
}
printf("data:%d\n",sendto_len);
printf("\n");
*/


//send and receive data
/*
int iFd = _uart_open();
if(iFd < 0){
perror("open fail");
return 0;
}
printf("open success,iFd=%d\n",iFd);
*/
uint16_t num_sense = 0;
while(1)
{
//analysis
/*
unsigned char verify_data[8] = {0x01 ,0x03, 0x00, 0x00, 0x00, 0x06, 0xC5, 0xC8};
int w_result= _uart_write(verify_data, 8);
printf("write_result is%d\n",w_result);
printf("");
int size_read = _uart_read(buf, sizeof(buf));
int len_read = buf[2];
int size_all = 3 + len_read + 2;
while(size_read < size_all){
size_read += _uart_read(buf + size_read, sizeof(buf) - size_read);
}
printf("Read data length: %d\n",size_all);
for(i = 0; i < size_all; i++) {
printf("%02x ", buf[i]);
}
printf("\n");
*/
if(sizeof(rec_data1) < 30){
analysis_s_part ana_s = analysis_s(rec_data1);
if(ana_s.Ia != 0 || ana_s.Va != 0){
if(ana_s.Ia != 0){
SendAndRec(ana_s.Ia, 0x0204, num_sense, p, request);
int sendto_len = sendto(sock, p, len, 0, (struct sockaddr *)&server_addr,sizeof(server_addr));
if(sendto_len < 0){
perror("sendto");
return 0;
}
printf("data:%d\n",sendto_len);
int rec_len = recvfrom(sock,rec_data, strlen(rec_data),0,(struct sockaddr *)&server_addr,&addr_len);
if(rec_len < 0)
{
printf("Receive data[%d] failure:%s \n",rec_len-1,strerror(errno));
continue;
}
printf("Receive data: %s\n", buf);
memset(&rec_data, 0, sizeof(rec_data));

}
if(ana_s.Va != 0){
SendAndRec(ana_s.Va, 0x0206, num_sense, p, request);
int sendto_len = sendto(sock, p, len, 0, (struct sockaddr *)&server_addr,sizeof(server_addr));
if(sendto_len < 0){
perror("sendto");
return 0;
}
printf("data:%d\n",sendto_len);
int rec_len = recvfrom(sock,rec_data, strlen(rec_data),0,(struct sockaddr *)&server_addr,&addr_len);
if(rec_len < 0)
{
printf("Receive data[%d] failure:%s \n",rec_len-1,strerror(errno));
continue;
}
printf("Receive data: %s\n", buf);
memset(&rec_data, 0, sizeof(rec_data));

}
num_sense ++;
}
if(ana_s.Ib != 0 || ana_s.Vb != 0){
if(ana_s.Ib != 0){
SendAndRec(ana_s.Ib, 0x0204, num_sense, p, request);
int sendto_len = sendto(sock, p, len, 0, (struct sockaddr *)&server_addr,sizeof(server_addr));
if(sendto_len < 0){
perror("sendto");
return 0;
}
printf("data:%d\n",sendto_len);
int rec_len = recvfrom(sock,rec_data, strlen(rec_data),0,(struct sockaddr *)&server_addr,&addr_len);
if(rec_len < 0)
{
printf("Receive data[%d] failure:%s \n",rec_len-1,strerror(errno));
continue;
}
printf("Receive data: %s\n", buf);
memset(&rec_data, 0, sizeof(rec_data));

}
if(ana_s.Vb != 0){
SendAndRec(ana_s.Vb, 0x0206, num_sense, p, request);
int sendto_len = sendto(sock, p, len, 0, (struct sockaddr *)&server_addr,sizeof(server_addr));
if(sendto_len < 0){
perror("sendto");
return 0;
}
printf("data:%d\n",sendto_len);
int rec_len = recvfrom(sock,rec_data, strlen(rec_data),0,(struct sockaddr *)&server_addr,&addr_len);
if(rec_len < 0)
{
printf("Receive data[%d] failure:%s \n",rec_len-1,strerror(errno));
continue;
}
printf("Receive data: %s\n", buf);
memset(&rec_data, 0, sizeof(rec_data));

}
num_sense ++;
}
if(ana_s.Ic != 0 || ana_s.Vc != 0){
if(ana_s.Ic != 0){
SendAndRec(ana_s.Ic, 0x0204, num_sense, p, request);
int sendto_len = sendto(sock, p, len, 0, (struct sockaddr *)&server_addr,sizeof(server_addr));
if(sendto_len < 0){
perror("sendto");
return 0;
}
printf("data:%d\n",sendto_len);
int rec_len = recvfrom(sock,rec_data, strlen(rec_data),0,(struct sockaddr *)&server_addr,&addr_len);
if(rec_len < 0)
{
printf("Receive data[%d] failure:%s \n",rec_len-1,strerror(errno));
continue;
}
printf("Receive data: %s\n", buf);
memset(&rec_data, 0, sizeof(rec_data));

}
if(ana_s.Vc != 0){
SendAndRec(ana_s.Vc, 0x0206, num_sense, p, request);
int sendto_len = sendto(sock, p, len, 0, (struct sockaddr *)&server_addr,sizeof(server_addr));
if(sendto_len < 0){
perror("sendto");
return 0;
}
printf("data:%d\n",sendto_len);

int rec_len = recvfrom(sock,rec_data, strlen(rec_data),0,(struct sockaddr *)&server_addr,&addr_len);
if(rec_len < 0)
{
printf("Receive data[%d] failure:%s \n",rec_len-1,strerror(errno));
continue;
}
printf("Receive data: %s\n", buf);
memset(&rec_data, 0, sizeof(rec_data));
}
num_sense ++;
}


}else{
analysis_l_part ana_l = analysis_l(buf);
if(ana_l.Ia != 0 || ana_l.Va != 0 || ana_l.Ca != 0){
if(ana_l.Ia != 0){
SendAndRec(ana_l.Ia, 0x0204, num_sense, p, request);
int sendto_len = sendto(sock, p, len, 0, (struct sockaddr *)&server_addr,sizeof(server_addr));
if(sendto_len < 0){
perror("sendto");
return 0;
}
printf("data:%d\n",sendto_len);
int rec_len = recvfrom(sock,rec_data, strlen(rec_data),0,(struct sockaddr *)&server_addr,&addr_len);
if(rec_len < 0)
{
printf("Receive data[%d] failure:%s \n",rec_len-1,strerror(errno));
continue;
}
printf("Receive data: %s\n", buf);
memset(&rec_data, 0, sizeof(rec_data));

}
if(ana_l.Va != 0){
SendAndRec(ana_l.Va, 0x0206, num_sense, p, request);
int sendto_len = sendto(sock, p, len, 0, (struct sockaddr *)&server_addr,sizeof(server_addr));
if(sendto_len < 0){
perror("sendto");
return 0;
}
printf("data:%d\n",sendto_len);
int rec_len = recvfrom(sock,rec_data, strlen(rec_data),0,(struct sockaddr *)&server_addr,&addr_len);
if(rec_len < 0)
{
printf("Receive data[%d] failure:%s \n",rec_len-1,strerror(errno));
continue;
}
printf("Receive data: %s\n", buf);
memset(&rec_data, 0, sizeof(rec_data));

}
if(ana_l.Ca != 0){
SendAndRec(ana_l.Ca, 0x0202, num_sense, p, request);
int sendto_len = sendto(sock, p, len, 0, (struct sockaddr *)&server_addr,sizeof(server_addr));
if(sendto_len < 0){
perror("sendto");
return 0;
}
printf("data:%d\n",sendto_len);
int rec_len = recvfrom(sock,rec_data, strlen(rec_data),0,(struct sockaddr *)&server_addr,&addr_len);
if(rec_len < 0)
{
printf("Receive data[%d] failure:%s \n",rec_len-1,strerror(errno));
continue;
}
printf("Receive data: %s\n", buf);
memset(&rec_data, 0, sizeof(rec_data));

}
num_sense ++;
}
if(ana_l.Ib != 0 || ana_l.Vb != 0 || ana_l.Cb != 0){
if(ana_l.Ib != 0){
SendAndRec(ana_l.Ib, 0x0204, num_sense, p, request);
int sendto_len = sendto(sock, p, len, 0, (struct sockaddr *)&server_addr,sizeof(server_addr));
if(sendto_len < 0){
perror("sendto");
return 0;
}
printf("data:%d\n",sendto_len);
int rec_len = recvfrom(sock,rec_data, strlen(rec_data),0,(struct sockaddr *)&server_addr,&addr_len);
if(rec_len < 0)
{
printf("Receive data[%d] failure:%s \n",rec_len-1,strerror(errno));
continue;
}
printf("Receive data: %s\n", buf);
memset(&rec_data, 0, sizeof(rec_data));

}
if(ana_l.Vb != 0){
SendAndRec(ana_l.Vb, 0x0206, num_sense, p, request);
int sendto_len = sendto(sock, p, len, 0, (struct sockaddr *)&server_addr,sizeof(server_addr));
if(sendto_len < 0){
perror("sendto");
return 0;
}
printf("data:%d\n",sendto_len);
int rec_len = recvfrom(sock,rec_data, strlen(rec_data),0,(struct sockaddr *)&server_addr,&addr_len);
if(rec_len < 0)
{
printf("Receive data[%d] failure:%s \n",rec_len-1,strerror(errno));
continue;
}
printf("Receive data: %s\n", buf);
memset(&rec_data, 0, sizeof(rec_data));

}
if(ana_l.Cb != 0){
SendAndRec(ana_l.Cb, 0x0202, num_sense, p, request);
int sendto_len = sendto(sock, p, len, 0, (struct sockaddr *)&server_addr,sizeof(server_addr));
if(sendto_len < 0){
perror("sendto");
return 0;
}
printf("data:%d\n",sendto_len);
int rec_len = recvfrom(sock,rec_data, strlen(rec_data),0,(struct sockaddr *)&server_addr,&addr_len);
if(rec_len < 0)
{
printf("Receive data[%d] failure:%s \n",rec_len-1,strerror(errno));
continue;
}
printf("Receive data: %s\n", buf);
memset(&rec_data, 0, sizeof(rec_data));

}
num_sense ++;
}
if(ana_l.Ic != 0 || ana_l.Vc != 0 || ana_l.Cc != 0){
if(ana_l.Ic != 0){
SendAndRec(ana_l.Ic, 0x0204, num_sense, p, request);
int sendto_len = sendto(sock, p, len, 0, (struct sockaddr *)&server_addr,sizeof(server_addr));
if(sendto_len < 0){
perror("sendto");
return 0;
}
printf("data:%d\n",sendto_len);
int rec_len = recvfrom(sock,rec_data, strlen(rec_data),0,(struct sockaddr *)&server_addr,&addr_len);
if(rec_len < 0)
{
printf("Receive data[%d] failure:%s \n",rec_len-1,strerror(errno));
continue;
}
printf("Receive data: %s\n", buf);
memset(&rec_data, 0, sizeof(rec_data));

}
if(ana_l.Vc != 0){
SendAndRec(ana_l.Vc, 0x0206, num_sense, p, request);
int sendto_len = sendto(sock, p, len, 0, (struct sockaddr *)&server_addr,sizeof(server_addr));
if(sendto_len < 0){
perror("sendto");
return 0;
}
printf("data:%d\n",sendto_len);
int rec_len = recvfrom(sock,rec_data, strlen(rec_data),0,(struct sockaddr *)&server_addr,&addr_len);
if(rec_len < 0)
{
printf("Receive data[%d] failure:%s \n",rec_len-1,strerror(errno));
continue;
}
printf("Receive data: %s\n", buf);
memset(&rec_data, 0, sizeof(rec_data));

}
if(ana_l.Cc != 0){
SendAndRec(ana_l.Cc, 0x0202, num_sense, p, request);
int sendto_len = sendto(sock, p, len, 0, (struct sockaddr *)&server_addr,sizeof(server_addr));
if(sendto_len < 0){
perror("sendto");
return 0;
}
printf("data:%d\n",sendto_len);
int rec_len = recvfrom(sock,rec_data, strlen(rec_data),0,(struct sockaddr *)&server_addr,&addr_len);
if(rec_len < 0)
{
printf("Receive data[%d] failure:%s \n",rec_len-1,strerror(errno));
continue;
}
printf("Receive data: %s\n", buf);
memset(&rec_data, 0, sizeof(rec_data));

}
num_sense ++;
}
}
//analysis_s_part ana_s = analysis_s(rec_data1);
//analysis_l_part ana_l = analysis_l(rec_data2);
printf("\n");

sleep(2000);

}
// if (recvfrom(sockfd, recv_buf, sizeof(recv_buf), 0, NULL, NULL) < 0) {

close(sock);
printf("\n");
return 0;
}

运行结果

sockaddr结构体

1
2
3
4
5
6
7
struct hostent {  
char *h_name; /* 主机的官方域名 */
char **h_aliases; /* 一个以NULL结尾的主机别名数组 */
int h_addrtype; /* 返回的地址类型,在Internet环境下为AF-INET */
int h_length; /* 地址的字节长度 */
char **h_addr_list; /* 一个以0结尾的数组,包含该主机的所有地址*/
};
1
2
3
4
struct sockaddr {  
unsigned short sa_family; // address family, AF_xxx
char sa_data[14]; // 14 bytes of protocol address
};

sockaddr_in结构体

1
2
3
4
5
6
7
8
9
10
11
12
13
struct   sockaddr_in   {  
short int sin_family; // Address family
unsigned short int sin_port; // Port number
struct in_addr sin_addr; /* Internet address */
unsigned char sin_zero[8]; /* Same size as struct sockaddr */
};
struct in_addr {
union {
struct { u_char s_b1,s_b2,s_b3,s_b4; } S_un_b;
struct { u_short s_w1,s_w2; } S_un_w;
u_long S_addr;
} S_un;
}

库函数

标准库函数

strcpy()/strncpy()函数

函数功能:把一个地址上的数据复制到另一个地址上去

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//1. 函数原型
#include <string.h>

char *strcpy(char *dest, const char *src);
//用法:把src这个地址的地址的字符串赋值到dest这个地址去

char *strncpy(char *dest, const char *src, size_t n);
//用法:把src这个地址的地址的n个字符串赋值到dest这个地址去


//2. 常见的指针与数组间的赋值问题
char a[] = {'a','b','c'};
char b[3];

char *p = a; //正确赋值,数组名为数组首元素地址,因此可使用指针保存该地址(指针只能保存地址)
>char c[3] = a; //错误---》数组不能直接给数组赋值
char d[3] = p; //错误---》指针不能直接给数组赋值
strcpy(b,a); //正确,使用标准库函数可实现字符串拷贝
char **p1 = &p; //正确,二级指针可接收一级指针的地址

注意:

  • 数组不能直接给数组赋值
  • 指针不能直接给数组赋值

perror函数

函数功能:这个函数内部会自己获取errno的值,调用这个函数会直接把错误提示字符串打印出来。此外,我们也可以在错误提示字符串前添加一些自己想要打印的信息。

1
2
3
#include <stdio.h>

void perror(const char *s)

参数:

  1. s:如果我们想要在错误提示字符串之前添加自己想要的打印信息,就通过此参数传入,如果不需要添加信息,则让其为空字符串即可。

套接字库函数

Socket() 函数

函数功能:在Linux下用socket()来创建套接字

1
2
3
4
#include <sys/types.h>
#include <sys/socket.h>

int socket(int af, int type, int protocol)

参数:

  1. af:地址族,也就是ip地址类型,常用的有AF_INETAF_INET6。AF是”Address Family”的简写,INET是”Internet”的简写。前者表示IPv4地址,后者表示IPv6地址。
  2. type:数据传输方式/套接字类型,常用的有SOCK_STREAM(流格式套接字/面向连接的套接字)SOCK_DGRAM(数据报套接字/无连接的套接字)
  • SOCK_STREAM 选择 TCP – 流式套接字 —>Stream Sockets
  • SOCK_DGRAM 选择 UDP – 数据报套接字 —>Datagram Sockets
  1. protocol:传输协议,传0表示使用默认协议,常用的有IPPROTO_TCPIPPTOTO_UDP,分别表示TCP协议和UDP协议。
  2. 返回值:socket返回的值是一个文件描述符,SOCKET类型本身也是定义为int的,0,1,2分别表示标准输入、标准输出、标准错误,所以其他打开的文件描述符都会大于2; 错误时就返回 -1。
bind() 函数

函数功能:绑定主机的IP地址和端口号

1
2
3
4
#include <sys/types.h>    
#include <sys/socket.h>

int PASCAL FAR bind(SOCKET s, const struct sockaddr* addr, socklen_t addrlen);

参数:

  1. s:由socket()调用返回的并且未作连接的套接字描述符(套接字号)。
  2. addr:赋给套接字s的本地 IP 地址和端口号
  3. socklen_t:地址的大小长度。
  4. 返回值:如果没有错误发生,bind()返回0。否则返回SOCKET_ERROR。
sendto() 函数

函数功能:用于UDP中发送数据

1
2
3
#include <sys/socket.h>

ssize_t sendto(int socket,const void* message,size_t length,const struct sockaddr* dest_addr,socklen_t dest_len);

参数:

  1. socket:套接字文件描述符
  2. message:你要发送的数据
  3. length:你要发送的数据大小,注意有多少写多少strlen
  4. flags:一般设置成 0
  5. dest_addr:对方的IP地址和端口号
  6. dest_len:结构体的大小
  7. 返回值:成功:发送出去的字节

失败:-1

recvform() 函数

函数功能:用于UDP中接收数据

1
2
3
#include <sys/socket.h>

ssize_t recvform(int socket,void* buffer,size_t length,int flags,struct sockaddr* dest_addr,socklen_t* address_len);

参数:

  1. socket:套接字文件描述符

  2. buffer:接受的数据存储在这里

  3. length:接受的数据的大小,以最大的来接受(sizeof)

  4. flags:一般设置成0

  5. address:存储客户端的IP地址和端口号,可以获取到是谁给你发送的

  6. address_len:结构体的大小

  7. 返回值:成功:接收到的字节数

    失败:-1

htons\ntons() 函数

函数功能:端口转换

1
2
3
4
5
6
#include <arpa/inet.h>

uint16_t htons(uint16_t hostshort); //将主机端口号转成网络端口号
uint16_t ntohs(uint16_t netshort); //将网络端口号转成主机端口号
uint32_t htonl(uint32_t hostint32); //将32位主机字节序数据转换成网络字节序数据
uint32_t ntohl(uint32_t netint32); //将32位网络字节序数据转换成主机字节序数据

参数:

  1. 返回值:成功:要转换的字节序

    失败:-1

inet_addr\ntoa() 函数

函数功能:

  1. 将字符串形式的IP “192.168.102.169”转换为IPV4的32位无符号整型数的IP
  2. 将无符号整型数的ip,从主机端序转为网络端序
1
2
3
4
5
6
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

in_addr_t inet_addr(const char *cp); //将主机IP转成网络IP
char* inet_ntoa(struct in_addr in); //将网络IP转成主机IP

常见问题

  1. 当定义并调用 analysis_s(char *rec_data, float Ia, float Ib, float Ic, uint16_t Va, uint16_t Vb, uint16_t Vc) 时,各个返回值为0

解决方法
将函数定义改为analysis_s(char *rec_data, float *Ia, float *Ib, float *Ic, uint16_t *Va, uint16_t *Vb, uint16_t *Vc)
将调用改为analysis_s(rec_data1,&Ia_s, &Ib_s, &Ic_s, &Va_s, &Vb_s, &Vc_s)
2. 在结构体当中,数值部分与实际数值相反,并且无法使用字节序转换函数进行转换,7.6对应的十六进制数应为40 F3 33 33,而结构体中正好相反。

解决方法
首先,因为该部分 value 数值在结构体中定义为 float 类型,所以无法直接在赋值时使用字节序转换函数。因此,需要直接在获取的结构体当中,通过数组互换的方式,来改变4个字节的值。