Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -
i use delphi xe2 , indy10 udp protocol. can't receive server echo on client side if use receivebuffer method. got "socket error # 10040" although send small echo message server client. console application illustrate problem below. in advance.
program project1; {$apptype console} {$r *.res} uses system.sysutils, idglobal, idbasecomponent, idcomponent, idsockethandle, idudpclient, idudpserver, idudpbase, idstack; type tudp_serv = class(tidudpserver) procedure udpsvudpread(athread: tidudplistenerthread; adata: tidbytes; abinding: tidsockethandle); end; var udpserver: tudp_serv; udpcl: tidudpclient; bsnd, brcv: tbytes; s: string; k: integer; //============================================================================== procedure tudp_serv.udpsvudpread(athread: tidudplistenerthread; adata: tidbytes; abinding: tidsockethandle); begin writeln(' server read: ' + tohex(adata, length(adata))); abinding sendto(peerip, peerport, adata); end; //============================================================================== begin try udpserver := tudp_serv.create; udpserver begin onudpread := udpsvudpread; defaultport := 20001; buffersize := 2048; threadedevent := true; active := true; if active writeln('server started on port: ' + inttostr(defaultport)); end; udpcl := tidudpclient.create; udpcl begin buffersize := 2048; host := '127.0.0.1'; port := 20001; end; setlength(bsnd, 5); bsnd[0] := $31; bsnd[1] := $0; bsnd[2] := $33; bsnd[3] := $0; bsnd[4] := $0; repeat writeln(' client send: ' + tohex(bsnd, length(bsnd))); udpcl sendbuffer(host, port, bsnd); sleep(100); try k := udpcl.receivebuffer(brcv, 10); if k > 0 writeln(' client read: ' + tohex(brcv, length(brcv))); except on e: exception begin writeln(format(' client read err: %s',[e.message])); end; end; readln(s); until s <> ''; except on e: exception begin writeln(e.classname, ': ', e.message); readln(s); end; end; end.
screen output:
server started on prot: 20001 client send: 3100330000 server read: 3100330000 client read err: socket error # 10040 message long.
10040 wsaemsgsize
, means buffer tried receive smaller actual size of message received.
you not allocating memory brcv
before calling receivebuffer()
, trying receive 0-byte buffer, hense error. need pre-allocate brcv
@ least same size messages, if not larger.
receivebuffer()
not allocate new tbytes
each received message. have allocate buffer beforehand , receivebuffer()
merely fill in, returning how many bytes received it.
Comments
Post a Comment