/
Lecture 22 Network Programming Part II.ass
1705 lines (1702 loc) · 174 KB
/
Lecture 22 Network Programming Part II.ass
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
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
[Script Info]
; Script generated by Aegisub 3.2.2
; http://www.aegisub.org/
Title: Default Aegisub file
ScriptType: v4.00+
WrapStyle: 0
ScaledBorderAndShadow: yes
YCbCr Matrix: TV.601
PlayResX: 1280
PlayResY: 720
[Aegisub Project Garbage]
Last Style Storage: Default
Audio File: ../../../../Desktop/csapp/Lecture 22 Network Programming Part II.mp4
Video File: ../../../../Desktop/csapp/Lecture 22 Network Programming Part II.mp4
Video AR Mode: 4
Video AR Value: 1.777778
Video Zoom Percent: 1.000000
Scroll Position: 913
Active Line: 917
Video Position: 72636
[V4+ Styles]
Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding
Style: English,Noto Sans CJK SC Black,34,&H00FFFFFF,&H00412A2C,&H00412A2C,&H00412A2C,0,0,0,0,100,100,0,0,1,1.5,1.2,2,10,10,10,1
Style: Chinese,Noto Sans CJK SC Black,46,&H00FFFF00,&H00FFFFFF,&H00000000,&H00000000,0,0,0,0,100,100,0,0,1,1.2,1.2,2,10,10,10,1
[Events]
Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
Dialogue: 0,0:00:00.72,0:00:06.36,English,,0,0,0,,So today's the second lecture about network programming we got started last time
Dialogue: 0,0:00:00.72,0:00:06.36,Chinese,,0,0,0,,我们今天接着讲网络编程的第二节
Dialogue: 0,0:00:07.04,0:00:15.18,English,,0,0,0,,And as you probably saw last time there the api is for this are really big and hairy and messy and
Dialogue: 0,0:00:07.04,0:00:15.18,Chinese,,0,0,0,,上节课我们可以看到 API 是庞大的,复杂的,混乱的
Dialogue: 0,0:00:15.54,0:00:19.06,English,,0,0,0,,It takes just a lot of time in my own personal experience of
Dialogue: 0,0:00:15.54,0:00:19.06,Chinese,,0,0,0,,我们花费了大量的时间
Dialogue: 0,0:00:19.64,0:00:24.74,English,,0,0,0,,Rereading we looking at code looking at examples rereading documentation
Dialogue: 0,0:00:19.64,0:00:24.74,Chinese,,0,0,0,,看了代码,看了例子,又看了文档
Dialogue: 0,0:00:25.26,0:00:28.00,English,,0,0,0,,Going back and forth over that a dozen or so times
Dialogue: 0,0:00:25.26,0:00:28.00,Chinese,,0,0,0,,来回折腾十几次
Dialogue: 0,0:00:28.56,0:00:30.02,English,,0,0,0,,Before it all really sinks in
Dialogue: 0,0:00:28.56,0:00:30.02,Chinese,,0,0,0,,如果我们真的想要理解它
Dialogue: 0,0:00:30.20,0:00:34.70,English,,0,0,0,,And it's one where it really helps a lot to look at example code
Dialogue: 0,0:00:30.20,0:00:34.70,Chinese,,0,0,0,,阅读示例代码是非常有用的
Dialogue: 0,0:00:34.70,0:00:40.90,English,,0,0,0,,And there's example code in the book in that csapp.c file
Dialogue: 0,0:00:34.70,0:00:40.90,Chinese,,0,0,0,,在书中的 csapp.c 文件中有示例代码
Dialogue: 0,0:00:40.90,0:00:43.34,English,,0,0,0,,That you've seen versions of in various forms
Dialogue: 0,0:00:40.90,0:00:43.34,Chinese,,0,0,0,,你已经看过各种形式的版本
Dialogue: 0,0:00:44.58,0:00:50.16,English,,0,0,0,,So I can lecture for days and weeks
Dialogue: 0,0:00:44.58,0:00:50.16,Chinese,,0,0,0,,所以我可以讲几天甚至几周
Dialogue: 0,0:00:50.30,0:00:54.24,English,,0,0,0,,But this really would all settle in until you've spent time studying yourself
Dialogue: 0,0:00:50.30,0:00:54.24,Chinese,,0,0,0,,但是,你必须自己花时间去研究才能理解他们
Dialogue: 0,0:00:54.86,0:00:59.90,English,,0,0,0,,Anyways I want to repeat and go back over a little bit of ground before
Dialogue: 0,0:00:54.86,0:00:59.90,Chinese,,0,0,0,,无论如何,我想重复一遍,回到之前的内容
Dialogue: 0,0:00:59.92,0:01:05.98,English,,0,0,0,,But last time we talked about the idea of translating from a domain name
Dialogue: 0,0:00:59.92,0:01:05.98,Chinese,,0,0,0,,上次我们谈到了域名映射的概念
Dialogue: 0,0:01:05.98,0:01:12.32,English,,0,0,0,,Which is something like www.cmu.edu to IP address
Dialogue: 0,0:01:05.98,0:01:12.32,Chinese,,0,0,0,,这就像 www.cmu.edu 到 IP 地址的映射
Dialogue: 0,0:01:13.26,0:01:21.00,English,,0,0,0,,For IPv4 is a 32 bit address,it's usually written in dotted decimal form
Dialogue: 0,0:01:13.26,0:01:21.00,Chinese,,0,0,0,, IPv4 是 32 位地址,它通常以点分十进制形式写入
Dialogue: 0,0:01:22.48,0:01:26.81,English,,0,0,0,,And I showed you a program last time called host info that you could
Dialogue: 0,0:01:22.48,0:01:26.81,Chinese,,0,0,0,,我上次给你们看了一个叫做 hostinfo 的程序
Dialogue: 0,0:01:27.74,0:01:29.78,English,,0,0,0,,That will look actually at the code for
Dialogue: 0,0:01:27.74,0:01:29.78,Chinese,,0,0,0,,这看起来是代码
Dialogue: 0,0:01:34.14,0:01:40.62,English,,0,0,0,,And I just wanted to show you today I just did one very small tweak to that program and I'll describe it today
Dialogue: 0,0:01:34.14,0:01:40.62,Chinese,,0,0,0,,今天我将展示在这个程序作出的一个修改
Dialogue: 0,0:01:41.34,0:01:48.88,English,,0,0,0,,And the interesting thing is it lets you look at both IPv4 and IPv6
Dialogue: 0,0:01:41.34,0:01:48.88,Chinese,,0,0,0,,这可以让你看到 IPv4 和 IPv6,非常有趣
Dialogue: 0,0:01:49.52,0:01:52.36,English,,0,0,0,,And it's one of the interesting features the code in the book
Dialogue: 0,0:01:49.52,0:01:52.36,Chinese,,0,0,0,,这是本书代码中有趣的功能之一
Dialogue: 0,0:01:52.36,0:01:55.76,English,,0,0,0,,And it's new to the third edition is it uses a new api
Dialogue: 0,0:01:52.36,0:01:55.76,Chinese,,0,0,0,,它是第三版的新功能,它使用了新的 API
Dialogue: 0,0:01:56.38,0:02:03.28,English,,0,0,0,,That lets you pretty easily write code that will work under either of these two versions of the internet protocol
Dialogue: 0,0:01:56.38,0:02:03.28,Chinese,,0,0,0,,这使你可以非常轻松地编写可在这两个版本的 Internet 协议之下运行的代码
Dialogue: 0,0:02:04.62,0:02:06.26,English,,0,0,0,,So for example
Dialogue: 0,0:02:04.62,0:02:06.26,Chinese,,0,0,0,,举个例子
Dialogue: 0,0:02:08.44,0:02:10.58,English,,0,0,0,,With this slightly modified program
Dialogue: 0,0:02:08.44,0:02:10.58,Chinese,,0,0,0,,通过这个略微修改的程序
Dialogue: 0,0:02:11.54,0:02:14.92,English,,0,0,0,,If I now look up the IP address for facebook
Dialogue: 0,0:02:11.54,0:02:14.92,Chinese,,0,0,0,,如果我现在查找 facebook 的 IP 地址
Dialogue: 0,0:02:16.80,0:02:19.02,English,,0,0,0,,You'll see it comes back with two different ones
Dialogue: 0,0:02:16.80,0:02:19.02,Chinese,,0,0,0,,它返回了 2 个不同的 IP 地址
Dialogue: 0,0:02:21.16,0:02:22.02,English,,0,0,0,,And the
Dialogue: 0,0:02:21.16,0:02:22.02,Chinese,,0,0,0,,而且
Dialogue: 0,0:02:25.26,0:02:31.46,English,,0,0,0,,The first one is the familiar dotted decimal form where each recall that each of these digits
Dialogue: 0,0:02:25.26,0:02:31.46,Chinese,,0,0,0,,第一个是熟悉的点分十进制形式,我们一个一个字节的看
Dialogue: 0,0:02:31.46,0:02:35.08,English,,0,0,0,,It's a decimal representation of of a single byte
Dialogue: 0,0:02:31.46,0:02:35.08,Chinese,,0,0,0,,它是单个字节的十进制表示
Dialogue: 0,0:02:35.22,0:02:37.66,English,,0,0,0,,So it's a number between 0 and 255
Dialogue: 0,0:02:35.22,0:02:37.66,Chinese,,0,0,0,,所以这是 0 到 255 之间的数字
Dialogue: 0,0:02:38.32,0:02:46.06,English,,0,0,0,,This is a IPv6 address which is I told you is 128 bits long
Dialogue: 0,0:02:38.32,0:02:46.06,Chinese,,0,0,0,,(第二个地址)这是一个 IPv6 地址, 128 位长
Dialogue: 0,0:02:46.18,0:02:53.18,English,,0,0,0,,So that's to the seventh takeaway 3 to the fifth 32 bytes right
Dialogue: 0,0:02:46.18,0:02:53.18,Chinese,,0,0,0,,所以这是 2^7 , 32 个字节(教授口误,此处应为 16 个字节)
Dialogue: 0,0:02:54.20,0:02:56.52,English,,0,0,0,,And it's usually written in this hex format
Dialogue: 0,0:02:54.20,0:02:56.52,Chinese,,0,0,0,,它通常以这种十六进制格式编写
Dialogue: 0,0:02:58.06,0:03:01.84,English,,0,0,0,,With : be after every pair of bytes
Dialogue: 0,0:02:58.06,0:03:01.84,Chinese,,0,0,0,,在每两个字节用 : 分隔
Dialogue: 0,0:03:02.68,0:03:05.74,English,,0,0,0,,And I think the : : just means that's a zero there
Dialogue: 0,0:03:02.68,0:03:05.74,Chinese,,0,0,0,, : : 之间没有数字说明那里是零
Dialogue: 0,0:03:06.24,0:03:16.38,English,,0,0,0,,When the cute things facebook did was embed face as a hex code inside of their IPv6 address
Dialogue: 0,0:03:06.24,0:03:16.38,Chinese,,0,0,0,,有趣的是 facebook 把 face (16 进制字符串)放在了他们的 IPv6 地址中
Dialogue: 0,0:03:23.84,0:03:24.72,English,,0,0,0,,And
Dialogue: 0,0:03:23.84,0:03:24.72,Chinese,,0,0,0,,嗯......
Dialogue: 0,0:03:28.32,0:03:30.20,English,,0,0,0,,That's really different I get very different results
Dialogue: 0,0:03:28.32,0:03:30.20,Chinese,,0,0,0,,得到了完全不一样的结果
Dialogue: 0,0:03:30.88,0:03:32.96,English,,0,0,0,,Running this at home and then I do here
Dialogue: 0,0:03:30.88,0:03:32.96,Chinese,,0,0,0,,在家里运行和在这里运行的结果不一样
Dialogue: 0,0:03:33.26,0:03:39.62,English,,0,0,0,,And as I mentioned you'll get different results running a DNS server depending on where in the country are
Dialogue: 0,0:03:33.26,0:03:39.62,Chinese,,0,0,0,,正如我所提到的,由于国家/地区的不同,你从 DNS 服务器获取的结果不同
Dialogue: 0,0:03:40.74,0:03:42.72,English,,0,0,0,,Uh if you repeat the same thing over and over again
Dialogue: 0,0:03:40.74,0:03:42.72,Chinese,,0,0,0,,如果你一遍又一遍地重复同样的事情
Dialogue: 0,0:03:42.72,0:03:43.84,English,,0,0,0,,You'll get different results
Dialogue: 0,0:03:42.72,0:03:43.84,Chinese,,0,0,0,,你也会得到不同的结果
Dialogue: 0,0:03:43.84,0:03:50.52,English,,0,0,0,,And that's because these very large companies are providing servers that are stationed all over the world
Dialogue: 0,0:03:43.84,0:03:50.52,Chinese,,0,0,0,,这是因为一些大公司在世界各地都部署了服务器
Dialogue: 0,0:03:51.02,0:03:56.54,English,,0,0,0,,And as a way to sort of balance out the load of the traffic they also have multiple server addresses
Dialogue: 0,0:03:51.02,0:03:56.54,Chinese,,0,0,0,,作为一种平衡流量负载的方法,它们拥有多台服务器地址
Dialogue: 0,0:03:57.12,0:04:02.56,English,,0,0,0,,That then they kind of rotate through to give you so that different people will get served by different machines
Dialogue: 0,0:03:57.12,0:04:02.56,Chinese,,0,0,0,,这些服务器轮流将地址发送,以便不同的人得到不同服务器的服务
Dialogue: 0,0:04:03.70,0:04:11.72,English,,0,0,0,,So you see for example (twitter) google just gave me two addresses and IPv4 and IPv6 address
Dialogue: 0,0:04:03.70,0:04:11.72,Chinese,,0,0,0,,例如 Google 只给了我两个地址,一个 IPv4 地址,一个 IPv6 地址
Dialogue: 0,0:04:12.34,0:04:15.12,English,,0,0,0,,Twitter gave me a for different IPv4 addresses
Dialogue: 0,0:04:12.34,0:04:15.12,Chinese,,0,0,0,, Twitter 返回了 4 个不同的 IPv4 地址
Dialogue: 0,0:04:18.16,0:04:21.36,English,,0,0,0,,So let's look then at the code
Dialogue: 0,0:04:18.16,0:04:21.36,Chinese,,0,0,0,,那么让我们看看代码
Dialogue: 0,0:04:22.08,0:04:25.38,English,,0,0,0,,I'll go back through some of what we talked about last time
Dialogue: 0,0:04:22.08,0:04:25.38,Chinese,,0,0,0,,我将回顾一下我们上次谈到的一些内容
Dialogue: 0,0:04:26.00,0:04:28.92,English,,0,0,0,,I quite honestly I spent a fair amount of time looking at it
Dialogue: 0,0:04:26.00,0:04:28.92,Chinese,,0,0,0,,老实说,我花了相当多的时间来探索它
Dialogue: 0,0:04:29.08,0:04:31.56,English,,0,0,0,,So I can really understand it better
Dialogue: 0,0:04:29.08,0:04:31.56,Chinese,,0,0,0,,所以我可以更好地理解它
Dialogue: 0,0:04:32.10,0:04:34.22,English,,0,0,0,,Because it's pretty it's not simple stuff
Dialogue: 0,0:04:32.10,0:04:34.22,Chinese,,0,0,0,,它很漂亮但也并不简单
Dialogue: 0,0:04:35.48,0:04:36.70,English,,0,0,0,,But as I mentioned that
Dialogue: 0,0:04:35.48,0:04:36.70,Chinese,,0,0,0,,但正如我提到的那样
Dialogue: 0,0:04:37.36,0:04:38.80,English,,0,0,0,,As we went over last time
Dialogue: 0,0:04:37.36,0:04:38.80,Chinese,,0,0,0,,我们回顾一下上次的
Dialogue: 0,0:04:39.54,0:04:42.52,English,,0,0,0,,This is sort of this giant block diagram that shows
Dialogue: 0,0:04:39.54,0:04:42.52,Chinese,,0,0,0,,这是一个概述图,它显示了
Dialogue: 0,0:04:43.06,0:04:47.86,English,,0,0,0,,All the activity involved in setting up and running both a server and a client
Dialogue: 0,0:04:43.06,0:04:47.86,Chinese,,0,0,0,,关于建立并运行服务端和客户端的一切事务
Dialogue: 0,0:04:48.76,0:04:50.08,English,,0,0,0,,And just to repeat
Dialogue: 0,0:04:48.76,0:04:50.08,Chinese,,0,0,0,,重复一遍
Dialogue: 0,0:04:50.74,0:04:54.70,English,,0,0,0,,The idea is you need to the server needs to fire up
Dialogue: 0,0:04:50.74,0:04:54.70,Chinese,,0,0,0,,核心思想是当你必须要启动服务器
Dialogue: 0,0:04:54.72,0:04:58.78,English,,0,0,0,,You can't contact a server until that server exists
Dialogue: 0,0:04:54.72,0:04:58.78,Chinese,,0,0,0,,如果服务器不存在,那么你不能和它通信
Dialogue: 0,0:04:59.04,0:05:02.12,English,,0,0,0,,So it's the server is the thing that has to exist first
Dialogue: 0,0:04:59.04,0:05:02.12,Chinese,,0,0,0,,所以首先要确保服务器启动
Dialogue: 0,0:05:02.70,0:05:09.30,English,,0,0,0,,And all these steps are what are required for a server to begin on a machine
Dialogue: 0,0:05:02.70,0:05:09.30,Chinese,,0,0,0,,服务器在计算机上启动需要进行上面所有的这些步骤
Dialogue: 0,0:05:09.30,0:05:12.60,English,,0,0,0,,And then to accept a connection request from a client
Dialogue: 0,0:05:09.30,0:05:12.60,Chinese,,0,0,0,,然后接受来自客户端的连接请求
Dialogue: 0,0:05:14.52,0:05:18.88,English,,0,0,0,,Similarly once the server is set then the client can begin
Dialogue: 0,0:05:14.52,0:05:18.88,Chinese,,0,0,0,,同样,一旦设置了服务器,客户端就可以开始请求
Dialogue: 0,0:05:19.32,0:05:22.52,English,,0,0,0,,And it's like placing a call to the server
Dialogue: 0,0:05:19.32,0:05:22.52,Chinese,,0,0,0,,这就像给服务器打电话一样
Dialogue: 0,0:05:23.04,0:05:25.02,English,,0,0,0,,And it makes a connection request
Dialogue: 0,0:05:23.04,0:05:25.02,Chinese,,0,0,0,,它会发出连接请求
Dialogue: 0,0:05:25.60,0:05:27.48,English,,0,0,0,,And that's accepted by the server
Dialogue: 0,0:05:25.60,0:05:27.48,Chinese,,0,0,0,,这里已经被服务器接受了
Dialogue: 0,0:05:27.96,0:05:32.22,English,,0,0,0,,And then what's called a session begins meaning an ability for these two
Dialogue: 0,0:05:27.96,0:05:32.22,Chinese,,0,0,0,,这样所谓的会话就会开始,这意味着这样两台机器
Dialogue: 0,0:05:32.78,0:05:37.87,English,,0,0,0,,Machines to talk back and for worth over an internet connection
Dialogue: 0,0:05:32.78,0:05:37.87,Chinese,,0,0,0,,可以通过互联网进行请求和回复
Dialogue: 0,0:05:38.54,0:05:42.70,English,,0,0,0,,And they'll do it this example I'll show is done by reading and writing
Dialogue: 0,0:05:38.54,0:05:42.70,Chinese,,0,0,0,,通过读和写来完成这样的会话
Dialogue: 0,0:05:43.22,0:05:49.08,English,,0,0,0,,As if they were file,a file each end looks like it's reading and writing a file
Dialogue: 0,0:05:43.22,0:05:49.08,Chinese,,0,0,0,,它们就像文件,每一端就像正在读写一个文件
Dialogue: 0,0:05:52.08,0:05:57.92,English,,0,0,0,,And then once the client is done with whatever it's trying to get
Dialogue: 0,0:05:52.08,0:05:57.92,Chinese,,0,0,0,,然后,一旦客户端得到它想要获得的东西
Dialogue: 0,0:05:58.60,0:06:01.30,English,,0,0,0,,It will disconnect
Dialogue: 0,0:05:58.60,0:06:01.30,Chinese,,0,0,0,,它将断开连接
Dialogue: 0,0:06:02.28,0:06:03.68,English,,0,0,0,,Since we hang up its phone
Dialogue: 0,0:06:02.28,0:06:03.68,Chinese,,0,0,0,,因为我们挂了电话
Dialogue: 0,0:06:04.76,0:06:10.74,English,,0,0,0,,And then the listening the server will detect this by receiving an end of file
Dialogue: 0,0:06:04.76,0:06:10.74,Chinese,,0,0,0,,当监听服务器试图从客户端读取时
Dialogue: 0,0:06:10.74,0:06:13.02,English,,0,0,0,,When it attempts to read from the client
Dialogue: 0,0:06:10.74,0:06:13.02,Chinese,,0,0,0,,将通过接收文件结束来检测到这一点
Dialogue: 0,0:06:13.74,0:06:19.70,English,,0,0,0,,And then it will close that connection but and this I didn't have stated quite properly last time
Dialogue: 0,0:06:13.74,0:06:19.70,Chinese,,0,0,0,,然后它会关闭那个连接,但是我上次没有说得很清楚
Dialogue: 0,0:06:20.42,0:06:24.60,English,,0,0,0,,This it's not going to actually shut down at that point it's just going to return back
Dialogue: 0,0:06:20.42,0:06:24.60,Chinese,,0,0,0,,它实际上不会关闭,只会返回
Dialogue: 0,0:06:25.42,0:06:27.12,English,,0,0,0,,And accept a new connection request
Dialogue: 0,0:06:25.42,0:06:27.12,Chinese,,0,0,0,,并接受新的连接请求
Dialogue: 0,0:06:27.66,0:06:32.42,English,,0,0,0,,And this is what's known as an iterative server it can only handle a single client at a time
Dialogue: 0,0:06:27.66,0:06:32.42,Chinese,,0,0,0,,这就是所谓的迭代服务器,它一次只能处理一个客户端
Dialogue: 0,0:06:32.96,0:06:39.72,English,,0,0,0,,Later in the course we'll talk about how to do a multi-threaded server that can handle multiple connections at one time
Dialogue: 0,0:06:32.96,0:06:39.72,Chinese,,0,0,0,,在本课程的后面,我们将讨论如何创建一个可以同时处理多个连接的多线程服务器
Dialogue: 0,0:06:40.66,0:06:42.58,English,,0,0,0,,So this is a pretty basic version
Dialogue: 0,0:06:40.66,0:06:42.58,Chinese,,0,0,0,,所以这是一个非常基本的例子
Dialogue: 0,0:06:44.98,0:06:57.02,English,,0,0,0,,So the first step on either the client or the server side is this very hairy messy relatively new api called getaddrinfo
Dialogue: 0,0:06:44.98,0:06:57.02,Chinese,,0,0,0,,客户端和服务器端的第一步都是这个很复杂、混乱、相对较新的 api ,称为 getaddrinfo
Dialogue: 0,0:06:57.52,0:07:03.02,English,,0,0,0,,And the idea of getaddrinfo is it's a sort of kitchen sink a Swiss army knife for you
Dialogue: 0,0:06:57.52,0:07:03.02,Chinese,,0,0,0,, getaddrinfo 是一个非常强大的函数,它就像一把瑞士军刀
Dialogue: 0,0:07:03.04,0:07:07.80,English,,0,0,0,,All those expressions of it has a zillion features
Dialogue: 0,0:07:03.04,0:07:07.80,Chinese,,0,0,0,,它具有很多功能
Dialogue: 0,0:07:08.26,0:07:13.42,English,,0,0,0,,To that sort of subsume a lot of other sort of individual protocols
Dialogue: 0,0:07:08.26,0:07:13.42,Chinese,,0,0,0,,包含了很多其他的协议
Dialogue: 0,0:07:13.52,0:07:17.90,English,,0,0,0,,So for example it's pretty much the same whether it's IPv4 IPv6
Dialogue: 0,0:07:13.52,0:07:17.90,Chinese,,0,0,0,,例如, IPv4,IPv6 都可以使用它
Dialogue: 0,0:07:18.64,0:07:27.68,English,,0,0,0,,It lets you sort of go forward from and a domain name to an IP address
Dialogue: 0,0:07:18.64,0:07:27.68,Chinese,,0,0,0,,它支持从域名到 IP 地址的转换
Dialogue: 0,0:07:27.82,0:07:31.34,English,,0,0,0,,Lets you go backward from an IP address back to a domain name
Dialogue: 0,0:07:27.82,0:07:31.34,Chinese,,0,0,0,,也支持从 IP 地址返回到域名的转换
Dialogue: 0,0:07:31.69,0:07:39.08,English,,0,0,0,,It supports all of those things it will let you look up what is the port number associated with an http server
Dialogue: 0,0:07:31.69,0:07:39.08,Chinese,,0,0,0,,它支持所有这些功能,这可以让你查找与 http 服务器关联的端口号
Dialogue: 0,0:07:39.20,0:07:41.70,English,,0,0,0,,So it will let you do all kinds of different things
Dialogue: 0,0:07:39.20,0:07:41.70,Chinese,,0,0,0,,你可以用它来做所有不同的事情
Dialogue: 0,0:07:43.22,0:07:51.50,English,,0,0,0,,And as a result it's a big and messy api with zillions features and parameters and and so forth
Dialogue: 0,0:07:43.22,0:07:51.50,Chinese,,0,0,0,,因此,它是一个十分庞杂的 api ,具有很多功能和参数
Dialogue: 0,0:07:55.26,0:08:05.44,English,,0,0,0,,So you recall from last time that the data structures we use to refer to a connection
Dialogue: 0,0:07:55.26,0:08:05.44,Chinese,,0,0,0,,回想一下上次课我们使用 sockaddr 这种数据结构来表示网络连接
Dialogue: 0,0:08:06.40,0:08:11.42,English,,0,0,0,,Whether it's an IPv4 or an IPv6 connection is called a sockaddr
Dialogue: 0,0:08:06.40,0:08:11.42,Chinese,,0,0,0,,无论是 IPv4 还是 IPv6 连接都是使用这种数据结构
Dialogue: 0,0:08:12.16,0:08:15.90,English,,0,0,0,,And as I mentioned this is sort of the C version of sub typing
Dialogue: 0,0:08:12.16,0:08:15.90,Chinese,,0,0,0,,说起来这是一种 C 语言版本的子类
Dialogue: 0,0:08:15.96,0:08:20.50,English,,0,0,0,,If you know a language with a type inheritance, this is how it's done
Dialogue: 0,0:08:15.96,0:08:20.50,Chinese,,0,0,0,,如果你了解有继承特性的编程语言,那是怎样处理的
Dialogue: 0,0:08:20.50,0:08:22.20,English,,0,0,0,,In C it's done by casting
Dialogue: 0,0:08:20.50,0:08:22.20,Chinese,,0,0,0,,但在 C 语言中就是这样
Dialogue: 0,0:08:22.88,0:08:26.40,English,,0,0,0,,And so in particular the sort of generic socket address
Dialogue: 0,0:08:22.88,0:08:26.40,Chinese,,0,0,0,,特别是在一般的套接字地址中
Dialogue: 0,0:08:26.44,0:08:32.06,English,,0,0,0,,All it needs to know is a two byte field at the front that specifies what kind of socket is this
Dialogue: 0,0:08:26.44,0:08:32.06,Chinese,,0,0,0,,要知道套接字的类型,只需通过数据中最前面两个字节的值就可以判断
Dialogue: 0,0:08:33.20,0:08:43.26,English,,0,0,0,,And then in particular, we'll be looking at what's called a sockaddr_in which is an IPv4 version of a socket
Dialogue: 0,0:08:33.20,0:08:43.26,Chinese,,0,0,0,,我们要特别注意的是,要知道那个是 IPv4 的套接字可以在 sockaddr_in 中查找
Dialogue: 0,0:08:43.44,0:08:49.38,English,,0,0,0,,And so what it needs to know is a port number which is a two byte integer
Dialogue: 0,0:08:43.44,0:08:49.38,Chinese,,0,0,0,,接下来要了解的是端口号,由两个字节组成的无符号数
Dialogue: 0,0:08:50.02,0:08:56.58,English,,0,0,0,,And a IP address Which is a 4 byte address for IPv4
Dialogue: 0,0:08:52.12,0:08:56.58,Chinese,,0,0,0,,和 IP 地址,IPv4 的地址是 4 字节
Dialogue: 0,0:08:59.02,0:09:02.44,English,,0,0,0,,Both of those are actually stored in this structure as I mentioned in
Dialogue: 0,0:08:59.02,0:09:02.44,Chinese,,0,0,0,,正如我上面提到的那样,这两个也都是保存在这个结构中
Dialogue: 0,0:09:02.82,0:09:06.56,English,,0,0,0,,In big-endian order even if you're running it on a little endian machine
Dialogue: 0,0:09:02.82,0:09:06.56,Chinese,,0,0,0,,即使你在一台小端机上运行它,也会以大端法的顺序存放
Dialogue: 0,0:09:07.32,0:09:09.78,English,,0,0,0,,And one of the interesting features of that
Dialogue: 0,0:09:07.32,0:09:09.78,Chinese,,0,0,0,,这里有个有趣的特点
Dialogue: 0,0:09:10.32,0:09:18.78,English,,0,0,0,,This api,this getaddrinfo,it's largely removed the need for you as a programmer to worry about byte ordering
Dialogue: 0,0:09:10.32,0:09:18.78,Chinese,,0,0,0,,getaddrinfo 这个 api 在很大程度上消除了作为程序员的你对字节排序的担心
Dialogue: 0,0:09:19.04,0:09:20.56,English,,0,0,0,,And that's a nice feature
Dialogue: 0,0:09:19.04,0:09:20.56,Chinese,,0,0,0,,这是一个很好的功能
Dialogue: 0,0:09:21.18,0:09:24.06,English,,0,0,0,,So in general for example and getaddrinfo
Dialogue: 0,0:09:21.18,0:09:24.06,Chinese,,0,0,0,,举例说明一下 getadrrinfo
Dialogue: 0,0:09:24.06,0:09:28.46,English,,0,0,0,,If I want to pass an port number, I'll pass it as a character string
Dialogue: 0,0:09:24.06,0:09:28.46,Chinese,,0,0,0,,如果我要传递一个端口号,我会以字符串的形式传递
Dialogue: 0,0:09:29.24,0:09:33.56,English,,0,0,0,,A string representation of a decimal number in other words
Dialogue: 0,0:09:29.24,0:09:33.56,Chinese,,0,0,0,,换句话说就是用十进制数的字符串表示形式
Dialogue: 0,0:09:34.20,0:09:41.62,English,,0,0,0,,And then the code here within this api will automatically turn that into a 16-bit integer
Dialogue: 0,0:09:34.20,0:09:41.62,Chinese,,0,0,0,,这个代码中使用这个 api 会自动将其转换为 16 位整数形式
Dialogue: 0,0:09:41.62,0:09:43.52,English,,0,0,0,,And put it in the proper byte order
Dialogue: 0,0:09:41.62,0:09:43.52,Chinese,,0,0,0,,并按正确的字节顺序排列
Dialogue: 0,0:09:44.16,0:09:49.50,English,,0,0,0,,And same with how it takes use of this 32 bit IP address
Dialogue: 0,0:09:44.16,0:09:49.50,Chinese,,0,0,0,,同样的 32 位的 IP 地址也是这种方式
Dialogue: 0,0:09:50.16,0:09:53.80,English,,0,0,0,,And you want to get it into dotted decimal form it will do that for you
Dialogue: 0,0:09:50.16,0:09:53.80,Chinese,,0,0,0,,如果你想要点分十进制的形式的 IP 地址也是可以的
Dialogue: 0,0:09:54.26,0:09:57.88,English,,0,0,0,,Without you having to worry about byte order so it's kind of a nice thing
Dialogue: 0,0:09:54.26,0:09:57.88,Chinese,,0,0,0,,你不必担心任何字节顺序的问题,非常人性化
Dialogue: 0,0:10:06.54,0:10:10.02,English,,0,0,0,,So getaddrinfo has a whole bunch of arguments
Dialogue: 0,0:10:06.54,0:10:10.02,Chinese,,0,0,0,, getaddrinfo 有很多参数
Dialogue: 0,0:10:10.48,0:10:16.86,English,,0,0,0,,And some of them are essentially outputs,some of them are inputs Things you're passing into the function
Dialogue: 0,0:10:10.48,0:10:16.86,Chinese,,0,0,0,,这些你传递给函数的参数,其中一些参数用于输入,而其中一些其实上用于输出
Dialogue: 0,0:10:17.18,0:10:20.94,English,,0,0,0,,And others are really more like outputs how it gets information back to you
Dialogue: 0,0:10:17.18,0:10:20.94,Chinese,,0,0,0,,还些像是输出的问题,比如如何把获取到的信息返回给你
Dialogue: 0,0:10:21.68,0:10:22.64,English,,0,0,0,,So in particular
Dialogue: 0,0:10:21.68,0:10:22.64,Chinese,,0,0,0,,具体来说
Dialogue: 0,0:10:23.62,0:10:28.52,English,,0,0,0,,The first one is just the name of the host and that can be either a domain name
Dialogue: 0,0:10:23.62,0:10:28.52,Chinese,,0,0,0,,第一个参数 host 是主机的名称,也可以是域名
Dialogue: 0,0:10:29.18,0:10:38.10,English,,0,0,0,,Or a dotted decimal name or one of those funky looking things with the const that you solve for IPv6
Dialogue: 0,0:10:29.18,0:10:38.10,Chinese,,0,0,0,,或点分十进制 IP 地址,或者井分16 进制的 IPv6 地址
Dialogue: 0,0:10:39.12,0:10:43.98,English,,0,0,0,,The service is usually if you're looking up so if we're using this to look up
Dialogue: 0,0:10:39.12,0:10:43.98,Chinese,,0,0,0,, *service 这个参数用与查找
Dialogue: 0,0:10:44.62,0:10:46.44,English,,0,0,0,,That'll actually be a null pointer
Dialogue: 0,0:10:44.62,0:10:46.44,Chinese,,0,0,0,,实际上它是一个空指针
Dialogue: 0,0:10:47.10,0:10:54.96,English,,0,0,0,,And basically what typically you'll have is one of those two will be a string and the other will be a null pointer
Dialogue: 0,0:10:47.10,0:10:54.96,Chinese,,0,0,0,,实际上你只须提供 host 或 service 其中一个就可以了,而另外一个设置为空指针
Dialogue: 0,0:10:55.02,0:11:00.50,English,,0,0,0,,And the reason for that is the same function can be used either to look up a service like
Dialogue: 0,0:10:55.02,0:11:00.50,Chinese,,0,0,0,,原因是这个函数可以用来查找服务相关信息
Dialogue: 0,0:11:00.94,0:11:03.88,English,,0,0,0,,What is the port number associated with http
Dialogue: 0,0:11:00.94,0:11:03.88,Chinese,,0,0,0,,比如与 http 协议关联的端口号
Dialogue: 0,0:11:04.68,0:11:10.22,English,,0,0,0,,Or it can be what is the IP address of www.cmu.edu
Dialogue: 0,0:11:04.68,0:11:10.22,Chinese,,0,0,0,,或者查找 www.cmu.edu 的 IP 地址
Dialogue: 0,0:11:11.44,0:11:14.40,English,,0,0,0,,The hints is something that you're passing in
Dialogue: 0,0:11:11.44,0:11:14.40,Chinese,,0,0,0,,参数 hints 用于输入信息
Dialogue: 0,0:11:15.06,0:11:22.00,English,,0,0,0,,And it's basically as you fill in a small amount of a fairly large body of information
Dialogue: 0,0:11:15.06,0:11:22.00,Chinese,,0,0,0,,而它基本上是作为你填写少量的相当大的信息量
Dialogue: 0,0:11:22.02,0:11:26.12,English,,0,0,0,,About what specifically are you looking for in this request
Dialogue: 0,0:11:22.02,0:11:26.12,Chinese,,0,0,0,,关于你在此请求中具体寻找什么?
Dialogue: 0,0:11:26.12,0:11:32.50,English,,0,0,0,,Are you looking for an IPv6 information? Are you looking for IPv4 and so forth
Dialogue: 0,0:11:26.12,0:11:32.50,Chinese,,0,0,0,,你是在寻在 IPv4 的信息还是 IPv6 的信息等等
Dialogue: 0,0:11:34.34,0:11:36.76,English,,0,0,0,,And then what it returns back
Dialogue: 0,0:11:34.34,0:11:36.76,Chinese,,0,0,0,,那么这将返回什么呢?
Dialogue: 0,0:11:37.32,0:11:46.84,English,,0,0,0,,That's why you pass a ** ,it will return back a linked list that it's generated of results
Dialogue: 0,0:11:37.32,0:11:46.84,Chinese,,0,0,0,,这就是为什么你传递一个**,它将返回一个链接的结果列表
Dialogue: 0,0:11:47.86,0:11:50.38,English,,0,0,0,,And the reason for that as I mentioned is
Dialogue: 0,0:11:47.86,0:11:50.38,Chinese,,0,0,0,,这样的原因是
Dialogue: 0,0:11:51.14,0:11:57.36,English,,0,0,0,,That IP addresses are sort of many to many mapping potentially there as you saw
Dialogue: 0,0:11:51.14,0:11:57.36,Chinese,,0,0,0,,正如你所看到的那样,IP 地址可能会有多对多映射
Dialogue: 0,0:11:57.80,0:12:01.58,English,,0,0,0,,It's possible for a single domain name to have multiple IP addresses
Dialogue: 0,0:11:57.80,0:12:01.58,Chinese,,0,0,0,,有可能单个域名拥有多个 IP 地址
Dialogue: 0,0:12:02.18,0:12:05.44,English,,0,0,0,,It's possible for a single IP address to have multiple domain names
Dialogue: 0,0:12:02.18,0:12:05.44,Chinese,,0,0,0,,也可能单个 IP 地址有多个域名
Dialogue: 0,0:12:05.96,0:12:11.60,English,,0,0,0,,And so this is how they handle that sort of fact that they return a whole list of results
Dialogue: 0,0:12:05.96,0:12:11.60,Chinese,,0,0,0,,所以就返回整个结果列表来应对这样的情况
Dialogue: 0,0:12:13.40,0:12:16.78,English,,0,0,0,,And associated with that once that result gets used
Dialogue: 0,0:12:13.40,0:12:16.78,Chinese,,0,0,0,,一旦结果被使用就建立了关联
Dialogue: 0,0:12:16.78,0:12:20.44,English,,0,0,0,,Once you done with it you're supposed to call a function called freeaddrinfo
Dialogue: 0,0:12:16.78,0:12:20.44,Chinese,,0,0,0,,一旦你调用完它,还应该调用一个名为 freeaddrinfo 的函数
Dialogue: 0,0:12:20.88,0:12:26.06,English,,0,0,0,,That will basically dispose of all the elements of that linked list you don't have to do it yourself with free
Dialogue: 0,0:12:20.88,0:12:26.06,Chinese,,0,0,0,,它可以处理该链接列表的所有元素,你可以很轻松的让它来完成
Dialogue: 0,0:12:26.86,0:12:31.26,English,,0,0,0,,And then there's a another helper function called gai_strerror
Dialogue: 0,0:12:26.86,0:12:31.26,Chinese,,0,0,0,,然后是另一个有用的函数名为 gai_strerror
Dialogue: 0,0:12:31.90,0:12:34.90,English,,0,0,0,,That you use if there's an error code to help you interpret that error
Dialogue: 0,0:12:31.90,0:12:34.90,Chinese,,0,0,0,,如果有错误代码,你可以用它来帮助你解释错误
Dialogue: 0,0:12:36.80,0:12:38.06,English,,0,0,0,,And it returns an error code
Dialogue: 0,0:12:36.80,0:12:38.06,Chinese,,0,0,0,,并返回错误代码
Dialogue: 0,0:12:40.26,0:12:44.50,English,,0,0,0,,So as I mentioned it returns up a linked list
Dialogue: 0,0:12:40.26,0:12:44.50,Chinese,,0,0,0,,我提到它会返回一个链表
Dialogue: 0,0:12:45.12,0:12:51.40,English,,0,0,0,,That result is a linked list of these a get of addr structs, addrinfo structs
Dialogue: 0,0:12:45.12,0:12:51.40,Chinese,,0,0,0,,这个结果是这些 addrinfo 结构体组成的链表
Dialogue: 0,0:12:53.14,0:12:56.08,English,,0,0,0,,And the addrinfo struct has a zillion fields
Dialogue: 0,0:12:53.14,0:12:56.08,Chinese,,0,0,0,, addrinfo 结构体有很多成员
Dialogue: 0,0:12:56.94,0:12:59.34,English,,0,0,0,,Some of which you'll use and some which you won't
Dialogue: 0,0:12:56.94,0:12:59.34,Chinese,,0,0,0,,其中一些你会使用到,一些你不会
Dialogue: 0,0:13:01.94,0:13:05.26,English,,0,0,0,,This is for example when you're passing in hints
Dialogue: 0,0:13:01.94,0:13:05.26,Chinese,,0,0,0,,例如,当你传递 hints 参数的时候
Dialogue: 0,0:13:06.78,0:13:11.40,English,,0,0,0,,Hints is sort of that what you're passing in the information you know some conditions about
Dialogue: 0,0:13:06.78,0:13:11.40,Chinese,,0,0,0,, Hints 就是你传递给函数的信息,你可以通过它来设置一些条件用来
Dialogue: 0,0:13:11.70,0:13:13.68,English,,0,0,0,,What you're looking for in this request
Dialogue: 0,0:13:11.70,0:13:13.68,Chinese,,0,0,0,,定义你想在这个请求中寻找什么
Dialogue: 0,0:13:17.24,0:13:21.20,English,,0,0,0,,And then what some of these you pass in and some of it returns
Dialogue: 0,0:13:17.24,0:13:21.20,Chinese,,0,0,0,,你传递的这些内容的其中一些会返回
Dialogue: 0,0:13:21.78,0:13:24.76,English,,0,0,0,,And so some of these fields are ones that then you can use
Dialogue: 0,0:13:21.78,0:13:24.76,Chinese,,0,0,0,,这是一些参数你可以用来
Dialogue: 0,0:13:25.16,0:13:30.88,English,,0,0,0,,When you're making calls to other socket functions,functions in the socket library
Dialogue: 0,0:13:25.16,0:13:30.88,Chinese,,0,0,0,,调用套接字库中的其他套接字函数
Dialogue: 0,0:13:31.32,0:13:37.80,English,,0,0,0,,It'll fill in instead of you having to worry about what's the structure the format of a sockaddr in
Dialogue: 0,0:13:31.32,0:13:37.80,Chinese,,0,0,0,,它将填入,而不是让你担心 sockaddr 的结构和格式
Dialogue: 0,0:13:37.80,0:13:40.60,English,,0,0,0,, and how do I convert into network order and stuff
Dialogue: 0,0:13:37.84,0:13:40.60,Chinese,,0,0,0,,以及如何转换为网络顺序和内容
Dialogue: 0,0:13:41.20,0:13:42.84,English,,0,0,0,,It will do all that for you
Dialogue: 0,0:13:41.20,0:13:42.84,Chinese,,0,0,0,,它会为你做所有这些
Dialogue: 0,0:13:46.88,0:13:52.56,English,,0,0,0,,So it's a little confusing to use though because sometimes the stuff you're providing is stuff you're giving it
Dialogue: 0,0:13:46.88,0:13:52.56,Chinese,,0,0,0,,所以使用起来有点混乱,因为有时候你提供的东西就是你给它的东西
Dialogue: 0,0:13:52.56,0:13:55.36,English,,0,0,0,, and sometimes it's stuff that it's returning back to you
Dialogue: 0,0:13:52.56,0:13:55.36,Chinese,,0,0,0,,有时又是返回给你的
Dialogue: 0,0:13:59.72,0:14:01.64,English,,0,0,0,,And then there's an inverse function
Dialogue: 0,0:13:59.72,0:14:01.64,Chinese,,0,0,0,,这里有一个与之相反的函数
Dialogue: 0,0:14:01.76,0:14:04.90,English,,0,0,0,,That so getaddrinfo is given a
Dialogue: 0,0:14:01.76,0:14:04.90,Chinese,,0,0,0,,这样 getaddrinfo 给了一个......
Dialogue: 0,0:14:09.06,0:14:14.32,English,,0,0,0,,It's actually give me the address information about some name
Dialogue: 0,0:14:09.06,0:14:14.32,Chinese,,0,0,0,,它实际上返回了地址名称信息
Dialogue: 0,0:14:15.50,0:14:19.34,English,,0,0,0,,And getnameinfo is I give you an address
Dialogue: 0,0:14:15.50,0:14:19.34,Chinese,,0,0,0,,但 getnameinfo 函数是给一个地址
Dialogue: 0,0:14:19.88,0:14:24.88,English,,0,0,0,,And you give me back some documentation about it including potentially a domain name
Dialogue: 0,0:14:19.88,0:14:24.88,Chinese,,0,0,0,,你给我一些关于它的文档,包括潜在的域名
Dialogue: 0,0:14:25.74,0:14:31.38,English,,0,0,0,,So there you pass in as a the first argument is a sockaddr struct
Dialogue: 0,0:14:25.74,0:14:31.38,Chinese,,0,0,0,,这里你需要传入的第一个参数是 sockaddr 结构体
Dialogue: 0,0:14:32.08,0:14:35.86,English,,0,0,0,,Remember for this lecture and I think in the book in a few places
Dialogue: 0,0:14:32.08,0:14:35.86,Chinese,,0,0,0,,我记得在书里面的这节有几个地方
Dialogue: 0,0:14:35.86,0:14:39.86,English,,0,0,0,,We've sort of created a typedef to alias
Dialogue: 0,0:14:35.86,0:14:39.86,Chinese,,0,0,0,,使用了 typedef 来定义别名
Dialogue: 0,0:14:40.52,0:14:45.02,English,,0,0,0,,So we don't have to say struct sockaddr all over the place but that's what the SA is
Dialogue: 0,0:14:40.52,0:14:45.02,Chinese,,0,0,0,,所以不用在所有地方都使用 sockaddr,而是以 SA 来代替
Dialogue: 0,0:14:46.96,0:14:54.70,English,,0,0,0,,And it will provide back information about that address
Dialogue: 0,0:14:46.96,0:14:54.70,Chinese,,0,0,0,,它将返回有关该地址的信息
Dialogue: 0,0:14:54.92,0:14:57.84,English,,0,0,0,,So if you passing in a 32 bit IP address
Dialogue: 0,0:14:54.92,0:14:57.84,Chinese,,0,0,0,,如果你传入一个 32 位的 IP 地址
Dialogue: 0,0:14:58.40,0:15:02.30,English,,0,0,0,,You can get it to return either a dotted decimal version of that
Dialogue: 0,0:14:58.40,0:15:02.30,Chinese,,0,0,0,,你可以从它返回中获得点分十进制版本的 IP 地址
Dialogue: 0,0:15:02.30,0:15:07.42,English,,0,0,0,,Or to do the DNS lookup and have it come back with the domain name for that
Dialogue: 0,0:15:02.30,0:15:07.42,Chinese,,0,0,0,,或者进行 DNS 查找并返回对应的域名
Dialogue: 0,0:15:11.16,0:15:17.60,English,,0,0,0,,So this is that program that I just demoed to you this one called hostinfo
Dialogue: 0,0:15:11.16,0:15:17.60,Chinese,,0,0,0,,这里有一个我写演示的程序,这个程序叫做 hostinfo
Dialogue: 0,0:15:17.70,0:15:20.74,English,,0,0,0,,So all it's really going to do is use these features to
Dialogue: 0,0:15:17.70,0:15:20.74,Chinese,,0,0,0,,这个程序的功能是......
Dialogue: 0,0:15:21.16,0:15:28.50,English,,0,0,0,,All on the command line give a domain name and it will just come back with a list of IP addresses
Dialogue: 0,0:15:21.16,0:15:28.50,Chinese,,0,0,0,,在命令行中给定一个域名,返回一个 IP 地址列表
Dialogue: 0,0:15:30.04,0:15:30.88,English,,0,0,0,,So
Dialogue: 0,0:15:30.04,0:15:30.88,Chinese,,0,0,0,,所以
Dialogue: 0,0:15:32.22,0:15:37.80,English,,0,0,0,,And this is pretty much the way you write code for this getaddrinfo api
Dialogue: 0,0:15:32.22,0:15:37.80,Chinese,,0,0,0,,这就是 getaddrinfo api 编写代码的方式
Dialogue: 0,0:15:39.02,0:15:45.04,English,,0,0,0,,That you typically so hints is the information I'm passing about what it is I'm looking for here
Dialogue: 0,0:15:39.02,0:15:45.04,Chinese,,0,0,0,, hints 就是我传递的关于我要找的内容的信息
Dialogue: 0,0:15:46.62,0:15:51.98,English,,0,0,0,,And so you start typically by zeroing out that entire field
Dialogue: 0,0:15:46.62,0:15:51.98,Chinese,,0,0,0,,因此,你通常会将整个字段归零
Dialogue: 0,0:15:53.68,0:15:56.56,English,,0,0,0,,And then setting the particular things you want
Dialogue: 0,0:15:53.68,0:15:56.56,Chinese,,0,0,0,,然后有选择的设置字段
Dialogue: 0,0:15:56.74,0:15:59.08,English,,0,0,0,,So in particular
Dialogue: 0,0:15:56.74,0:15:59.08,Chinese,,0,0,0,,特别是
Dialogue: 0,0:16:00.36,0:16:05.58,English,,0,0,0,,In this code that's...in the slides and the one I demo to you last time
Dialogue: 0,0:16:00.36,0:16:05.58,Chinese,,0,0,0,,在这段代码中,幻灯片和我上次向你演示的代码
Dialogue: 0,0:16:05.64,0:16:08.70,English,,0,0,0,,I'm only going to be looking for IPv4 connections
Dialogue: 0,0:16:05.64,0:16:08.70,Chinese,,0,0,0,,我只会寻找 IPv4 连接
Dialogue: 0,0:16:09.30,0:16:14.20,English,,0,0,0,,And so I use the constant called AF_INET which is defined in the sockets interface
Dialogue: 0,0:16:09.30,0:16:14.20,Chinese,,0,0,0,,所以我使用在套接字接口中定义的名为 AF_INET 的常量
Dialogue: 0,0:16:14.94,0:16:20.00,English,,0,0,0,,And also I'll say that I want a TCP connection what's called a socket stream
Dialogue: 0,0:16:14.94,0:16:20.00,Chinese,,0,0,0,,而且我会说我想要一个 TCP 连接,这就是套接字流
Dialogue: 0,0:16:20.54,0:16:28.84,English,,0,0,0,,Which if you recall is the sort of protocol that 99% of the the IP traffic in the world is carrying is on TCP
Dialogue: 0,0:16:20.54,0:16:28.84,Chinese,,0,0,0,,如果你还记得,就是世界上 99% 的 IP 流量都是基于 TCP 的
Dialogue: 0,0:16:29.34,0:16:35.78,English,,0,0,0,,Meaning it provides a somewhat higher level abstraction of what's going on of sending packets back and forth
Dialogue: 0,0:16:29.34,0:16:35.78,Chinese,,0,0,0,,这意味着它提供了对来回发送数据包的更高级别的抽象
Dialogue: 0,0:16:37.26,0:16:40.04,English,,0,0,0,,So that's as much hint as I'm giving it here
Dialogue: 0,0:16:37.26,0:16:40.04,Chinese,,0,0,0,,这就是提供的 hints 参数
Dialogue: 0,0:16:41.24,0:16:46.02,English,,0,0,0,,And now I'm making a call to getaddrinfo and this argument is the name
Dialogue: 0,0:16:41.24,0:16:46.02,Chinese,,0,0,0,,现在我正在调用 getaddrinfo,参数就是这个名字(域名)
Dialogue: 0,0:16:46.44,0:16:49.18,English,,0,0,0,,Which is I'm getting it right off of the command line here
Dialogue: 0,0:16:46.44,0:16:49.18,Chinese,,0,0,0,,我从命令行得到的
Dialogue: 0,0:16:49.26,0:16:55.54,English,,0,0,0,,So in my demo it was always the commit up a domain name
Dialogue: 0,0:16:49.26,0:16:55.54,Chinese,,0,0,0,,在我的演示中,它始终是提交域名
Dialogue: 0,0:16:55.92,0:16:58.10,English,,0,0,0,,I'm passing it NULL for the service
Dialogue: 0,0:16:55.92,0:16:58.10,Chinese,,0,0,0,,我传递 NULL 给 service 作为参数
Dialogue: 0,0:16:58.10,0:17:04.02,English,,0,0,0,,As I mentioned this call you typically have one of the two are is null but not both
Dialogue: 0,0:16:58.10,0:17:04.02,Chinese,,0,0,0,,我之前提到过,通常这两个参数中的一个是 NULL ,不会两个都是 NULL
Dialogue: 0,0:17:05.34,0:17:08.98,English,,0,0,0,,Hints is what I'm passing in is this is the kind of information I want
Dialogue: 0,0:17:05.34,0:17:08.98,Chinese,,0,0,0,, hints 是传递我想要的信息类型
Dialogue: 0,0:17:09.86,0:17:17.58,English,,0,0,0,,And listp is a will now become a pointer to the results that get returned by getaddrinfo
Dialogue: 0,0:17:09.86,0:17:17.58,Chinese,,0,0,0,,而 listp 现在将成为指向 getaddrinfo 返回结果的指针
Dialogue: 0,0:17:21.24,0:17:24.76,English,,0,0,0,,So that's the calling all of its quarry
Dialogue: 0,0:17:21.24,0:17:24.76,Chinese,,0,0,0,,这就是调用前所有的准备
Dialogue: 0,0:17:29.18,0:17:32.62,English,,0,0,0,,Okay and now I want to go back so I got one of those linked lists
Dialogue: 0,0:17:29.18,0:17:32.62,Chinese,,0,0,0,,好的,现在我想返回,我得到了一个链接列表
Dialogue: 0,0:17:32.78,0:17:37.30,English,,0,0,0,,And that will have a whole bunch of IP address of potentially multiple IP addresses in it
Dialogue: 0,0:17:32.78,0:17:37.30,Chinese,,0,0,0,,而这里面会有一大堆可能的 IP 地址
Dialogue: 0,0:17:37.80,0:17:39.56,English,,0,0,0,,Maybe just one maybe none
Dialogue: 0,0:17:37.80,0:17:39.56,Chinese,,0,0,0,,也许只有一个,也许一个都没有
Dialogue: 0,0:17:40.38,0:17:42.44,English,,0,0,0,,But some number of them in a linked list
Dialogue: 0,0:17:40.38,0:17:42.44,Chinese,,0,0,0,,但其中有一些在链表中
Dialogue: 0,0:17:43.08,0:17:47.84,English,,0,0,0,,And I want to walk through that list and get back the dotted decimal version of all those names
Dialogue: 0,0:17:43.08,0:17:47.84,Chinese,,0,0,0,,我会遍历该列表并获取所有这些名称中的点分十进制版本(的 IP 地址)
Dialogue: 0,0:17:48.36,0:17:53.98,English,,0,0,0,,Or the colon eyes of those IPv6 addresses I don't know how you refer to those by the way
Dialogue: 0,0:17:48.36,0:17:53.98,Chinese,,0,0,0,,或者那些 IPv6 地址的带冒号的形式,我不知道你们是怎么命名它们的
Dialogue: 0,0:17:54.86,0:17:58.60,English,,0,0,0,,I know relatively little about IPv6 I have to confess
Dialogue: 0,0:17:54.86,0:17:58.60,Chinese,,0,0,0,,我不得不承认我对 IPv6 的了解相对较少
Dialogue: 0,0:18:00.10,0:18:03.32,English,,0,0,0,,So then there's a function called Getnameinfo
Dialogue: 0,0:18:00.10,0:18:03.32,Chinese,,0,0,0,,接下来是一个名为 Getnameinfo 的函数
Dialogue: 0,0:18:03.32,0:18:09.66,English,,0,0,0,,Which usually you use to take a socket address IP address and go backward and get a domain name
Dialogue: 0,0:18:03.32,0:18:09.66,Chinese,,0,0,0,,通常这个函数用于通过 IP 地址获取对应的主机域名
Dialogue: 0,0:18:10.24,0:18:16.74,English,,0,0,0,,But if I tell it by a flag one of the final argument to it is a flag argument
Dialogue: 0,0:18:10.24,0:18:16.74,Chinese,,0,0,0,,但如果我设置了最后一个参数 flag 的值
Dialogue: 0,0:18:17.20,0:18:22.44,English,,0,0,0,,And say what I want back from this is not a domain name I wanted in this numeric format
Dialogue: 0,0:18:17.20,0:18:22.44,Chinese,,0,0,0,,告诉它我想要的不是主机域名而是数字形式的地址
Dialogue: 0,0:18:22.82,0:18:27.42,English,,0,0,0,,Either dotted decimal or the colons between hex digits
Dialogue: 0,0:18:22.82,0:18:27.42,Chinese,,0,0,0,,点分十进制或冒分十六进制
Dialogue: 0,0:18:28.80,0:18:31.20,English,,0,0,0,,So by passing that to Getnameinfo
Dialogue: 0,0:18:28.80,0:18:31.20,Chinese,,0,0,0,,通过传递这样的信息给 Getnameinfo
Dialogue: 0,0:18:31.84,0:18:39.80,English,,0,0,0,,What it's going to return and I'm passing in a buffer meaning of a char* And some size of that buffer
Dialogue: 0,0:18:31.84,0:18:39.80,Chinese,,0,0,0,,它将返回什么?我向它传入了 buf ,指向一个字符缓冲区,和缓冲区的大小
Dialogue: 0,0:18:39.80,0:18:43.82,English,,0,0,0,,And it's going to fill that in with the string representation of it
Dialogue: 0,0:18:39.80,0:18:43.82,Chinese,,0,0,0,,者将用代表它的字符串来填充这里
Dialogue: 0,0:18:44.38,0:18:46.90,English,,0,0,0,,So you see this is a way I'm going from the
Dialogue: 0,0:18:44.38,0:18:46.90,Chinese,,0,0,0,,我从这个方法出发
Dialogue: 0,0:18:47.20,0:18:52.98,English,,0,0,0,,If you look at this is the actual sockaddr of each of the list elements
Dialogue: 0,0:18:47.20,0:18:52.98,Chinese,,0,0,0,,你看这是每个列表元素的 sockaddr
Dialogue: 0,0:18:55.26,0:18:59.88,English,,0,0,0,,I'm giving it,it's going from that sockaddr which embedded within it
Dialogue: 0,0:18:55.26,0:18:59.88,Chinese,,0,0,0,,这些 sockaddr 都复制到了缓冲区
Dialogue: 0,0:19:00.30,0:19:03.82,English,,0,0,0,,As you know is the 4 byte IP address
Dialogue: 0,0:19:00.30,0:19:03.82,Chinese,,0,0,0,,如你所知,4 字节的 IP 地址
Dialogue: 0,0:19:04.44,0:19:06.78,English,,0,0,0,,And what it's doing is it's taking that
Dialogue: 0,0:19:04.44,0:19:06.78,Chinese,,0,0,0,,而它正在做的是
Dialogue: 0,0:19:07.28,0:19:11.20,English,,0,0,0,,Flipping the bytes well and converting it to the dotted decimal form so
Dialogue: 0,0:19:07.28,0:19:11.20,Chinese,,0,0,0,,正确地调整字节并将其转换为点分十进制形式
Dialogue: 0,0:19:12.06,0:19:17.06,English,,0,0,0,,This function can do a lot of different things and I'm using it for that particular feature here
Dialogue: 0,0:19:12.06,0:19:17.06,Chinese,,0,0,0,,这个函数可以做很多不同的事情,我在这里只是使用了它的一个功能
Dialogue: 0,0:19:17.20,0:19:21.00,English,,0,0,0,,Question
Dialogue: 0,0:19:17.20,0:19:21.00,Chinese,,0,0,0,,有什么问题吗?
Dialogue: 0,0:19:21.22,0:19:23.84,English,,0,0,0,,If p is NULL then I will go very far
Dialogue: 0,0:19:21.22,0:19:23.84,Chinese,,0,0,0,,如果 p 是 NULL 会一直进行下去
Dialogue: 0,0:19:28.02,0:19:30.60,English,,0,0,0,,Yes because you know it's a null-terminated list so
Dialogue: 0,0:19:28.02,0:19:30.60,Chinese,,0,0,0,,是的,因为它是一个到了 NULL 才停止的列表
Dialogue: 0,0:19:31.42,0:19:32.24,English,,0,0,0,,Other questions
Dialogue: 0,0:19:31.42,0:19:32.24,Chinese,,0,0,0,,其他问题
Dialogue: 0,0:19:32.64,0:19:33.40,English,,0,0,0,,Good questions
Dialogue: 0,0:19:32.64,0:19:33.40,Chinese,,0,0,0,,好问题
Dialogue: 0,0:19:34.10,0:19:36.32,English,,0,0,0,,But that's exactly the code you saw running
Dialogue: 0,0:19:34.10,0:19:36.32,Chinese,,0,0,0,,这是你看到的运行代码
Dialogue: 0,0:19:36.90,0:19:42.16,English,,0,0,0,,And all I did to generalize it to include IPv6 addresses
Dialogue: 0,0:19:36.90,0:19:42.16,Chinese,,0,0,0,,与前面的相比我所做的就是兼容 IPv6 地址
Dialogue: 0,0:19:42.60,0:19:44.66,English,,0,0,0,,Was I commented out this one
Dialogue: 0,0:19:42.60,0:19:44.66,Chinese,,0,0,0,,就是我注释的这个
Dialogue: 0,0:19:45.94,0:19:53.26,English,,0,0,0,, I don't demand that it be a IP, AF_INET is a shorthand for IPv4
Dialogue: 0,0:19:45.94,0:19:53.26,Chinese,,0,0,0,,我不要求它是 IP...... AF_INET 是 IPv4 的简写
Dialogue: 0,0:19:53.32,0:20:01.72,English,,0,0,0,,So by commenting that out it said okay I'll get whatever TCP connections this domain will support
Dialogue: 0,0:19:53.32,0:20:01.72,Chinese,,0,0,0,,注释掉它,我会得到这个域支持的任何 TCP 连接
Dialogue: 0,0:20:01.94,0:20:04.16,English,,0,0,0,,And that will include IPv6 addresses
Dialogue: 0,0:20:01.94,0:20:04.16,Chinese,,0,0,0,,这将包括 IPv6 地址
Dialogue: 0,0:20:07.98,0:20:09.66,English,,0,0,0,,So I thought it's pretty neat
Dialogue: 0,0:20:07.98,0:20:09.66,Chinese,,0,0,0,,所以我觉得它很巧妙
Dialogue: 0,0:20:10.42,0:20:14.90,English,,0,0,0,,And you have to be a pretty nerdy but I am pretty nerdy to think this kind of thing is neat
Dialogue: 0,0:20:10.42,0:20:14.90,Chinese,,0,0,0,,你也许觉得很乏味,但我坚持认为它是巧妙的
Dialogue: 0,0:20:14.90,0:20:17.96,English,,0,0,0,,But that you could write code in this way
Dialogue: 0,0:20:14.90,0:20:17.96,Chinese,,0,0,0,,你也可以使用这种方式编写代码
Dialogue: 0,0:20:18.08,0:20:25.30,English,,0,0,0,,Whether you're doing your different versions of the internet protocol well it's all one common body of code
Dialogue: 0,0:20:18.08,0:20:25.30,Chinese,,0,0,0,,无论你是处理的不同版本的互联网协议,它都是使用的相同的代码
Dialogue: 0,0:20:27.78,0:20:29.30,English,,0,0,0,,So we've seen demo of this
Dialogue: 0,0:20:27.78,0:20:29.30,Chinese,,0,0,0,,我们已经看到了这个的演示
Dialogue: 0,0:20:30.16,0:20:37.60,English,,0,0,0,,Okay so now let's jump into that shows you sort of a little bit of this getaddrinfo api
Dialogue: 0,0:20:30.16,0:20:37.60,Chinese,,0,0,0,,现在让我们从 getaddrinfo api 开始
Dialogue: 0,0:20:37.76,0:20:42.04,English,,0,0,0,,Now let's jump into the full-blown web server part
Dialogue: 0,0:20:37.76,0:20:42.04,Chinese,,0,0,0,,现在让我们跳到完整的 Web 服务器部分吧
Dialogue: 0,0:20:42.80,0:20:48.12,English,,0,0,0,,And as I mentioned in chronological order you actually want to set up the server first
Dialogue: 0,0:20:42.80,0:20:48.12,Chinese,,0,0,0,,正如我提到的时间顺序,你首先要设置服务器
Dialogue: 0,0:20:48.92,0:20:51.52,English,,0,0,0,,But it's easiest to understand the code for the client
Dialogue: 0,0:20:48.92,0:20:51.52,Chinese,,0,0,0,,但是最容易理解的是客户端的代码
Dialogue: 0,0:20:51.80,0:20:56.20,English,,0,0,0,,Because as you see it has a couple fewer calls to make
Dialogue: 0,0:20:51.80,0:20:56.20,Chinese,,0,0,0,,因为你可以看到它的调用次数减少了几次
Dialogue: 0,0:20:56.68,0:20:58.14,English,,0,0,0,,So let's go for through the client
Dialogue: 0,0:20:56.68,0:20:58.14,Chinese,,0,0,0,,让我们先来看看客户端
Dialogue: 0,0:20:58.14,0:21:03.28,English,,0,0,0,,Just imagine that somewhere out there there's a server that's waiting for us to connect to
Dialogue: 0,0:20:58.14,0:21:03.28,Chinese,,0,0,0,,想象一下,在某个地方,有一个服务器在等着我们连接
Dialogue: 0,0:21:06.72,0:21:09.10,English,,0,0,0,,So this now doesn't perhaps
Dialogue: 0,0:21:06.72,0:21:09.10,Chinese,,0,0,0,,也许......
Dialogue: 0,0:21:09.56,0:21:13.18,English,,0,0,0,,When you break it down maybe this picture looks a little less daunting
Dialogue: 0,0:21:09.56,0:21:13.18,Chinese,,0,0,0,,当你把它分开来看时,这张照片可能看起来不那么令人生畏
Dialogue: 0,0:21:13.52,0:21:22.92,English,,0,0,0,,We already saw getaddrinfo is the way I'm going to get an IP address out of whatever I'm where my server is
Dialogue: 0,0:21:13.52,0:21:22.92,Chinese,,0,0,0,,我们已经看到 getaddrinfo 是我从服务器所在的地方获取 IP 地址的方法
Dialogue: 0,0:21:23.50,0:21:26.88,English,,0,0,0,,Socket I mentioned this yesterday or last time
Dialogue: 0,0:21:23.50,0:21:26.88,Chinese,,0,0,0,,我昨天还是上次提到过这个 socket
Dialogue: 0,0:21:27.40,0:21:33.90,English,,0,0,0,,It's just a local call that's used to allocate some data structures for this particular connection
Dialogue: 0,0:21:27.40,0:21:33.90,Chinese,,0,0,0,,它只是一个本地调用,用于为此特定连接分配一些数据结构
Dialogue: 0,0:21:34.48,0:21:37.10,English,,0,0,0,,And then connect is like placing a call
Dialogue: 0,0:21:34.48,0:21:37.10,Chinese,,0,0,0,,然后连接就像拨打电话一样
Dialogue: 0,0:21:37.78,0:21:42.52,English,,0,0,0,,It's an api it's actually sending over the internet to the server
Dialogue: 0,0:21:37.78,0:21:42.52,Chinese,,0,0,0,,这是一个 api ,它实际上通过互联网发送到服务器
Dialogue: 0,0:21:43.04,0:21:45.66,English,,0,0,0,,Some information saying hey I'd like to connect you
Dialogue: 0,0:21:43.04,0:21:45.66,Chinese,,0,0,0,,有些信息是:嘿,我想和你联系
Dialogue: 0,0:21:47.10,0:21:53.78,English,,0,0,0,,And then once the server risk...that once that connection is accepted then I can begin my session
Dialogue: 0,0:21:47.10,0:21:53.78,Chinese,,0,0,0,,然后一旦服务器接受该连接,我就可以开始我的会话了
Dialogue: 0,0:21:55.02,0:21:56.06,English,,0,0,0,,So let's look at that
Dialogue: 0,0:21:55.02,0:21:56.06,Chinese,,0,0,0,,那么让我们来看看
Dialogue: 0,0:21:58.94,0:22:03.44,English,,0,0,0,,So first of all I have to go through all this rigmarole with getaddrinfo
Dialogue: 0,0:21:58.94,0:22:03.44,Chinese,,0,0,0,,首先,我必须通过 getaddrinfo 来完成所有这些操作
Dialogue: 0,0:22:04.10,0:22:06.90,English,,0,0,0,,But it will look pretty similar to what you've seen before
Dialogue: 0,0:22:04.10,0:22:06.90,Chinese,,0,0,0,,但它看起来与你之前看到的非常相似
Dialogue: 0,0:22:07.30,0:22:10.80,English,,0,0,0,,Except I'm going to give a little more information because I want this to be
Dialogue: 0,0:22:07.30,0:22:10.80,Chinese,,0,0,0,,除了我要提供更多信息,因为我希望......
Dialogue: 0,0:22:14.26,0:22:18.56,English,,0,0,0,,I'm filling in a few more details here
Dialogue: 0,0:22:14.26,0:22:18.56,Chinese,,0,0,0,,我在这里填写一些细节
Dialogue: 0,0:22:18.56,0:22:21.48,English,,0,0,0,,Because I want to specify more features of this connection
Dialogue: 0,0:22:18.56,0:22:21.48,Chinese,,0,0,0,,用来具体说明此连接的更多特点
Dialogue: 0,0:22:22.10,0:22:26.16,English,,0,0,0,,So I'm saying that it's a TCP connection that's a sock stream
Dialogue: 0,0:22:22.10,0:22:26.16,Chinese,,0,0,0,,所以这是一个 TCP 连接,就是 SOCK_SREAM
Dialogue: 0,0:22:27.42,0:22:31.30,English,,0,0,0,,I'm saying that I'm going to give it a port number in a numeric form
Dialogue: 0,0:22:27.42,0:22:31.30,Chinese,,0,0,0,,我打算用数字形式给它一个端口号
Dialogue: 0,0:22:31.38,0:22:41.02,English,,0,0,0,,You could also say as I mentioned of the certain ports have standard names http,ssh and so forth
Dialogue: 0,0:22:31.38,0:22:41.02,Chinese,,0,0,0,,像我提到的某些有标准名称 http , ssh 的端口等等
Dialogue: 0,0:22:41.02,0:22:44.28,English,,0,0,0,,You can actually pass in the string representation of those
Dialogue: 0,0:22:41.02,0:22:44.28,Chinese,,0,0,0,,你实际上可以传入那些字符串表示形式
Dialogue: 0,0:22:44.86,0:22:49.54,English,,0,0,0,,But I'm using this in a sort of idiosyncratic way here
Dialogue: 0,0:22:44.86,0:22:49.54,Chinese,,0,0,0,,但我在这里以一种特殊的方式使用它
Dialogue: 0,0:22:50.46,0:22:57.52,English,,0,0,0,,And then this feature get adder config is a parameter that's described in the book
Dialogue: 0,0:22:50.46,0:22:57.52,Chinese,,0,0,0,,然后,这个 AI_ADDRCONFIG 是本书中描述的参数
Dialogue: 0,0:22:57.56,0:22:59.68,English,,0,0,0,,And it they say it's a good idea to use it
Dialogue: 0,0:22:57.56,0:22:59.68,Chinese,,0,0,0,,推荐使用这个标志
Dialogue: 0,0:23:00.14,0:23:03.56,English,,0,0,0,,And then I'm making a call to my friend getaddrinfo
Dialogue: 0,0:23:00.14,0:23:03.56,Chinese,,0,0,0,,当我调用 Getaddrinfo 的时候
Dialogue: 0,0:23:04.38,0:23:13.04,English,,0,0,0,,With these hints and it's going to return back this linked list of representing the the results of this request
Dialogue: 0,0:23:04.38,0:23:13.04,Chinese,,0,0,0,,根据这些 hints ,它将返回此请求的结果链表
Dialogue: 0,0:23:13.64,0:23:16.02,English,,0,0,0,,So basically a linked list of IP addresses
Dialogue: 0,0:23:13.64,0:23:16.02,Chinese,,0,0,0,,是一个 IP 地址链表
Dialogue: 0,0:23:18.30,0:23:21.84,English,,0,0,0,,And now just like you saw before I'm going to walk that list with a
Dialogue: 0,0:23:18.30,0:23:21.84,Chinese,,0,0,0,,现在就像你之前看到的那样,我要遍历这个列表
Dialogue: 0,0:23:23.44,0:23:26.92,English,,0,0,0,,Except now what I'm going to try and do is for each of those say
Dialogue: 0,0:23:23.44,0:23:26.92,Chinese,,0,0,0,,现在我只需要遍历每一个结果
Dialogue: 0,0:23:27.34,0:23:30.40,English,,0,0,0,,Well here's a number let me give that a try and if that fails
Dialogue: 0,0:23:27.34,0:23:30.40,Chinese,,0,0,0,,那么让我尝试一下这个套接字地址,如果失败了
Dialogue: 0,0:23:30.42,0:23:34.90,English,,0,0,0,,Here's another address let me give that a try so I'm going to just sequentially try
Dialogue: 0,0:23:30.42,0:23:34.90,Chinese,,0,0,0,,让我再试一试另一个地址,这样我将继续尝试
Dialogue: 0,0:23:35.54,0:23:37.30,English,,0,0,0,,All the different addresses you saw
Dialogue: 0,0:23:35.54,0:23:37.30,Chinese,,0,0,0,,你看到的所有不同地址
Dialogue: 0,0:23:37.74,0:23:41.14,English,,0,0,0,,For example for potential addresses for twitter
Dialogue: 0,0:23:37.74,0:23:41.14,Chinese,,0,0,0,,例如,针对 twitter 可能的套接字地址
Dialogue: 0,0:23:41.70,0:23:43.24,English,,0,0,0,,I'll walk through all of those
Dialogue: 0,0:23:41.70,0:23:43.24,Chinese,,0,0,0,,我将遍历所有的
Dialogue: 0,0:23:44.72,0:23:47.46,English,,0,0,0,,And on each one I'm going to call for a socket
Dialogue: 0,0:23:44.72,0:23:47.46,Chinese,,0,0,0,,每一个我都会调用 socket 这个函数
Dialogue: 0,0:23:48.36,0:23:50.48,English,,0,0,0,,And the socket is the one that
Dialogue: 0,0:23:48.36,0:23:50.48,Chinese,,0,0,0,, socket 函数是......
Dialogue: 0,0:23:52.86,0:23:55.72,English,,0,0,0,,As I said it allocates some data structures
Dialogue: 0,0:23:52.86,0:23:55.72,Chinese,,0,0,0,,它分配了一些数据结构所需内存
Dialogue: 0,0:23:55.88,0:23:59.50,English,,0,0,0,,I talked about this last time that's why I'm rushing through is pretty fast
Dialogue: 0,0:23:55.88,0:23:59.50,Chinese,,0,0,0,,我上次匆匆忙忙地谈到这个问题
Dialogue: 0,0:24:00.04,0:24:10.76,English,,0,0,0,,But you'll notice that the fields that you're filling in are actually all been generated for us by the Getaddrinfo call
Dialogue: 0,0:24:00.04,0:24:10.76,Chinese,,0,0,0,,你会注意到这些字段的填充实际上都是由调用的 Getaddrinfo 自动生成的
Dialogue: 0,0:24:11.28,0:24:18.00,English,,0,0,0,,So whereas the east have to remember oh this is a number and it should be a shortened network order and yada da
Dialogue: 0,0:24:11.28,0:24:18.00,Chinese,,0,0,0,,但你必须记住这是一个数字,它是一个缩短的网络顺序
Dialogue: 0,0:24:18.40,0:24:25.96,English,,0,0,0,,Pretty much nowadays you can just say here I'm just going to pass you this information that
Dialogue: 0,0:24:18.40,0:24:25.96,Chinese,,0,0,0,,现在你可以说,我传给你的信息
Dialogue: 0,0:24:32.12,0:24:34.56,English,,0,0,0,,Was returned by a function call
Dialogue: 0,0:24:32.12,0:24:34.56,Chinese,,0,0,0,,是通过函数调用返回的
Dialogue: 0,0:24:35.46,0:24:39.04,English,,0,0,0,,Oh and then as I mentioned also last time
Dialogue: 0,0:24:35.46,0:24:39.04,Chinese,,0,0,0,,然后还是我上次提到的那样
Dialogue: 0,0:24:39.24,0:24:41.14,English,,0,0,0,,I know I went through a pretty fast last time
Dialogue: 0,0:24:39.24,0:24:41.14,Chinese,,0,0,0,,我知道上次我讲的很快
Dialogue: 0,0:24:41.68,0:24:45.32,English,,0,0,0,,Socket actually returns back to you a file descriptor
Dialogue: 0,0:24:41.68,0:24:45.32,Chinese,,0,0,0,, socket 实际上会返回给你一个文件描述符
Dialogue: 0,0:24:45.70,0:24:51.54,English,,0,0,0,,And you recall a file descriptor is just a small integer starts with 0,1 & 2
Dialogue: 0,0:24:45.70,0:24:51.54,Chinese,,0,0,0,,你应该还记得一个文件描述符是一个以 0,1,2 开头的小整数
Dialogue: 0,0:24:52.20,0:24:53.72,English,,0,0,0,,And then it goes up from there
Dialogue: 0,0:24:52.20,0:24:53.72,Chinese,,0,0,0,,然后它从那里开始
Dialogue: 0,0:24:54.94,0:25:00.50,English,,0,0,0,,So I'm going to interface with this network code as if I'm talking through files
Dialogue: 0,0:24:54.94,0:25:00.50,Chinese,,0,0,0,,我打算连接这个网络代码,就好像我正在通过文件进行交谈一样
Dialogue: 0,0:25:02.16,0:25:06.18,English,,0,0,0,,But it doesn't socket doesn't actually do anything as far as the internet goes
Dialogue: 0,0:25:02.16,0:25:06.18,Chinese,,0,0,0,,但就互联网而言,套接字实际上并没有做任何事情
Dialogue: 0,0:25:06.18,0:25:08.56,English,,0,0,0,,There's nothing going there's not even
Dialogue: 0,0:25:06.18,0:25:08.56,Chinese,,0,0,0,,什么都没有
Dialogue: 0,0:25:09.26,0:25:14.06,English,,0,0,0,,The you know serious effort within the operating system nothing goes over the network here
Dialogue: 0,0:25:09.26,0:25:14.06,Chinese,,0,0,0,,你知道,在操作系统中,在网络上什么都没有
Dialogue: 0,0:25:15.76,0:25:22.04,English,,0,0,0,,So the real work here is but it also allocates some local data structures
Dialogue: 0,0:25:15.76,0:25:22.04,Chinese,,0,0,0,,这里的实际工作是,它也分配了一些本地数据结构
Dialogue: 0,0:25:23.96,0:25:28.16,English,,0,0,0,,And then what I'll do is I'll attempt to connect to the server
Dialogue: 0,0:25:23.96,0:25:28.16,Chinese,,0,0,0,,然后我将尝试连接到服务器
Dialogue: 0,0:25:28.62,0:25:32.73,English,,0,0,0,,And again you'll see the various arguments I have to pass to the connect command
Dialogue: 0,0:25:28.62,0:25:32.73,Chinese,,0,0,0,,你看到了我再次传递给 connect 函数的各种参数
Dialogue: 0,0:25:33.24,0:25:38.24,English,,0,0,0,,I get provided for me out of this call I made to getaddrinfo
Dialogue: 0,0:25:33.24,0:25:38.24,Chinese,,0,0,0,,这些参数是用 getaddrinfo 函数提供的
Dialogue: 0,0:25:39.40,0:25:41.46,English,,0,0,0,,And that's either going to work or it's not going to work
Dialogue: 0,0:25:39.40,0:25:41.46,Chinese,,0,0,0,,它们要么正在发挥作用,要么还没发挥作用
Dialogue: 0,0:25:41.94,0:25:44.92,English,,0,0,0,,Similarly the socket call is either going to work or not work
Dialogue: 0,0:25:41.94,0:25:44.92,Chinese,,0,0,0,,类似地,要判断 socket 是否能正常返回
Dialogue: 0,0:25:45.44,0:25:48.06,English,,0,0,0,,Be very unusual for a call to socket to fail
Dialogue: 0,0:25:45.44,0:25:48.06,Chinese,,0,0,0,, socket 连接失败很不寻常
Dialogue: 0,0:25:48.72,0:25:54.74,English,,0,0,0,,But connect for example will fail if the server's not available or doesn't like you for some reason
Dialogue: 0,0:25:48.72,0:25:54.74,Chinese,,0,0,0,,但是,如果服务器由于某种原因不可用或不欢迎你,则连接将失败
Dialogue: 0,0:25:56.28,0:26:02.48,English,,0,0,0,,And so if that succeeds I'm going to break out of this loop
Dialogue: 0,0:25:56.28,0:26:02.48,Chinese,,0,0,0,,如果连接成功,将跳出这个循环
Dialogue: 0,0:26:03.16,0:26:11.10,English,,0,0,0,,I'm going to then free this data structure and return the client the file descriptor
Dialogue: 0,0:26:03.16,0:26:11.10,Chinese,,0,0,0,,然后释放这个链表并返回给客户端文件描述符
Dialogue: 0,0:26:11.98,0:26:19.76,English,,0,0,0,,For the this particular function its job is to open a connection to a client and return a file descriptor for it
Dialogue: 0,0:26:11.98,0:26:19.76,Chinese,,0,0,0,,这个函数的作用是打开客户端的连接并为其返回文件描述符
Dialogue: 0,0:26:20.34,0:26:24.88,English,,0,0,0,,And this is just what happens if you fall out of the loop not having made any connections
Dialogue: 0,0:26:20.34,0:26:24.88,Chinese,,0,0,0,,这就是如果你没有建立任何连接而退出循环会发生的事情
Dialogue: 0,0:26:30.94,0:26:35.54,English,,0,0,0,,Ok so I know this is I'm marching this fast
Dialogue: 0,0:26:30.94,0:26:35.54,Chinese,,0,0,0,,我知道这里我讲的很快
Dialogue: 0,0:26:36.76,0:26:41.82,English,,0,0,0,,And as I said I know from my own personal experience I can test this stuff
Dialogue: 0,0:26:36.76,0:26:41.82,Chinese,,0,0,0,,正如我所说,根据我自己的经验,我可以测试这些东西
Dialogue: 0,0:26:42.32,0:26:46.08,English,,0,0,0,,Just takes time to understand because there's so many different features here
Dialogue: 0,0:26:42.32,0:26:46.08,Chinese,,0,0,0,,只是需要时间来理解,因为这里有很多不同的功能
Dialogue: 0,0:26:47.04,0:26:50.48,English,,0,0,0,,Ok but that's the idea is we're going to
Dialogue: 0,0:26:47.04,0:26:50.48,Chinese,,0,0,0,,好的,这就是我们的正在讨论的
Dialogue: 0,0:26:51.88,0:26:56.60,English,,0,0,0,,Now on this server side which as I mentioned is that actually what you do beforehand
Dialogue: 0,0:26:51.88,0:26:56.60,Chinese,,0,0,0,,就像我说的,对于服务器这端,这是你应该提前做的
Dialogue: 0,0:26:56.60,0:27:00.46,English,,0,0,0,,Because you can't really have a client connect to a server that doesn't exist
Dialogue: 0,0:26:56.60,0:27:00.46,Chinese,,0,0,0,,因为你无法将客户端连接到不存在的服务器
Dialogue: 0,0:27:01.16,0:27:04.00,English,,0,0,0,,But it's just a little bit trickier to understand what's going on
Dialogue: 0,0:27:01.16,0:27:04.00,Chinese,,0,0,0,,但要理解正在发生的事情有点棘手
Dialogue: 0,0:27:05.00,0:27:07.84,English,,0,0,0,,But you'll see it the first two steps are the same I need a socket
Dialogue: 0,0:27:05.00,0:27:07.84,Chinese,,0,0,0,,但你会看到它的前两个步骤是相同的,它们都需要一个 socket
Dialogue: 0,0:27:08.54,0:27:13.22,English,,0,0,0,,But then remember what I want to do is essentially set up a server
Dialogue: 0,0:27:08.54,0:27:13.22,Chinese,,0,0,0,,但是记住我想要做的事实上是设置一个服务器
Dialogue: 0,0:27:13.22,0:27:18.54,English,,0,0,0,,That's listening on a particular port for clients to request on it
Dialogue: 0,0:27:13.22,0:27:18.54,Chinese,,0,0,0,,它在特定端口监听客户端的请求
Dialogue: 0,0:27:18.98,0:27:21.62,English,,0,0,0,,And that's the function of the bind and the listen functions
Dialogue: 0,0:27:18.98,0:27:21.62,Chinese,,0,0,0,,这就是 bind 和 listen 函数的功能
Dialogue: 0,0:27:22.68,0:27:26.44,English,,0,0,0,,And then the accept is when I'm saying ok the server is ready
Dialogue: 0,0:27:22.68,0:27:26.44,Chinese,,0,0,0,,然后服务器准备就绪就调用 accept
Dialogue: 0,0:27:27.04,0:27:28.50,English,,0,0,0,,I'm ready to accept requests
Dialogue: 0,0:27:27.04,0:27:28.50,Chinese,,0,0,0,,准备好接受请求
Dialogue: 0,0:27:29.00,0:27:32.02,English,,0,0,0,,And it will fire that up
Dialogue: 0,0:27:29.00,0:27:32.02,Chinese,,0,0,0,,它就会启动
Dialogue: 0,0:27:33.04,0:27:38.02,English,,0,0,0,,In particular the accept will just stop and hang up
Dialogue: 0,0:27:33.04,0:27:38.02,Chinese,,0,0,0,,同步的是 accept 一直处理停止,挂起,
Dialogue: 0,0:27:38.42,0:27:43.02,English,,0,0,0,,And wait indefinitely until some connection request comes in
Dialogue: 0,0:27:38.42,0:27:43.02,Chinese,,0,0,0,,和无限期的等待中,直到某些连接请求进入
Dialogue: 0,0:27:43.86,0:27:47.38,English,,0,0,0,,So if the server has no customers if there are no clients
Dialogue: 0,0:27:43.86,0:27:47.38,Chinese,,0,0,0,,因此,如果没有客户端,服务器就没有客户
Dialogue: 0,0:27:48.02,0:27:54.00,English,,0,0,0,,It will just be waiting potentially forever with the call to accept
Dialogue: 0,0:27:48.02,0:27:54.00,Chinese,,0,0,0,,它只是调用 accept 函数,用于等待潜在的客户访问
Dialogue: 0,0:27:55.64,0:27:57.82,English,,0,0,0,,Ok everything else goes through boom boom boom
Dialogue: 0,0:27:55.64,0:27:57.82,Chinese,,0,0,0,,之前的一切都能砰!砰!砰!的运行
Dialogue: 0,0:27:57.92,0:28:02.18,English,,0,0,0,,But that one is where the actual waiting occurs for client connections
Dialogue: 0,0:27:57.92,0:28:02.18,Chinese,,0,0,0,,但这里却要等待客户端连接
Dialogue: 0,0:28:07.00,0:28:08.74,English,,0,0,0,,Some way I should mention the connect
Dialogue: 0,0:28:07.00,0:28:08.74,Chinese,,0,0,0,,我得提一下连接 connect
Dialogue: 0,0:28:10.42,0:28:13.86,English,,0,0,0,,Will also hang up and wait until the connection is formed
Dialogue: 0,0:28:10.42,0:28:13.86,Chinese,,0,0,0,,也会挂起并等待连接形成
Dialogue: 0,0:28:14.36,0:28:19.10,English,,0,0,0,,But typically it's built in with a time out so that it will fail if it takes too long
Dialogue: 0,0:28:14.36,0:28:19.10,Chinese,,0,0,0,,但通常情况下它是内置的超时时间,如果花费的时间太长就会失败
Dialogue: 0,0:28:19.42,0:28:24.06,English,,0,0,0,,And there's you can adjust the parameters on both of these how long you wait for,whether your wait or not
Dialogue: 0,0:28:19.42,0:28:24.06,Chinese,,0,0,0,,而且你可以调整这两个参数,设置等待多长时间,是否等待
Dialogue: 0,0:28:26.28,0:28:28.00,English,,0,0,0,,But I'm talking the typical case
Dialogue: 0,0:28:26.28,0:28:28.00,Chinese,,0,0,0,,我说的是一般情况
Dialogue: 0,0:28:28.54,0:28:30.20,English,,0,0,0,,So anyways
Dialogue: 0,0:28:28.54,0:28:30.20,Chinese,,0,0,0,,那么
Dialogue: 0,0:28:30.80,0:28:34.30,English,,0,0,0,,That again what will look a little those functions then
Dialogue: 0,0:28:30.80,0:28:34.30,Chinese,,0,0,0,,那些其他的函数又是什么样的呢?
Dialogue: 0,0:28:37.04,0:28:40.72,English,,0,0,0,,Okay so this again we have to make a call to getaddrinfo
Dialogue: 0,0:28:37.04,0:28:40.72,Chinese,,0,0,0,,这里好的,我们再次调用 Getaddrinfo
Dialogue: 0,0:28:41.34,0:28:45.64,English,,0,0,0,,And this time it's not because I need any IP addresses I already have an IP address
Dialogue: 0,0:28:41.34,0:28:45.64,Chinese,,0,0,0,,这次并不是需要 IP 地址,因为我已经有了 IP 地址
Dialogue: 0,0:28:45.64,0:28:52.64,English,,0,0,0,,I'm a local server I don't actually need to know the IP addresses of my of the clients
Dialogue: 0,0:28:45.64,0:28:52.64,Chinese,,0,0,0,,我是本地服务器,实际上它不需要知道客户端的 IP 地址
Dialogue: 0,0:28:52.74,0:28:53.94,English,,0,0,0,,I usually won't know them
Dialogue: 0,0:28:52.74,0:28:53.94,Chinese,,0,0,0,,通常不会认识他们
Dialogue: 0,0:28:54.96,0:28:59.68,English,,0,0,0,,But I need to fill in all those nasty little data structures and so
Dialogue: 0,0:28:54.96,0:28:59.68,Chinese,,0,0,0,,但需要填写那些讨厌的数据结构等等
Dialogue: 0,0:29:00.00,0:29:02.76,English,,0,0,0,,That's what I'm going to use getaddrinfo here for here
Dialogue: 0,0:29:00.00,0:29:02.76,Chinese,,0,0,0,,这时就要使用 Getaddrinfo
Dialogue: 0,0:29:03.72,0:29:08.06,English,,0,0,0,,So I'm going to say this time again I want a TCP connection a sock stream
Dialogue: 0,0:29:03.72,0:29:08.06,Chinese,,0,0,0,,我希望 TCP 连接所以使用 SOCK_STREAM
Dialogue: 0,0:29:09.16,0:29:19.24,English,,0,0,0,,I want it to be a listening for any IP address connection coming in
Dialogue: 0,0:29:09.16,0:29:19.24,Chinese,,0,0,0,,我希望它能够监听任何 IP 地址连接
Dialogue: 0,0:29:21.00,0:29:25.02,English,,0,0,0,,So it's passive meaning it's not actively reaching out
Dialogue: 0,0:29:21.00,0:29:25.02,Chinese,,0,0,0,,所以它是被动的
Dialogue: 0,0:29:25.18,0:29:27.74,English,,0,0,0,,Its passively waiting for connection requests
Dialogue: 0,0:29:25.18,0:29:27.74,Chinese,,0,0,0,,它被动地等待连接请求
Dialogue: 0,0:29:29.52,0:29:32.18,English,,0,0,0,,I'll say that I'm going to tell you a port number
Dialogue: 0,0:29:29.52,0:29:32.18,Chinese,,0,0,0,,我告诉你一个端口号
Dialogue: 0,0:29:32.70,0:29:36.52,English,,0,0,0,,That I'm going to actually use as my servers port number
Dialogue: 0,0:29:32.70,0:29:36.52,Chinese,,0,0,0,,实际上这个端口号是作为我服务器的端口号
Dialogue: 0,0:29:37.46,0:29:43.54,English,,0,0,0,,And then I'm going to make a call and this time you see that
Dialogue: 0,0:29:37.46,0:29:43.54,Chinese,,0,0,0,,然后我准备连接
Dialogue: 0,0:29:44.02,0:29:47.66,English,,0,0,0,,I'm not giving any information about the host because I am the host
Dialogue: 0,0:29:44.02,0:29:47.66,Chinese,,0,0,0,,我没有提供任何有关主机的信息,因为我就是主机
Dialogue: 0,0:29:48.40,0:29:53.58,English,,0,0,0,,But I'm giving the information of the port number that I'm using for this server
Dialogue: 0,0:29:48.40,0:29:53.58,Chinese,,0,0,0,,但我提供了服务器使用的端口号的信息
Dialogue: 0,0:29:54.34,0:29:56.92,English,,0,0,0,,And the other hints that I've just filled in
Dialogue: 0,0:29:54.34,0:29:56.92,Chinese,,0,0,0,,还有我刚刚填写的其他 hints
Dialogue: 0,0:29:57.34,0:29:59.40,English,,0,0,0,,And then it will return a listp yes
Dialogue: 0,0:29:57.34,0:29:59.40,Chinese,,0,0,0,,然后它将返回 listp ,有什么问题吗?
Dialogue: 0,0:29:59.40,0:30:02.06,English,,0,0,0,,[student speaking]
Dialogue: 0,0:29:59.40,0:30:02.06,Chinese,,0,0,0,,【学生提问】
Dialogue: 0,0:30:02.08,0:30:11.56,English,,0,0,0,,Memset is the standard way that you set all the bytes in some number of bytes to a fixed value
Dialogue: 0,0:30:11.51,0:30:11.56,Chinese,,0,0,0,, Memset 是将某些字节设置为固定值的标准方法
Dialogue: 0,0:30:12.20,0:30:15.84,English,,0,0,0,,It's the same as if you've use be 0 before or be...
Dialogue: 0,0:30:12.20,0:30:15.84,Chinese,,0,0,0,,它就像你之前使用 0 一样
Dialogue: 0,0:30:16.94,0:30:20.82,English,,0,0,0,,It was is the way you often would zero out a bunch of bytes
Dialogue: 0,0:30:16.94,0:30:20.82,Chinese,,0,0,0,,这是你经常将一堆字节归零的方式
Dialogue: 0,0:30:21.72,0:30:27.76,English,,0,0,0,,And if you look at the web page and I don't know why they say use memset don't use be 0 anymore so
Dialogue: 0,0:30:21.72,0:30:27.76,Chinese,,0,0,0,,如果你看看网页,我不知道为什么他们说使用 memset 初始化 0 ,不要再直接置 0
Dialogue: 0,0:30:30.98,0:30:34.28,English,,0,0,0,,addrinfo equal to 0 it out but I'm not actually
Dialogue: 0,0:30:30.98,0:30:34.28,Chinese,,0,0,0,, addrinfo 等于 0 ,实际上并不是
Dialogue: 0,0:30:34.80,0:30:39.12,English,,0,0,0,,Allocating anything see the hints is a struct here
Dialogue: 0,0:30:34.80,0:30:39.12,Chinese,,0,0,0,, hints 在这里是一个结构体
Dialogue: 0,0:30:41.30,0:30:44.72,English,,0,0,0,,And remember is struct is just a bunch of bytes
Dialogue: 0,0:30:41.30,0:30:44.72,Chinese,,0,0,0,,请记住, struct 只是一堆字节
Dialogue: 0,0:30:44.80,0:30:50.00,English,,0,0,0,,And so if I make that particular call to memset it will set every byte in that structure to 0
Dialogue: 0,0:30:44.80,0:30:50.00,Chinese,,0,0,0,,因此,如果我对 memset 进行特定调用,它会将该结构中的每个字节设置为 0
Dialogue: 0,0:30:50.04,0:30:54.54,English,,0,0,0,,And this is sort of the official standard way you're supposed to do that kind of
Dialogue: 0,0:30:50.04,0:30:54.54,Chinese,,0,0,0,,这才是你说的那种标准形式
Dialogue: 0,0:30:55.20,0:30:56.82,English,,0,0,0,,Thing it's a good question so question
Dialogue: 0,0:30:55.20,0:30:56.82,Chinese,,0,0,0,,这是一个很好的问题。还有什么问题?
Dialogue: 0,0:30:56.96,0:31:01.20,English,,0,0,0,,[student speaking]
Dialogue: 0,0:30:56.96,0:31:01.20,Chinese,,0,0,0,,【学生提问】
Dialogue: 0,0:31:01.20,0:31:03.90,English,,0,0,0,,AI_PASSIVE that it's a...
Dialogue: 0,0:31:01.20,0:31:03.90,Chinese,,0,0,0,, AI_PASSIVE 它是 ......
Dialogue: 0,0:31:05.04,0:31:09.44,English,,0,0,0,,It's a listening connection it's going to be a listening connection
Dialogue: 0,0:31:05.04,0:31:09.44,Chinese,,0,0,0,,这是一个监听的连接,它将成为一个监听的连接
Dialogue: 0,0:31:10.26,0:31:15.88,English,,0,0,0,,And AI_ADDRCONFIG was some other argument
Dialogue: 0,0:31:10.26,0:31:15.88,Chinese,,0,0,0,, AI_ADDRCONFIG 是另一个参数
Dialogue: 0,0:31:17.44,0:31:23.60,English,,0,0,0,,And quite honestly I don't remember but you saw I gave it before with the get host name
Dialogue: 0,0:31:17.44,0:31:23.60,Chinese,,0,0,0,,老实说,我不记得了,但是你看到我之前在获取主机名称时用了它
Dialogue: 0,0:31:24.19,0:31:29.18,English,,0,0,0,,So I don't remember it's a slot somewhat obscure per parameter
Dialogue: 0,0:31:24.19,0:31:29.18,Chinese,,0,0,0,,我不太记得,每个参数都有些模糊
Dialogue: 0,0:31:29.18,0:31:31.18,English,,0,0,0,,That's not even a required parameter
Dialogue: 0,0:31:29.18,0:31:31.18,Chinese,,0,0,0,,这甚至不是必需的参数
Dialogue: 0,0:31:31.90,0:31:33.52,English,,0,0,0,,It's in the book I'm sorry
Dialogue: 0,0:31:31.90,0:31:33.52,Chinese,,0,0,0,,它在书中可以找到,我很抱歉
Dialogue: 0,0:31:34.92,0:31:36.46,English,,0,0,0,,Or you could google it right now
Dialogue: 0,0:31:34.92,0:31:36.46,Chinese,,0,0,0,,或者你可以立即谷歌
Dialogue: 0,0:31:41.98,0:31:46.82,English,,0,0,0,,One interesting thing is I didn't say that on this code on either side that it's AF_INET
Dialogue: 0,0:31:41.98,0:31:46.82,Chinese,,0,0,0,,一个有趣的事情是我没有说这个代码是 AF_INET
Dialogue: 0,0:31:46.82,0:31:49.52,English,,0,0,0,,I didn't force this to be IPv4 code
Dialogue: 0,0:31:46.82,0:31:49.52,Chinese,,0,0,0,,我并不是要强调这是 IPv4 代码
Dialogue: 0,0:31:49.76,0:31:53.28,English,,0,0,0,,This will work for IPv4 or 6 the exact same code
Dialogue: 0,0:31:49.76,0:31:53.28,Chinese,,0,0,0,,它适用于 IPv4 或 IPv6
Dialogue: 0,0:31:54.56,0:32:01.78,English,,0,0,0,,Okay so again listp comes back with a list of stuff typically that list will be of length one
Dialogue: 0,0:31:54.56,0:32:01.78,Chinese,,0,0,0,,回到这里,listp 返回一个列表,通常列表的长度为 1
Dialogue: 0,0:32:04.24,0:32:06.54,English,,0,0,0,,In fact I don't know why it would be anything else
Dialogue: 0,0:32:04.24,0:32:06.54,Chinese,,0,0,0,,事实上,我不知道为什么会这样
Dialogue: 0,0:32:07.32,0:32:10.46,English,,0,0,0,,But it has the same general look to it
Dialogue: 0,0:32:07.32,0:32:10.46,Chinese,,0,0,0,,但一般都是这样
Dialogue: 0,0:32:11.44,0:32:15.12,English,,0,0,0,,So again I'm going to make a call to socket
Dialogue: 0,0:32:11.44,0:32:15.12,Chinese,,0,0,0,,现在我再调用 socket
Dialogue: 0,0:32:16.04,0:32:19.88,English,,0,0,0,,To create a file descriptor that I'm going to use this will be the one that
Dialogue: 0,0:32:16.04,0:32:19.88,Chinese,,0,0,0,,创建一个我将要使用的文件描述符
Dialogue: 0,0:32:19.98,0:32:23.10,English,,0,0,0,,It's missed the server listens on of your connection requests
Dialogue: 0,0:32:19.98,0:32:23.10,Chinese,,0,0,0,,用来监听连接请求失败
Dialogue: 0,0:32:25.08,0:32:31.24,English,,0,0,0,,And then there's another kind of obscure thing you have to do of setting some options on that socket
Dialogue: 0,0:32:25.08,0:32:31.24,Chinese,,0,0,0,,然后,你需要在 socket 上设置一些选项,这有点模糊不清
Dialogue: 0,0:32:31.78,0:32:39.22,English,,0,0,0,,That will get it to free it up more quickly if there's a failure
Dialogue: 0,0:32:31.78,0:32:39.22,Chinese,,0,0,0,,如果出现故障,这将使其更快地释放它
Dialogue: 0,0:32:40.48,0:32:45.60,English,,0,0,0,,If this is gone this one I dunno and it's kind of obscure and it's in the book
Dialogue: 0,0:32:40.48,0:32:45.60,Chinese,,0,0,0,,如果这个没了,我就不知道了,这有点模糊,具体内容在书中
Dialogue: 0,0:32:45.60,0:32:47.36,English,,0,0,0,,So I won't spend a lot of time talking about it
Dialogue: 0,0:32:45.60,0:32:47.36,Chinese,,0,0,0,,所以我不会花很多时间谈论它
Dialogue: 0,0:32:48.08,0:32:54.52,English,,0,0,0,,Okay but now what here's the real work is the bind call is the one that says
Dialogue: 0,0:32:48.08,0:32:54.52,Chinese,,0,0,0,,那么现在的工作就是调用 bind 函数了
Dialogue: 0,0:32:55.64,0:33:01.82,English,,0,0,0,,I want this file descriptor to be associated with
Dialogue: 0,0:32:55.64,0:33:01.82,Chinese,,0,0,0,,它将套接字描述符与
Dialogue: 0,0:33:03.20,0:33:07.78,English,,0,0,0,,This information which in particular specifies the port number
Dialogue: 0,0:33:03.20,0:33:07.78,Chinese,,0,0,0,,套接字特别指定的端口号绑定
Dialogue: 0,0:33:07.94,0:33:12.04,English,,0,0,0,,So that's where the binding occurs for this machine
Dialogue: 0,0:33:07.94,0:33:12.04,Chinese,,0,0,0,,这就是绑定这台机器端口号的地方
Dialogue: 0,0:33:12.04,0:33:19.28,English,,0,0,0,,Remember port numbers are a resource that all the different applications on a single machine are sort of sharing
Dialogue: 0,0:33:12.04,0:33:19.28,Chinese,,0,0,0,,端口号是单机中所有不同应用共享的资源
Dialogue: 0,0:33:19.72,0:33:21.32,English,,0,0,0,,Where are competing for
Dialogue: 0,0:33:19.72,0:33:21.32,Chinese,,0,0,0,,同时也竞争的资源
Dialogue: 0,0:33:21.88,0:33:30.00,English,,0,0,0,,And so this is requesting I want to use this particular port number in doing this in setting up a server
Dialogue: 0,0:33:21.88,0:33:30.00,Chinese,,0,0,0,,所以这要求在设置服务器时使用这个特定的端口号
Dialogue: 0,0:33:30.48,0:33:33.00,English,,0,0,0,,And that may fail if the ports already in use
Dialogue: 0,0:33:30.48,0:33:33.00,Chinese,,0,0,0,,如果端口已在使用中,则可能会失败
Dialogue: 0,0:33:33.56,0:33:36.26,English,,0,0,0,,Or if you're trying to set up a service on a port
Dialogue: 0,0:33:33.56,0:33:36.26,Chinese,,0,0,0,,如果你尝试在端口上设置一个服务
Dialogue: 0,0:33:36.28,0:33:45.18,English,,0,0,0,,That you're not allowed to do, you're not allowed to set up a service on port 80 unless you have root access
Dialogue: 0,0:33:36.28,0:33:45.18,Chinese,,0,0,0,,除非你具有 root 访问权限,否则不允许你在 80 端口上设置服务
Dialogue: 0,0:33:50.02,0:33:50.72,English,,0,0,0,,So and
Dialogue: 0,0:33:50.02,0:33:50.72,Chinese,,0,0,0,,然后
Dialogue: 0,0:33:51.96,0:33:58.14,English,,0,0,0,,Then again it passes in the information that was filled in by the call to getaddrinfo
Dialogue: 0,0:33:51.96,0:33:58.14,Chinese,,0,0,0,,它再次传递 Getaddrinfo 调用时填写的信息
Dialogue: 0,0:34:04.96,0:34:06.28,English,,0,0,0,,So we're not quite done yet
Dialogue: 0,0:34:04.96,0:34:06.28,Chinese,,0,0,0,,还没有完成
Dialogue: 0,0:34:07.00,0:34:12.96,English,,0,0,0,,But we if this succeeds then what we've done is we bound a port number
Dialogue: 0,0:34:07.00,0:34:12.96,Chinese,,0,0,0,,但是,如果这成功,那么我们就绑定了一个端口号
Dialogue: 0,0:34:13.34,0:34:17.42,English,,0,0,0,,And a file descriptor together and a socket
Dialogue: 0,0:34:13.34,0:34:17.42,Chinese,,0,0,0,,也绑定一个文件描述符,一个套接字
Dialogue: 0,0:34:23.28,0:34:29.44,English,,0,0,0,,But it all that's done is that's again just within the machine saying okay
Dialogue: 0,0:34:23.28,0:34:29.44,Chinese,,0,0,0,,但所有这一切都得机器说可以
Dialogue: 0,0:34:30.10,0:34:36.18,English,,0,0,0,,I agree you particularly this particular process gets to be listening on a particular port
Dialogue: 0,0:34:30.10,0:34:36.18,Chinese,,0,0,0,,我同意这个特定的进程可以监听特定的端口
Dialogue: 0,0:34:36.90,0:34:38.50,English,,0,0,0,,But it's not actually listening yet
Dialogue: 0,0:34:36.90,0:34:38.50,Chinese,,0,0,0,,但实际上还没有听
Dialogue: 0,0:34:39.68,0:34:41.99,English,,0,0,0,,And I don't know why they split it into two but they do
Dialogue: 0,0:34:39.68,0:34:41.99,Chinese,,0,0,0,,我不知道为什么将它分成两部分,但就是这样
Dialogue: 0,0:34:42.66,0:34:46.64,English,,0,0,0,,And now I actually have to explicitly make a call to listen saying okay
Dialogue: 0,0:34:42.66,0:34:46.64,Chinese,,0,0,0,,现在必须得明确允许监听了
Dialogue: 0,0:34:47.06,0:34:50.00,English,,0,0,0,,Now I really am ready to receive connection requests
Dialogue: 0,0:34:47.06,0:34:50.00,Chinese,,0,0,0,,而且真的准备接收连接请求了
Dialogue: 0,0:34:51.24,0:34:55.62,English,,0,0,0,,And you can give as a parameter of a number
Dialogue: 0,0:34:51.24,0:34:55.62,Chinese,,0,0,0,,你可以提供一个数字类型的参数
Dialogue: 0,0:34:55.62,0:35:03.36,English,,0,0,0,, that says how many a request connection requests are you allowed to queue up
Dialogue: 0,0:34:55.62,0:35:03.36,Chinese,,0,0,0,,设置允许接收连接的最大请求数
Dialogue: 0,0:35:05.10,0:35:09.28,English,,0,0,0,,Before they start getting refused and this is a performance parameter
Dialogue: 0,0:35:05.10,0:35:09.28,Chinese,,0,0,0,,在开始拒绝连接请求之前,这是一个性能参数
Dialogue: 0,0:35:09.78,0:35:11.62,English,,0,0,0,,That if you set it too low then
Dialogue: 0,0:35:09.78,0:35:11.62,Chinese,,0,0,0,,如果你把它设置得太小,那么
Dialogue: 0,0:35:12.08,0:35:17.10,English,,0,0,0,,Unless you have a really fast server people will get failures in their connection requests
Dialogue: 0,0:35:12.08,0:35:17.10,Chinese,,0,0,0,,除非你有一个非常快的服务器,否则很多连接请求会失败
Dialogue: 0,0:35:18.20,0:35:25.86,English,,0,0,0,,But if it's too big then you can you're vulnerable to certain types of sort of denial of service attacks
Dialogue: 0,0:35:18.20,0:35:25.86,Chinese,,0,0,0,,但如果它太大,那么你就可以容易受到某些类型的拒绝服务攻击
Dialogue: 0,0:35:27.82,0:35:30.24,English,,0,0,0,,So again a little bit of an obscure feature
Dialogue: 0,0:35:27.82,0:35:30.24,Chinese,,0,0,0,,所以还是有点晦涩难懂
Dialogue: 0,0:35:31.40,0:35:33.44,English,,0,0,0,,But the result of this is now
Dialogue: 0,0:35:31.40,0:35:33.44,Chinese,,0,0,0,,但结果是现在
Dialogue: 0,0:35:33.86,0:35:35.10,English,,0,0,0,,I'm returning out of this
Dialogue: 0,0:35:33.86,0:35:35.10,Chinese,,0,0,0,,我正在退出这个
Dialogue: 0,0:35:35.70,0:35:40.62,English,,0,0,0,,A file descriptor that the server can use to listen to pick up connection requests
Dialogue: 0,0:35:35.70,0:35:40.62,Chinese,,0,0,0,,服务器可用于监听收集连接请求的文件描述符
Dialogue: 0,0:35:44.54,0:35:49.54,English,,0,0,0,,Okay so let's do a little example of how we might actually do something
Dialogue: 0,0:35:44.54,0:35:49.54,Chinese,,0,0,0,,好的,让我们举一个例子来说明我们可以做些什么
Dialogue: 0,0:35:49.84,0:35:55.24,English,,0,0,0,,And I'm going to use as an example a rather mundane application
Dialogue: 0,0:35:49.84,0:35:55.24,Chinese,,0,0,0,,我将以一个相当普通的应用程序为例
Dialogue: 0,0:35:57.30,0:35:59.10,English,,0,0,0,,Which is an echo server
Dialogue: 0,0:35:57.30,0:35:59.10,Chinese,,0,0,0,,这是一个 echo 服务器
Dialogue: 0,0:35:59.80,0:36:06.06,English,,0,0,0,,An echo server is just one that response back whatever it just received
Dialogue: 0,0:35:59.80,0:36:06.06,Chinese,,0,0,0,, echo 服务器仅仅是将它接收到的信息
Dialogue: 0,0:36:08.22,0:36:11.74,English,,0,0,0,,And the code for it saw in the book on the slides
Dialogue: 0,0:36:08.22,0:36:11.74,Chinese,,0,0,0,,它的代码可以在幻灯片和书中找到
Dialogue: 0,0:36:12.40,0:36:13.20,English,,0,0,0,,We're studying
Dialogue: 0,0:36:12.40,0:36:13.20,Chinese,,0,0,0,,我们正在学习
Dialogue: 0,0:36:17.58,0:36:20.38,English,,0,0,0,,So let me set up this amazing demonstration
Dialogue: 0,0:36:17.58,0:36:20.38,Chinese,,0,0,0,,让我来做个精彩的演示
Dialogue: 0,0:36:32.16,0:36:38.88,English,,0,0,0,,So it's called the echoserveri, i meaning iterative, it can only do one connection request at a time
Dialogue: 0,0:36:32.16,0:36:38.88,Chinese,,0,0,0,,它被称为 echoserveri,i 意思是迭代,它一次只能接收一个连接请求
Dialogue: 0,0:36:40.18,0:36:44.14,English,,0,0,0,,And I'm going to set it up on my laptop here
Dialogue: 0,0:36:40.18,0:36:44.14,Chinese,,0,0,0,,我打算在我的笔记本电脑上安装它
Dialogue: 0,0:36:45.40,0:36:46.26,English,,0,0,0,,And
Dialogue: 0,0:36:45.40,0:36:46.26,Chinese,,0,0,0,,并且
Dialogue: 0,0:36:49.68,0:36:51.10,English,,0,0,0,,I'll connect to that
Dialogue: 0,0:36:49.68,0:36:51.10,Chinese,,0,0,0,,我会连接到那个
Dialogue: 0,0:37:00.24,0:37:08.24,English,,0,0,0,,So you'll see that I told the server use port 15213 which is just a random number I picked out as the port number
Dialogue: 0,0:37:00.24,0:37:08.24,Chinese,,0,0,0,,如你所见,我告诉服务器使用 15213 作为端口号,这个数字是我随机选择的
Dialogue: 0,0:37:09.44,0:37:11.78,English,,0,0,0,,In general if you want to do your own stuff
Dialogue: 0,0:37:09.44,0:37:11.78,Chinese,,0,0,0,,一般来说,如果你想搭建自己的服务器
Dialogue: 0,0:37:11.92,0:37:19.36,English,,0,0,0,,You pick port numbers that are a thousand or greater and they're limited to be unsigned 16-bit
Dialogue: 0,0:37:11.92,0:37:19.36,Chinese,,0,0,0,,你可以选择大于 1000 的整数作为端口号,但仅限于 16 位无符号数表示的范围内
Dialogue: 0,0:37:19.50,0:37:22.62,English,,0,0,0,,So 64 or 65 thousand something some maximum
Dialogue: 0,0:37:19.50,0:37:22.62,Chinese,,0,0,0,,最多有 64000 或者 65000 这么多(最大值是 65535)
Dialogue: 0,0:37:24.14,0:37:26.64,English,,0,0,0,,And as long as and by the way
Dialogue: 0,0:37:24.14,0:37:26.64,Chinese,,0,0,0,,顺便说一下
Dialogue: 0,0:37:27.38,0:37:32.80,English,,0,0,0,,If you're you live in a corporate world your ports will all be locked down and they will not let you do this
Dialogue: 0,0:37:27.38,0:37:32.80,Chinese,,0,0,0,,如果你是在公司,你的端口将被锁定,他们不会让你这样做
Dialogue: 0,0:37:32.92,0:37:37.38,English,,0,0,0,,But in the CMU environment has a pretty permissive policy
Dialogue: 0,0:37:32.92,0:37:37.38,Chinese,,0,0,0,,但在 CMU 环境中有一个非常宽松的政策
Dialogue: 0,0:37:37.76,0:37:44.12,English,,0,0,0,,And actually I'm just doing this on localhost,but you're allowed to at CMU open up ports like you want
Dialogue: 0,0:37:37.76,0:37:44.12,Chinese,,0,0,0,,实际上,我只是在本地主机上这样做,但是你可以在 CMU 打开你想要的端口
Dialogue: 0,0:37:47.94,0:37:50.79,English,,0,0,0,,So anyways I'm going to run this client
Dialogue: 0,0:37:47.94,0:37:50.79,Chinese,,0,0,0,,无论如何我要运行这个客户端了
Dialogue: 0,0:37:51.48,0:37:53.04,English,,0,0,0,,And you'll see that
Dialogue: 0,0:37:51.48,0:37:53.04,Chinese,,0,0,0,,你会看到
Dialogue: 0,0:37:54.54,0:38:01.98,English,,0,0,0,,The server just said I've received a connection from the localhost not very exciting
Dialogue: 0,0:37:54.54,0:38:01.98,Chinese,,0,0,0,,服务器说我收到了本地主机的连接,这很正常
Dialogue: 0,0:38:02.52,0:38:06.12,English,,0,0,0,,And I've received it from port 55457
Dialogue: 0,0:38:02.52,0:38:06.12,Chinese,,0,0,0,,我从 55457 端口收到了它
Dialogue: 0,0:38:06.26,0:38:08.86,English,,0,0,0,,So this is the part that got assigned to the client
Dialogue: 0,0:38:06.26,0:38:08.86,Chinese,,0,0,0,,所以这是分配给客户端的部分
Dialogue: 0,0:38:09.72,0:38:14.46,English,,0,0,0,,In making the connection the server is using port 15213
Dialogue: 0,0:38:09.72,0:38:14.46,Chinese,,0,0,0,,在建立连接时,服务器使用端口 15213
Dialogue: 0,0:38:14.80,0:38:21.80,English,,0,0,0,,And you recall we talked about last time that a link is defined by the host and port names on either end
Dialogue: 0,0:38:14.80,0:38:21.80,Chinese,,0,0,0,,还记得我们上次谈到过,两端的主机和端口名定义了一个连接
Dialogue: 0,0:38:22.46,0:38:26.54,English,,0,0,0,,And that the client side is usually assigned what they call a ephemeral port
Dialogue: 0,0:38:22.46,0:38:26.54,Chinese,,0,0,0,,并且客户端通常被分配一个叫做临时端口的东西
Dialogue: 0,0:38:26.76,0:38:31.74,English,,0,0,0,,It's meaning it's just assigned a trip for this particular session that I'm going to run
Dialogue: 0,0:38:26.76,0:38:31.74,Chinese,,0,0,0,,意味着这只是一次短暂的对话
Dialogue: 0,0:38:32.58,0:38:36.12,English,,0,0,0,,And now here's the amazing feature of an echo server is that
Dialogue: 0,0:38:32.58,0:38:36.12,Chinese,,0,0,0,,现在我们演示 echoserver 的一个惊人功能
Dialogue: 0,0:38:36.12,0:38:39.98,English,,0,0,0,,You can type in stuff and it will come back to you
Dialogue: 0,0:38:36.12,0:38:39.98,Chinese,,0,0,0,,你可以输入一些内容,它会立即显示出来
Dialogue: 0,0:38:43.58,0:38:44.46,English,,0,0,0,,Because echoing
Dialogue: 0,0:38:43.58,0:38:44.46,Chinese,,0,0,0,,因为有回音
Dialogue: 0,0:38:44.74,0:38:50.50,English,,0,0,0,,So what's happening is I'm sending those meaningful messages to the server
Dialogue: 0,0:38:44.74,0:38:50.50,Chinese,,0,0,0,,那么我把消息发送给服务器发生了什么
Dialogue: 0,0:38:51.30,0:38:54.86,English,,0,0,0,,It's reading them and it's just sending them write back again
Dialogue: 0,0:38:51.30,0:38:54.86,Chinese,,0,0,0,,服务器读取了它们,并把它们返回回来了
Dialogue: 0,0:38:55.14,0:39:00.10,English,,0,0,0,,And then the clients reading that and printing it on the screen
Dialogue: 0,0:38:55.14,0:39:00.10,Chinese,,0,0,0,,然后客户端读取并将其打印在屏幕上
Dialogue: 0,0:39:00.44,0:39:03.10,English,,0,0,0,,It's sort of the minimum function you could imagine
Dialogue: 0,0:39:00.44,0:39:03.10,Chinese,,0,0,0,,这是你能看到的最小功能
Dialogue: 0,0:39:03.64,0:39:06.24,English,,0,0,0,,But you can see that the server is telling you
Dialogue: 0,0:39:03.64,0:39:06.24,Chinese,,0,0,0,,但是你可以看到服务器在告诉你
Dialogue: 0,0:39:06.60,0:39:12.04,English,,0,0,0,,Oh by the way I just received so many bytes,so many bytes,so many bytes,so you can see that it's actually doing something
Dialogue: 0,0:39:06.60,0:39:12.04,Chinese,,0,0,0,,我收到这么多字节,这么多字节,这么多字节,这样你可以看到它实际上在做什么
Dialogue: 0,0:39:14.24,0:39:18.22,English,,0,0,0,,And then if I send end-of-file which is just to Ctrl-D
Dialogue: 0,0:39:14.24,0:39:18.22,Chinese,,0,0,0,,如果我发送文件结束符(EOF)按下 Ctrl-D
Dialogue: 0,0:39:19.76,0:39:28.98,English,,0,0,0,,The client will exit and the server will drop that connection but doesn't show you anything
Dialogue: 0,0:39:19.76,0:39:28.98,Chinese,,0,0,0,,客户端将退出,服务器也将断开连接,但什么都不显示
Dialogue: 0,0:39:29.54,0:39:31.78,English,,0,0,0,,and if I wanted to I could start it again
Dialogue: 0,0:39:29.54,0:39:31.78,Chinese,,0,0,0,,如果我想要重来一次,我需要重新运行
Dialogue: 0,0:39:32.30,0:39:37.32,English,,0,0,0,,And you see this time it signed a different ephemeral port to the client
Dialogue: 0,0:39:32.30,0:39:37.32,Chinese,,0,0,0,,你看到这次给客户端分配了一个不同的临时端口
Dialogue: 0,0:39:39.50,0:39:41.46,English,,0,0,0,,But I can not only do one at a time
Dialogue: 0,0:39:39.50,0:39:41.46,Chinese,,0,0,0,,但是我不想同时只请求一次
Dialogue: 0,0:39:41.46,0:39:48.20,English,,0,0,0,,If I tried to now do another connection...I will try and do it
Dialogue: 0,0:39:41.46,0:39:48.20,Chinese,,0,0,0,,如果我现在尝试发起另一次连接
Dialogue: 0,0:39:48.20,0:39:49.48,English,,0,0,0,,If I tried to do it
Dialogue: 0,0:39:48.20,0:39:49.48,Chinese,,0,0,0,,如果我这样做
Dialogue: 0,0:39:51.70,0:39:56.30,English,,0,0,0,,I wouldn't...it would just hang up the the client would hang up it would not be able to get a connection
Dialogue: 0,0:39:51.70,0:39:56.30,Chinese,,0,0,0,,这无法做到 ...... 客户端会挂起,它无法连接到服务器
Dialogue: 0,0:39:56.34,0:40:01.44,English,,0,0,0,,Because the server is only able to handle one client at a time
Dialogue: 0,0:39:56.34,0:40:01.44,Chinese,,0,0,0,,因为服务器一次只能处理一个客户端
Dialogue: 0,0:40:02.70,0:40:09.84,English,,0,0,0,,Okay so that's the what the application does and now let's see how its implemented
Dialogue: 0,0:40:02.70,0:40:09.84,Chinese,,0,0,0,,这就是该应用程序的功能,现在让我们看看它是如何实现的
Dialogue: 0,0:40:13.98,0:40:18.26,English,,0,0,0,,It's pretty simple the nastiness was those functions we've already seen
Dialogue: 0,0:40:13.98,0:40:18.26,Chinese,,0,0,0,,这很简单,这是由我们已经看到过的那些函数实现的
Dialogue: 0,0:40:19.34,0:40:20.66,English,,0,0,0,,So here's the client side of it
Dialogue: 0,0:40:19.34,0:40:20.66,Chinese,,0,0,0,,这是客户端的代码
Dialogue: 0,0:40:21.18,0:40:25.62,English,,0,0,0,,It calls that function I went through called open the client file descriptor
Dialogue: 0,0:40:21.18,0:40:25.62,Chinese,,0,0,0,,它调用了我之前说的 Open_clientfd 的函数
Dialogue: 0,0:40:25.64,0:40:29.40,English,,0,0,0,,Where you gave the name of the host and the name of the port
Dialogue: 0,0:40:25.64,0:40:29.40,Chinese,,0,0,0,,这里提供了主机的名称和端口
Dialogue: 0,0:40:30.28,0:40:32.52,English,,0,0,0,,That you're the server's operating on
Dialogue: 0,0:40:30.28,0:40:32.52,Chinese,,0,0,0,,你是服务器的运作
Dialogue: 0,0:40:32.60,0:40:36.18,English,,0,0,0,,So I said here localhost 15213
Dialogue: 0,0:40:32.60,0:40:36.18,Chinese,,0,0,0,,这里是本地主机 15213
Dialogue: 0,0:40:37.00,0:40:38.76,English,,0,0,0,,And I'm going to initialize a buffer
Dialogue: 0,0:40:37.00,0:40:38.76,Chinese,,0,0,0,,我要初始化一个缓冲区
Dialogue: 0,0:40:39.26,0:40:44.98,English,,0,0,0,,That will be used for communication back and forth for this network communication
Dialogue: 0,0:40:39.26,0:40:44.98,Chinese,,0,0,0,,这将用于此网络通信的来回通信
Dialogue: 0,0:40:46.00,0:40:52.64,English,,0,0,0,,So now echoing means I from a client perspective it will just read in what I've typed
Dialogue: 0,0:40:46.00,0:40:52.64,Chinese,,0,0,0,,从客户端的角度来看, echoing 意味着它只会读取我输入的内容
Dialogue: 0,0:40:53.44,0:41:00.28,English,,0,0,0,,So using gets or fgets to read in what's been typed on the standard input
Dialogue: 0,0:40:53.44,0:41:00.28,Chinese,,0,0,0,,使用 gets 或 fgets 来读取标准输入中输入的内容
Dialogue: 0,0:41:01.50,0:41:05.50,English,,0,0,0,,And as long as that's not null meaning I don't have a end of file
Dialogue: 0,0:41:01.50,0:41:05.50,Chinese,,0,0,0,,只要它不是文件结束符(EOF)就意味着文件没有结束
Dialogue: 0,0:41:06.60,0:41:09.50,English,,0,0,0,,Then I will write it to the client file descriptors
Dialogue: 0,0:41:06.60,0:41:09.50,Chinese,,0,0,0,,然后我将它写入客户端文件描述符
Dialogue: 0,0:41:09.58,0:41:11.38,English,,0,0,0,,So that's how I actually send it over the network
Dialogue: 0,0:41:09.58,0:41:11.38,Chinese,,0,0,0,,这就是我通过网络发送它的方式
Dialogue: 0,0:41:11.50,0:41:19.08,English,,0,0,0,,As I do a write to that a socket that file descriptor represents
Dialogue: 0,0:41:11.50,0:41:19.08,Chinese,,0,0,0,,当我写如套接字也就是文件描述符
Dialogue: 0,0:41:19.68,0:41:23.76,English,,0,0,0,,And then to get something back from the server its response I'll do a read
Dialogue: 0,0:41:19.68,0:41:23.76,Chinese,,0,0,0,,我会读取从服务器上返回的响应
Dialogue: 0,0:41:26.58,0:41:29.08,English,,0,0,0,,And RIO as you call has embedded within it
Dialogue: 0,0:41:26.58,0:41:29.08,Chinese,,0,0,0,,调用的时候 rio 也在里面
Dialogue: 0,0:41:33.64,0:41:37.18,English,,0,0,0,,Now what file descriptor that read is using
Dialogue: 0,0:41:33.64,0:41:37.18,Chinese,,0,0,0,,现在使用读取的文件描述符
Dialogue: 0,0:41:39.22,0:41:45.32,English,,0,0,0,,So I'll say read from that particular file descriptor fill the results back into this buffer
Dialogue: 0,0:41:39.22,0:41:45.32,Chinese,,0,0,0,,从特定文件描述符读取并将结果填充回 buf 缓冲区
Dialogue: 0,0:41:46.06,0:41:49.58,English,,0,0,0,,So I'm using buff both as a sending buffer and a receiving buffer
Dialogue: 0,0:41:46.06,0:41:49.58,Chinese,,0,0,0,,这里使用 buf 作为发送缓冲区和接收缓冲区
Dialogue: 0,0:41:50.62,0:41:55.98,English,,0,0,0,,And then just print that out on to the terminal
Dialogue: 0,0:41:50.62,0:41:55.98,Chinese,,0,0,0,,然后将其输出到终端上
Dialogue: 0,0:41:56.60,0:42:01.24,English,,0,0,0,,And this is by the way the new lines being included in this not just the
Dialogue: 0,0:41:56.60,0:42:01.24,Chinese,,0,0,0,,顺便说一句,不仅包括新行
Dialogue: 0,0:42:01.94,0:42:04.44,English,,0,0,0,,In all of this and that's how it's working
Dialogue: 0,0:42:01.94,0:42:04.44,Chinese,,0,0,0,,也包含所有的这一切,这就是它的工作方式
Dialogue: 0,0:42:05.02,0:42:05.98,English,,0,0,0,,So that's all that happens
Dialogue: 0,0:42:05.02,0:42:05.98,Chinese,,0,0,0,,演示完毕
Dialogue: 0,0:42:05.98,0:42:13.06,English,,0,0,0,,So the main point you see here is that I'm using this a socket like a file to both send and receive
Dialogue: 0,0:42:05.98,0:42:13.06,Chinese,,0,0,0,,这里的要点是使用这个像文件一样的套接字来发送和接收
Dialogue: 0,0:42:17.44,0:42:23.56,English,,0,0,0,,The server code is just a shade more nasty than that to support some of those amazing features it has
Dialogue: 0,0:42:17.44,0:42:23.56,Chinese,,0,0,0,,服务器端的代码比客户端的代码更加令人讨厌
Dialogue: 0,0:42:24.50,0:42:25.56,English,,0,0,0,,So in particular
Dialogue: 0,0:42:24.50,0:42:25.56,Chinese,,0,0,0,,特别是
Dialogue: 0,0:42:26.76,0:42:29.80,English,,0,0,0,,There's a weird struct called a sockaddr_storage
Dialogue: 0,0:42:26.76,0:42:29.80,Chinese,,0,0,0,,有一个奇怪的结构体称为 sockaddr_storage
Dialogue: 0,0:42:30.56,0:42:34.74,English,,0,0,0,,That is just again part of this generic api
Dialogue: 0,0:42:30.56,0:42:34.74,Chinese,,0,0,0,,这只是这个通用 api 的一部分
Dialogue: 0,0:42:34.82,0:42:39.16,English,,0,0,0,,That gives you a buffer of some kind that's big enough to store
Dialogue: 0,0:42:34.82,0:42:39.16,Chinese,,0,0,0,,这给你一个足够大的存储缓冲区
Dialogue: 0,0:42:39.78,0:42:46.70,English,,0,0,0,,A name of a host is what I'm going to use it for you