Solved

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

Posted on 2009-04-04
11
1,384 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

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…
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…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
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
Course of the Month9 days, 15 hours left to enroll

624 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