Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2009-04-04
11
Medium Priority
?
1,395 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 17

Accepted Solution

by:
TheRealLoki earned 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

730 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