Hello Frank,
You use methods for UDP to receive, and you use TCP. Another problem is
that you use a ShowMessage in the event handler witch call the message
pump, so your ondataavailable can be re-entered if you send next packet
whil the window is still open. Probably this is for test/logging but
then you better use WriteLn.
Something like this (from the top of my head):
//receiving data
procedure TForm1.WSocket1DataAvailable(Sender: TObject; Error: Word);
var
Count: integer;
begin
if error <> 0 then
Exit;
Count := wsocket1.Receive(@MsgRec[WritePtr], SizeOf(MsgRec) - WritePtr);
if Count <= 0 then
Exit;
Inc(WritePtr, Count)
if WritePtr >= SizeOf(MsgRec) then begin
WriteLn(MsgRec.MsgText);
// data is complete, handel it here
// prepare for next packet
WritePtr := 0;
end;
end;
---
Rgds, Wilfried
http://www.mestdagh.biz
Monday, June 27, 2005, 09:05, Frank Wunderlich wrote:
> Hi,
> i trying to send a record with TWSocket in delphi (code below).
> send/receive works well, but when i close the connection to the server.
> OnDataAvailable is fired and i get a message (from my showmessage) with
> strange signs. That means i cast the incoming data to my record an get
> something useless stuff ;>. How can i prevent this?
> //******* the client ********
> //connecting to server
> procedure TForm1.Btn_ConnectClick(Sender: TObject);
> begin
> WSocket1.Proto:='tcp';
> WSocket1.Addr:=Edit_Addr.Text;
> WSocket1.Port:=Edit_Port.Text;
> Wsocket1.Connect;
> end;
> //sending
> var msgrec:Tmsgrec; //msgrec is a simple record-struct with 1 string[255]
> if wsocket1.State=wsConnected then
> begin
> msgrec.MsgText:='my text';
> Wsocket1.Send(@msgrec,sizeof(msgrec));
> end else showmessage('not connected');
> //********* the server *******
> // starting the server
> procedure TForm1.Btn_ListenClick(Sender: TObject);
> begin
> WSocket1.proto:='tcp';
> WSocket1.Addr:='0.0.0.0';
> WSocket1.Port:='50000';
> wsocket1.Listen;
> end;
> //accepting all incomming connections
> procedure TForm1.WSocket1SessionAvailable(Sender: TObject; Error: Word);
> begin
> wsocket1.HSocket:=Wsocket1.Accept;
> end;
> //receiving data
> procedure TForm1.WSocket1DataAvailable(Sender: TObject; Error: Word);
> var msgrec : Tmsgrec;
> Src : TSockAddrIn;
> SrcLen : Integer;
> RemoteAddr: string[15];
> begin
> if error=0 then
> begin
> SrcLen := SizeOf(Src);
> wsocket1.ReceiveFrom(@MsgRec,SizeOf(MsgRec),Src,SrcLen);
> if SrcLen>0 then
> begin
> RemoteAddr:=inet_ntoa(src.sin_addr); //IP ermitteln
> if msgrec.msgtext<>'' then showmessage(msgrec.msgtext);
> end;
> end;
> end;
> i found nothing usefult in google, hope you can help...
> greets frank
> --
> Weitersagen: GMX DSL-Flatrates mit Tempo-Garantie!
> Ab 4,99 Euro/Monat: http://www.gmx.net/de/go/dsl
--
To unsubscribe or change your settings for TWSocket mailing list
please goto http://www.elists.org/mailman/listinfo/twsocket
Visit our website at http://www.overbyte.be