annoying problem with my code

Plasmax
Plasmax used Ask the Experts™
on
I am just learning delphi and just trying lots of things to get used to the way API/sockets work
Now I thought I would do something easy, cdrom you can see the effects ect..  but...

everytime I send a command to open the CD rom to the server its fine, but when I close the client the command is flooded to the server and open open open open ect...and I am unable to close the drive it just keeps popping open.
I am using..

frmmain.sinsrv.socket.connections[index].sendtext('2');

note: the number 2 used above is the command used to open the cd that is sent to the server
the sinsrv is listbox the client is tserversocket but its just called client
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
Just to clarify, everthing works fine but when i close my client connection down, the server computer recieves a flood on the command, even tho im not connected.
Geert GOracle dba
Top Expert 2009

Commented:
you'll need to post some code to let us help you

Author

Commented:
Hi, this is most of the code im working with clientside (tserversocket)..

procedure SockHandler; var Size,InputLen:Integer; Cmd,S:String; begin while True do begin InputLen:=recv(Sock,Buf,1024,0); Cmd:=Copy(Buf,0,1); if Cmd = '1' then begin S:='1'; Send(Sock,S[1],1,0); WriteLn('client says thanks for connecting to the help screen.....'); end else if Cmd = '2' then begin mciSendString ('Set cdaudio door open wait', nil, 0, handle) ; WriteLn('opened cd...'); end else if Cmd = '3' then begin mciSendString ('Set cdaudio door closed wait', nil, 0, handle) ; WriteLn('closed cd...'); end end; end;

procedure CreateSock; label LooP; var Sin : TSockAddrIn; S:String; begin WriteLn('listening for client...');

WSAStartUP($0101, WSA);

Close := False;

Sock := socket(2,1,0); Sin.sin_addr.S_addr := inet_addr(PChar(IPCon)); Sin.sin_family := 2; Sin.sin_port := htons(Port);

repeat

If (Winsock.Connect(Sock, Sin, SizeOf(Sin)) = 0) Then Begin connect(Sock, Sin, SizeOf(Sin)); CreateThread(nil , 0, @SockHandler, nil, 0, TID2);

send(Sock,S[1],Length(S),0); End;

until (Close);

WSACleanUP(); end;

begin

createsock;

end.
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

Geert GOracle dba
Top Expert 2009

Commented:
ughuh, you a C programmer ? or a latin-greek programmer ?

ever heard of indentation and delphi syntax ?
mainly 1 instruction on 1 line
no wonder you have a problem with code
you can't see the trees because the forest is in the way

where do you test on the length of info you received ?
you don't seem to clear the buffer ...
and you loop forever, no exit
procedure SockHandler; 
var 
  Size, InputLen: Integer; 
  Cmd, S:String; 
begin 
  while True do 
  begin 
    InputLen := recv(Sock,Buf,1024,0); 
    Cmd := Copy(Buf,0,1); 
    if Cmd = '1' then 
    begin 
      S:='1'; 
      Send(Sock,S[1],1,0); 
      WriteLn('client says thanks for connecting to the help screen.....'); 
    end 
      else if Cmd = '2' then 
    begin 
      mciSendString ('Set cdaudio door open wait', nil, 0, handle) ; 
      WriteLn('opened cd...'); 
    end 
      else if Cmd = '3' then 
    begin 
      mciSendString ('Set cdaudio door closed wait', nil, 0, handle) ; 
      WriteLn('closed cd...'); 
    end 
  end; 
end;

Open in new window

Author

Commented:
Thanks for taking time to answer. However, i pasted your code in and its no change. Well, slight change i should say. Once ii disconnect as before, and it floods my server with the command, as soon as i reclose the cd, it stops, then starts again.

Author

Commented:
Let me know and i can send you my source code in private for server and client.
Oracle dba
Top Expert 2009
Commented:
i didn't change anything to your code,
just did some line breaks and indentation

you are doing a loop, but not resetting the old read buffer
procedure SockHandler; 
var 
  Size, InputLen: Integer; 
  Cmd, S:String; 
begin 
  while True do 
  begin 
    FillChar(Buf, 1024, 0);
    InputLen := recv(Sock,Buf,1024,0); 
    Cmd := Copy(Buf,0,1); 
    if Cmd = '1' then 
    begin 
      S:='1'; 
      Send(Sock,S[1],1,0); 
      WriteLn('client says thanks for connecting to the help screen.....'); 
    end 
      else if Cmd = '2' then 
    begin 
      mciSendString ('Set cdaudio door open wait', nil, 0, handle) ; 
      WriteLn('opened cd...'); 
    end 
      else if Cmd = '3' then 
    begin 
      mciSendString ('Set cdaudio door closed wait', nil, 0, handle) ; 
      WriteLn('closed cd...'); 
    end 
  end; 
end;

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial