www.digitalmars.com         C & C++   DMDScript  

D - a new version of socket.d

reply "Christian Lesage" <christian.lesage.1 agora.ulaval.ca> writes:
Hi folks,

I found two bugs in Pavel Minayev's "socket.d", which is, I believe, a great
module to play with if you need TCP/IP in your apps. I'm posting an updated
version of it along with a slightly modified version of Pavel's sample
program. Is there a better place to send files to, so that everybody can
access them?

The first bug was due to the fact that you can't instantiate an object from
a class which has, somewhere in it's ancestry, an abstract method left
undefined. The TCPSocket class inherits from the StreamSocket class which
inherits from the Stream class which has three abstract methods. One of
these, seek(), was left undefined in the sub-classes. I just defined a dummy
seek() method in StreamSocket to circumvent the problem. Should it be done
otherwise?

The second bug concerned the initialization of an IP address (within the IP
class) using a hostname. It was broken because it didn't use correctly the
data structure returned by gethostbyname(). A char** has to be dereferenced
twice...

Not bad for a newbie, uh? I think I'm going to love D.


Thanks to Lars Ivar Igesund for his hint regarding abstract classes.

Christian Lesage


begin 666 socket.d

M4&%V96P (D5V:6Q/;F4B($UI;F%Y978-"B J($-O<'ER:6=H=" H8RD ,C P
M,R!-:6ME(%=Y;FX /&UI:V4N=WEN;D!L.&YI9VAT+F-O+G5K/ T*("H 1FEX
M960 8GD 0VAR:7-T:6%N($QE<V%G92!O;B!T:&4 .'1H(&]F(&]C=&]B97(L

M<V4L(&-O<'DL(&UO9&EF>2P 9&ES=')I8G5T92!A;F0 <V5L;"!T:&ES('-O

M;W-E(&ES(&AE<F5B>2!G<F%N=&5D('=I=&AO=70 9F5E+ T*("H <')O=FED
M960 =&AA="!T:&4 86)O=F4 8V]P>7)I9VAT(&YO=&EC92!A<'!E87( :6X 
M86QL(&-O<&EE<R!A;F0-"B J('1H870 8F]T:"!T:&%T(&-O<'ER:6=H="!N

M;B!S=7!P;W)T:6YG(&1O8W5M96YT871I;VXN("!!=71H;W( ;6%K97, ;F\ 

M=&AI<R!S;V9T=V%R92!F;W( 86YY('!U<G!O<V4N($ET(&ES('!R;W9I9&5D




M4V]C:V5T(&5R<F]R+"!B87-E(&-L87-S(&9O<B!A;&P-"B\O(&]T:&5R(%-O


M"0ES=&%T:6, 8VAA<ELR,%T ;G5M.PT*"0EE<G)N;R ](%=304=E=$QA<W1%
M<G)O<B I.PT*"0ES=7!E<BAM<V< ?B!N=6U;," N+B!S<')I;G1F*&YU;2P 
M(B H97)C.B5D*2(L(&5R<FYO*5TI.PT*"7T-"GT-" T*+R\ =&AR;W=N('=H


M*&US9RD[('T-"GT-" T*+R\ <V5Q=65N8V5D+"!R96QI86)L92P ='=O+7=A
M>2P 8V]N;F5C=&EO;BUB87-E9"!S;V-K970-"B\O(&EN(&9O<FT ;V8 8GET
M92!S=')E86T *%=I;E-O8VLR(%-/0TM?4U1214%-*0T*8VQA<W, 4W1R96%M


M:7-T:6YG('-O8VME=#L

M8FQE(#T =W)I=&5A8FQE(#T 8V]N;F5C=&5D.PT*"7T-" T*"71H:7,H*2![

M960L(&9A;'-E(&]T:&5R=VES90T*"6)I="!C;VYN96-T960H*0T*"7L-" D)


M="P )F)U9BP ,2P 35-'7U!%14LI.PT*"0EI;G0 97)R(#T 5U-!1V5T3&%S

M24].0DE/+" F;F(I.PT*"0EI9B H<F5S=6QT(#T](%-/0TM%5%]%4E)/4BD-
M" D)"7)E='5R;B H97)R(#T](%=304573U5,1$),3T-+*3L-" D)96QS90T*
M"0D)<F5T=7)N("AR97-U;'0A/3 I.PT*"7T-" D-" DO+R!D:7-C;VYN96-T


M"0EC;&]S97-O8VME="AS;V-K970I.PT*"0D)<F5A9&%B;&4 /2!W<FET96%B


M9&%T82!I;B!T:&4 8G5F9F5R+ T*"2\O('-O(&-A;&P =&\ <F5A9" I('=O
M=61L;B=T(&)L;V-K+"!F86QS92!O=&AE<G=I<V4-" EB:70 9&%T85)E8V5I

M"69D7W-E="!F9#L-" D)1D1?6D523R F9F0I.PT*"0E&1%]3150H8V%S="A3
M3T-+150I('-O8VME="P )F9D*3L-" D)<F5T=7)N('-E;&5C=" P+" F9F0L
M(&YU;&PL(&YU;&PL("9T=BD /B P.PT*"7T-" D-" EO=F5R<FED92!U:6YT
M(')E861";&]C:RAV;VED*B!B=69F97(L('5I;G0 <VEZ92D-" DO+R!S:6YC
M92!I;BUB;&]C:W, 87)E(&YO="!I;FAE<FET960L(')E9&5F:6YE('1H96T-

M" D):6YT(')E<W5L=" ](')E8W8H<V]C:V5T+"!C87-T*&-H87(J*2!B=69F

M*3L-" D)96QS92!I9B H<F5S=6QT(#T](%-/0TM%5%]%4E)/4BD-" D)>PT*

M. T*"0D)8V%S92!74T%%0T].3E)%4T54. T*"0D)"61I<V-O;FYE8W0H*3L-
M" D)"0ER971U<FX ,#L-" D)"6-A<V4 5U-!14-/3DY!0D]25$5$. T*"0D)

M875L=#H-" D)"0ET:')O=R!N97< 0V]N;F5C=$5R<F]R*")C;VYN96-T:6]N

M" EO=F5R<FED92!U:6YT('=R:71E0FQO8VLH=F]I9"H 8G5F9F5R+"!U:6YT







M,"TP. T*"6]V97)R:61E('5L;VYG('-E96LH;&]N9R!O9F9S970L(%-E96M0
M;W, <F5L*0T*"2\O('-I;F-E(&EN+6)L;V-K<R!A<F4 ;F]T(&EN:&5R:71E







M"0EI<&%D9'(N861D<F5S<R ](&EP+FEP861D<BYA9&1R97-S.PT*"7T-" D-



M6S%=(#T 8C([(&EP861D<BYB6S)=(#T 8C,[(&EP861D<BYB6S-=(#T 8C0[



M/2!G971H;W-T8GEN86UE*'1O4W1R:6YG>BAH;W-T;F%M92DI.PT*"0EI9B H

M(%1H:7, :7, 8G)O:V5N(#H-"B\O"0D):7!A9&1R+F%D9')E<W, /2 J*&-A
M<W0H=5]L;VYG*BDH:&]S="YH7V%D9'(I*3L-"B\O($$ 8VAA<BHJ(&AA<R!T

M:6=H="!B92!T:&%T(&A?861D<B!I<R!N;W0 9&5F:6YE9"!A<R!H7V%D9')?
M;&ES=%LP72!I;B!W:6YS;V-K+F0L(&%S(&ET('-H;W5L9"!B90T*"0D):7!A
M9&1R+F%D9')E<W, /2 J*&-A<W0H=6EN="HI*&AO<W0N:%]A9&1R7VQI<W1;

M<V4-" D)"71H<F]W(&YE=R!3;V-K971%<G)O<B B8V%N;F]T(')E<V]L=F4 


M;G1F*&)U9BP (B5D+B5D+B5D+B5D(BP :7!A9&1R+F);,%TL(&EP861D<BYB

M(&9U;&P 26YT97)N970 861D<F5S<R H25  *R!P;W)T*0T*8VQA<W, 26YT

M"0T*"71H:7,H*2![(&EP(#T


M8GET92!B,RP =6)Y=&4 8C0L('5S:&]R="!P;W)T*0T*"7L-" D)=&AI<RAN

M:&%R6UT :&]S=&YA;64L('5S:&]R="!P;W)T*0T*"7L-" D)=&AI<RAN97< 


M," N+B!S<')I;G1F*&)U9BP (B4N*G,Z)60B+"!I<"YT;U-T<FEN9R I+"!P

M970Z(%-T<F5A;5-O8VME= T*>PT*"71H:7,H*2![('T-" T*"2\O(&-O;FYE
M8W1S('1O(&=I=F5N($EN=&5R;F5T(&%D9')E<W,-" ET:&ES*$EN=&5R;F5T

M;FYE8W1S('1O(&=I=F5N($EN=&5R;F5T(&%D9')E<W,-" ET:&ES*'5B>71E
M(&(Q+"!U8GET92!B,BP =6)Y=&4 8C,L('5B>71E(&(T+"!U<VAO<G0 <&]R

M" D-" DO+R!C;VYN96-T<R!T;R!G:79E;B!H;W-T;F%M90T*"71H:7,H8VAA

M=&YA;64L('!O<G0I.PT*"7T-" D-" DO+R!C;VYN96-T<R!T;R!G:79E;B!)



M*%!&7TE.150L(%-/0TM?4U1214%-+"!)4%!23U1/7TE0*3L-" D)<V]C:V%D

M:6Y?861D<BYS7V%D9'( /2!I82YI<"YI<&%D9'(N861D<F5S<SL-" D)<V$N
M<VEN7W!O<G0 /2!H=&]N<RAI82YP;W)T*3L-" D):68 *'=I;G-O8VLN8V]N
M;F5C="AS;V-K970L(&-A<W0H<V]C:V%D9'(J*2 F<V$L('-A+G-I>F4I*0T*
M"0D)=&AR;W< ;F5W($-O;FYE8W1%<G)O<B B8V%N;F]T(&-O;FYE8W0B*3L-
M" D)<F5A9&%B;&4 /2!W<FET96%B;&4 /2!T<G5E.PT*"7T-" T*"2\O(&-O
M;FYE8W1S('1O(&=I=F5N($EN=&5R;F5T(&%D9')E<W,-" EV;VED(&-O;FYE
M8W0H=6)Y=&4 8C$L('5B>71E(&(R+"!U8GET92!B,RP =6)Y=&4 8C0L('5S
M:&]R="!P;W)T*0T*"7L-" D)8V]N;F5C="AN97< 26YT97)N971!9&1R97-S

M=&\ 9VEV96X :&]S=&YA;64-" EV;VED(&-O;FYE8W0H8VAA<EM=(&AO<W1N

M061D<F5S<RAH;W-T;F%M92P <&]R="DI.PT*"7T-"GT-" T*+R\ <W1R96%M
M+6)A<V5D('-E<G9E<B!S;V-K970-"F-L87-S(%-E<G9E<E-O8VME= T*>PT*
M"7!R;W1E8W1E9"!33T-+150 <V]C:V5T(#T 8V%S="A33T-+150I,#L-" EP
M<F]T96-T960 8FET(&U?;&ES=&5N:6YG(#T
M<R!S;V-K970 ;&ES=&5N(&9O<B!I;F-O;6EN9R!C;VYN96-T:6]N<PT*"79O




M?0T*"0T*"2\O(')E='5R;G, =')U92!I9B!S;V-K970 :7, ;&ES=&5N:6YG
M+"!F86QS92!O=&AE<G=I<V4-" EB:70 ;&ES=&5N:6YG*"D >R!R971U<FX 
M;5]L:7-T96YI;F<[('T-" T*"2\O('-H=71S(&1O=VX =&AE('-E<G9E< T*
M"79O:60 <VAU=&1O=VXH*0T*"7L-" D):68 *'-O8VME="D-" D)>PT*"0D)
M=VEN<V]C:RYS:'5T9&]W;BAS;V-K970L(%-$7T)/5$ I.PT*"0D)8VQO<V5S
M;V-K970H<V]C:V5T*3L-" D)"6U?;&ES=&5N:6YG(#T

M;G, =')U92!I9B!T:&5R92!A<F4 <&5N9&EN9R!C;VYN96-T:6]N<RP 9F%L

M8R!T:6UE=F%L('1V(#T
M6D523R F9F0I.PT*"0E&1%]3150H<V]C:V5T+" F9F0I.PT*"0ER971U<FX 


M= T*>PT*"7!R:79A=&4 8FET(&U?8VQI96YT5&AR96%D<R ](&9A;'-E.PT*

M?0T*"0T*"2\O(&-R96%T97, =&AE('-O8VME="!A;F0 8FEN9', :70 =&\ 

M"0ES:'5T9&]W;B I.PT*"0ES;V-K970 /2!W:6YS;V-K+G-O8VME="A01E])

M('-A.PT*"0ES82YS:6Y?9F%M:6QY(#T 4$9?24Y%5#L-" D)<V$N<VEN7V%D
M9'(N<U]A9&1R(#T ,#L-" D)<V$N<VEN7W!O<G0 /2!H=&]N<RAP*3L-" D)
M8FEN9"  <V]C:V5T+"!C87-T*'-O8VMA9&1R*BD )G-A+"!S82YS:7IE("D[

M(&-O;FYE8W1I;VYS+"!F:6QL<R!T:&4 9VEV96X-" DO+R!);G1E<FYE=$%D



M"5-T<F5A;5-O8VME="!A8V-E<'0H26YT97)N971!9&1R97-S(&EA*0T*"7L-

M"5-/0TM%5"!R97-U;'0 /2!W:6YS;V-K+F%C8V5P="  <V]C:V5T+"!C87-T
M*'-O8VMA9&1R*BD )G-A+" F<V%L96X *3L-" D):68 *')E<W5L=" ]/2!)

M:6QE9"!T;R!A8V-E<'0 :6YC;VUI;F< 8V]N;F5C=&EO;B(I.PT*"0EI82YI
M<"YI<&%D9'(N861D<F5S<R ]('-A+G-I;E]A9&1R+G-?861D<CL-" D):6$N
M<&]R=" ](&YT;VAS*'-A+G-I;E]P;W)T*3L-" D)4W1R96%M4V]C:V5T(', 




M=$%D9')E<W,I.PT*"7T-" D-" EB:70 8VQI96YT5&AR96%D<R I('L <F5T
M=7)N(&U?8VQI96YT5&AR96%D<SL ?0T*"0T*"79O:60 8VQI96YT5&AR96%D

M97!T<R!A;GD :6YC;VUI;F< 8V]N;F5C=&EO;G, :6X 82!L;V]P(&%N9"!T
M<F%N<V9E<G,-" DO+R!C;VYT<F]L('1O(&]N0V]N;F5C=&EO;B I.R!I9B!C
M;&EE;G14:')E861S(&ES('1R=64L('-T87)T<PT*"2\O(&$ <V5P87)A=&4 


M9')E<W, :6$ /2!N97< 26YT97)N971!9&1R97-S.PT*"0D)4W1R96%M4V]C

M"0EI9B H;5]C;&EE;G14:')E861S*0T*"0D)>PT*"0D)"6-O;FY?9&%T82!C




M=F]I9"!B9U-E<G9E<DQO;W H*0T*"7L-" D)7V)E9VEN=&AR96%D*"9B9U-E

M;G1R;VP =VAE;B!S97)V97),;V]P*"D 86-C97!T<R!A(&YE=R!C;VYN96-T

M;7!L:69I960 =F5R<VEO;B!W:&EC: T*"2\O(&1O97-N)W0 =&%K92!)4"!A



M;"!W:&5N('-E<G9E<DQO;W H*2!A8V-E<'1S(&$ ;F5W(&-O;FYE8W1I;VXL

M<FED96X-" EP<F]T96-T960 =F]I9"!O;D-O;FYE8W1I;VXH4W1R96%M4V]C




M"0ES97)V97( /2!S97)V97(P.PT*"0D)<V]C:V5T(#T <V]C:V5T,#L-" D)
M"6EA("  (" ](&EA,#L-


M8W1I;VY4:')E860H=F]I9"H 87)G*0T*"7L-" D)8V]N;E]D871A(&-N(#T 
M8V%S="AC;VYN7V1A=&$I(&%R9SL-" D)8VXN<V5R=F5R+F]N0V]N;F5C=&EO


M"0E40U!397)V97( <W9R(#T 8V%S="A40U!397)V97(I(&%R9SL-" D)<W9R

M97-S86=E+6]R:65N=&5D('-O8VME=', *%=I;E-O8VLR(%-/0TM?1$=204TI



M971U<FYS('1R=64 :68 =&AE<F4 :7, <F5C96EV960 9&%T82!I;B!T:&4 
M8G5F9F5R+"!S;PT*"2\O(&-A;&P =&\ <F5A9"!F=6YC=&EO;G, =VEL;"!N
M;W0 8FQO8VLL(&9A;'-E(&]T:&5R=VES90T*"6)I="!D871A4F5C96EV960H




M9B!T:&4 ;&%R9V5S="!D871A9W)A;2!T:&ES('-O8VME="!C86X :&%N9&QE

M:6YT(')L96X /2!R97-U;'0N<VEZ93L-" D)9V5T<V]C:V]P="AS;V-K970L
M(%-/3%]33T-+150L(%-/7TU!6%]-4T=?4TE:12P-" D)"6-A<W0H8VAA<BHI
M("9R97-U;'0L("9R;&5N*3L-" D)<F5T=7)N(')E<W5L=#L-
M"B\O(%5$4"!S;V-K970-"F-L87-S(%5$4%-O8VME=#H 1&%T86=R86U3;V-K

M*%!&7TE.150L(%-/0TM?1$=204TL($E04%)/5$]?25 I.PT*"7T-" D-" DO
M+R!B:6YD<R!T:&4 <V]C:V5T('1O('1H92!S<&5C:69I960 <&]R= T*"71H

M<R!T:&4 <V]C:V5T('1O('1H92!S<&5C:69I960 <&]R= T*"79O:60 <&]R

M9F%M:6QY(#T 4$9?24Y%5#L-" D)<V$N<VEN7V%D9'(N<U]A9&1R(#T ,#L-
M" D)<V$N<VEN7W!O<G0 /2!H=&]N<RAP*3L-" D)8FEN9"AS;V-K970L(&-A
M<W0H<V]C:V%D9'(J*2 F<V$L('-A+G-I>F4I.PT*"7T-" T*"2\O('-E;F1S
M(&$ 9&%T86=R86T =&\ 9VEV96X :&]S="P <F5T=7)N<R!A8W1U86P ;G5M

M;"!T;R!B=69F97( <VEZ92$I+ T*"2\O('1H<F]W<R!7<FET945R<F]R(&]N
M(&9A:6QU<F4-" EU:6YT('-E;F14;RA);G1E<FYE=$%D9')E<W, :6$L('9O

M"0ES82YS:6Y?9F%M:6QY(#T 4$9?24Y%5#L-" D)<V$N<VEN7V%D9'(N<U]A
M9&1R(#T

M+"!C87-T*&-H87(J*2!B;&]C:RP <VEZ92P ,"P-" D)"6-A<W0H<V]C:V%D
M9'(J*2 F<V$L('-A+G-I>F4I.PT*"0EI9B H<F5S=6QT(#T](%-/0TM%5%]%
M4E)/4BD-" D)"71H<F]W(&YE=R!7<FET945R<F]R*")S;V-K970 9F%I;&5D
M('1O('-E;F0 9&%T86=R86TB*3L-" D)<F5T=7)N(')E<W5L=#L-
M" DO+R!R96-E:79E<R!A(&1A=&%G<F%M(&%N9"!S=&]R97, :6YF;W)M871I
M;VX 86)O=70 =&AE(&AO<W0-" DO+R!T:&%T('-E;G0 :70L(')E='5R;G, 

M=&AE(&1A=&%G<F%M(&ES(&=R96%T97( =&AA;B!S:7IE(&]F('1H92!S=7!P
M;&EE9"!B=69F97(L( T*"2\O(&5V97)Y=&AI;F< =&AA="!D;V5S;B=T(&9I
M="!I;B!B=69F97( :7, 3$]35"P 86YD(')E='5R; T*"2\O('9A;'5E(&ES
M(&=R96%T97( =&AA;B!S:7IE(&]F('1H92!B=69F97( *&)U="!H;VQD<R!N

M9W5A<F%N=&5E9"!T;R!B92!T:&4-" DO+R!A8W1U86P <VEZ92!O9B!T:&4 
M9&%T86=R86TI.R!T:')O=W, 4F5A9$5R<F]R(&]N(&9A:6QU<F4-" EU:6YT
M(')E8W9&<F]M*$EN=&5R;F5T061D<F5S<R!I82P =F]I9"H 8FQO8VLL(&EN


M<W0H8VAA<BHI(&)L;V-K+"!S:7IE+ T*"0D),"P 8V%S="AS;V-K861D<BHI
M("9S82P )G-A;&5N*3L-" D):68 *')E<W5L=" ]/2!33T-+151?15)23U(I

M12D-" D)"0ER97-U;'0 /2!S:7IE("L ,3L-" D)"65L<V4-" D)"0ET:')O
M=R!N97< 4F5A9$5R<F]R*")S;V-K970 9F%I;&5D('1O(')E8V5I=F4 9&%T
M86=R86TB*3L-" D)?0T*"0EI82YI<"YI<&%D9'(N861D<F5S<R ]('-A+G-I
M;E]A9&1R+G-?861D<CL-" D):6$N<&]R=" ](&YT;VAS*'-A+G-I;E]P;W)T
M*3L-" D)<F5T=7)N(')E<W5L=#L-
M*2!S;V-K971S(&%R92!U<V5D(&UO<F4 9G)E<75E;G1L>2P-"B\O('-O('-U
M8V  86X 86QI87, <V5E;7, ;&]G:6-A; T*86QI87, 4W1R96%M4V]C:V5T
((%-O8VME=#L`
`
end

begin 666 HTTPGET.D
M+RH-"B J(%-I;7!L:7-T:6, 2%144"!C;&EE;G0 <V%M<&QE('=R:71T96X 



M:71S(&1O8W5M96YT871I;VX 9F]R(&%N>2!P=7)P;W-E(&ES(&AE<F5B>2!G
M<F%N=&5D('=I=&AO=70 9F5E+ T*("H <')O=FED960 =&AA="!T:&4 86)O
M=F4 8V]P>7)I9VAT(&YO=&EC92!A<'!E87( :6X 86QL(&-O<&EE<R!A;F0-
M"B J('1H870 8F]T:"!T:&%T(&-O<'ER:6=H="!N;W1I8V4 86YD('1H:7, 

M8W5M96YT871I;VXN("!!=71H;W( ;6%K97, ;F\ <F5P<F5S96YT871I;VYS




M(")W=W<N9&EG:71A;&UA<G,N8V]M(CL-"F-O;G-T(&EN="!P;W)T(#T



M(#T ;F5W($EN=&5R;F5T061D<F5S<RAA9&1R+"!P;W)T*3L-" EP<FEN=&8H

M="!S(#T

M,2XP7&Y<;B(I.PT*"7,N=W)I=&53=')I;F<H<F5Q=65S=%LP("XN(&QE;ETI
M.PT*"0T*"7!R:6YT9B B1&]W;FQO861I;F<B*3L-" D-" EC:&%R6UT 9FEL
M93L-" EI;G0 8V]U;G0 /2 P.PT*"7=H:6QE("AS+F-O;FYE8W1E9" I*0T*

M(#X

M<')I;G1F*")(:70 82!K97DN+BY<;B(L(&9I;&4I.PT*"7=H:6QE("  (6MB
::&ET*"D *2![?2 -" ER971U<FX ,#L-"GT`
`
end
Oct 08 2003
parent Brad Anderson <brad sankaty.com> writes:
 One of
 these, seek(), was left undefined in the sub-classes. I just defined a dummy
 seek() method in StreamSocket to circumvent the problem. Should it be done
 otherwise?
 
 The second bug concerned the initialization of an IP address (within the IP
 class) using a hostname. It was broken because it didn't use correctly the
 data structure returned by gethostbyname(). A char** has to be dereferenced
 twice...
 
Bravo! I was struggling with seek() a few weeks back. I can't think of another way to do it, but I'll let you know how it works in my app... Thanks, Brad
Oct 09 2003