Solved

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

Posted on 2009-04-04
11
1,371 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Intraweb submit form as a POST request 4 259
Delphi Mdi application Child forms get behind control 7 151
LAN or WAN ? 11 81
CheckListBox usage 3 48
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
A short film showing how OnPage and Connectwise integration works.

930 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

10 Experts available now in Live!

Get 1:1 Help Now