Solved

Delphi 2009 - IdTCPServer1.Execute +Winsock.send = nothing

Posted on 2009-04-04
11
1,363 Views
Last Modified: 2013-11-23
I've done some searching on the web, and i can't seem to find any answer to the problem i have.
The client is made in c++ using winsock send & recieve.

The server is done with Indy IdTCPServer component.
When the winsock client connects, IdTCPServer get knowledge of it.
However, when the winsock client sends a chararray with the cmd send, nothing gets parsed in IdTCPServer.Execute.
When using Acontext.Connection.IOHandler.ReadLn;
Is it known that Indy vs winsock base does not work? And does anyone know why the string never gets to the Execute part?

Kind Regards

Jocke

TClientinfo = class(TIdContext)

 public

  IP: String;

  procedure SendResponse(const Clientip: String;

  const AResponse: String);

 end;

 

procedure TForm1.IdTCPServer1Execute(AContext: TIdContext);

var

clientcmd,checkcmd,from:string;

begin

//the request from the client is received in the request var

clientcmd:=Acontext.Connection.IOHandler.ReadLn;

checkcmd:=clientcmd;

from:= acontext.Connection.Socket.Binding.PeerIP;

  //Check thecommand the client sent

  if checkcmd='thisisthecorrectstring' then

  begin

    //Send the response to the client...

    TClientinfo(AContext).SendResponse(sentfrom,StrTobase64('thisistheanswer'));

    TClientinfo(AContext).Connection.Disconnect;

      end

  else

  if (checkcmd<>'thisisthecorrectstring') and (length(checkcmd) <> 18) then

  begin

  TClientinfo(AContext).Connection.Disconnect;

  failedtries := failedtries + 1;

 

  end

  else

  if (checkcmd<>'thisisthecorrectstring') and (length(checkcmd) = 18) then

  begin

  TClientinfo(AContext).Connection.Disconnect;

  failedtries := failedtries + 1;

  end

  else

  begin

  TClientinfo(AContext).Connection.Disconnect;

  failedtries := failedtries + 1;

  end;

  failedlabel.Caption := 'Failed Attempts: '+ IntToStr(failedtries);

 

 

end;

 

procedure TClientInfo.SendResponse(const Clientip: String;

const AResponse: String);

begin

              Connection.IOHandler.WriteLn(AResponse);

end;

Open in new window

0
Comment
Question by:fjocke
  • 7
  • 4
11 Comments
 
LVL 3

Author Comment

by:fjocke
ID: 24070079
Update: Found out that cmd:=AContext.Connection.IOHandler.ReadLn(CHAR0, 1, -1, TIdencoding.en8bit);
seems to work.
However, is this really the best way?
0
 
LVL 17

Expert Comment

by:TheRealLoki
ID: 24072570
ReadLn;
 expects a CR/LF pair as a delilmiter
looks to me like the c++ code isn't sending this.
So what does the c++ code send ?
Otherwise, the other method is to read the incoming data into a buffer, and then check that when you have enough characters...

0
 
LVL 3

Author Comment

by:fjocke
ID: 24078522
var
 MemoryArray: array[0..25] of AnsiChar;  
begin
........
somecode
......
FillChar(MemoryArray, SizeOf(MemoryArray), 0);
  str := 'thisisacommand';
  Move(str[1], MemoryArray, 25);
 send(ClientSocket,MemoryArray,SizeOf(MemoryArray),0);
end;

Something like that, my translation skills arent the best, but almost.
0
 
LVL 17

Accepted Solution

by:
TheRealLoki earned 500 total points
ID: 24093454
changing your code from
 clientcmd:=Acontext.Connection.IOHandler.ReadLn;
to
  clientcmd:=Acontext.Connection.IOHandler.ReadLn(#0);

should be all you need to do, and yes, this is the correct way to do things

The client app has chosen to use a zero byte as the indicator of end of data instead of a Carriage Return/Linefeed pair (CR/LF)
This is perfectly valid.
There are many protocols based on #0 and a lot of the common protocols use things such as a single  dot "." as an indicator of end of data

0
 
LVL 3

Author Comment

by:fjocke
ID: 24097433
If it's not to much asked, would there be possible to get an alternative way to handle the Execute-part for the TCPServer?
Since its 500 points im giving away.
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 17

Expert Comment

by:TheRealLoki
ID: 24105374
I suppose so.
However, since you answered your own question, you wouldn't really have to give away any points.

Tell me everything you need it to do
e.g. is there a process flow of login/ command A/ Command B
or is it just the same "type" of command coming in each time?
If it's just the 1 type of command, then what you have is all you need.

How many connections will there be?
The more info you can give, the more likely I (or another expert) can give you a suitable solution on the first attempt ;-)

0
 
LVL 3

Author Comment

by:fjocke
ID: 24106367
There are about max 500 people that will connect the same time, it's an auth application which has a client that will send "requeststring" the program shall then log the connection, send a "answerstring" back. Log that action and then disconnect. If the requeststring got a length that differs from the one the server application expects, it should also log that, and disconnect the user. Once everything is done, dissconnect the user.
Since there might be a spike of connection at certain times of the day it has to handle multiply connection correctly, oh and yes, the client is done with winsock.

So it has to use the clientcmd:=Acontext.Connection.IOHandler.ReadLn(#0); Thingie

Client is piece of cake, so only server part needed. I'll need a change of perspective :)
0
 
LVL 3

Author Comment

by:fjocke
ID: 24106789
Oh and um, Logging should be done to a Memo :) For deep stuff i use the Intercept.
0
 
LVL 17

Expert Comment

by:TheRealLoki
ID: 24114140
So the server will be an application, and not a service?
(impacts on how I write it)
0
 
LVL 3

Author Comment

by:fjocke
ID: 24114534
Application preferable, i want to have a GUI to look over :)
0
 
LVL 3

Author Closing Comment

by:fjocke
ID: 31566593
Allthough it required some more tweaking, you did send me in the right direction, sorry for not accepting this earlier.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now