Solved

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

Posted on 2009-04-04
11
1,376 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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
 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Mydac connection data base issue 3 167
Tvertscrollbox like a whatsapp layout 5 36
Tvertscrollbox like a whatsapp layout delete messages 1 24
shape, triangle, dbctrlgrid 3 17
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

830 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