Send text with Client Socket from a form that is displayed above another Form that is always at the top

Asked

Viewed 317 times

1

I’m having trouble sending text messages from a Client Socket. The form that sends the text, is always displayed above the form that always stays at the top, when FormOnTop does not exist (is not shown), works well, already when FormOnTop exists and form that sends the text is displayed above the FormOnTop, the sending fails, and the text (message) does not even leave the Client.exe of my software.

Is there any solution to this?

To better understand, I will leave below the code I use:

Form containing Client Socket component:

unit Unit1;

interface

uses
  FormSender;

type
 ......

end;

 var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.CS1Read(Sender: TObject; Socket: TCustomWinSocket);
var
  StrCommand: String;
begin

    StrCommand := Socket.ReceiveText;

  if Pos('<|Command_From_Server|>', StrCommand) > 0 then

   begin

     FormSender.PopupMode:= pmExplicit;
     FormSender.PopupParent:= FormOnTop;
     FormSender.Show;

   end;
end;

end.

Form that sends the text (Formsender) and appears above the form that always stays at the top:

unit FormSender;

interface

uses
  Unit1;

type
 ......

end;

 var
  FormSender: TFormSender;

implementation

{$R *.dfm}

procedure TFormSender.Button1Click(Sender: TObject);
begin

 Form1.CS1.Socket.SendText('<|Hello_To_Server|>' + Edit1.Text + '<<|);

end;

end.

Form that always stays at the top:

NOTE: The Formstyle property is: fsStayOnTop.

unit FormOnTop;

        interface

        uses
          .......

        type
         ......

        end;

         var
          FormOnTop: TFormOnTop;

        implementation

        {$R *.dfm}

    procedure TFormOnTop.FormCreate(Sender: TObject);
    begin
    { Position form }
      Top := 0 ;
      Left := 0 ;

      { Go full screen }
      BorderStyle := bsNone ;
      WindowState  := wsmaximized;
      ClientWidth  := Screen.Width ;
      ClientHeight := Screen.Height;
      Refresh;
      SetForegroundWindow(Handle) ;
      SetActiveWindow(Application.Handle);

    end;

    procedure TFormOnTop.CreateParams(var Params: TCreateParams);
    begin
      inherited;
      if (FormStyle = fsStayOnTop) then begin
        Params.ExStyle := Params.ExStyle or WS_EX_APPWINDOW;
        Params.WndParent := GetDesktopWindow;
      end;
    end;

procedure TFormOnTop.FormShow(Sender: TObject);
begin
 SetWindowPos(FormOnTop.handle, HWND_TOPMOST, FormOnTop.Left, FormOnTop.Top, FormOnTop.Width, FormOnTop.Height, 0);
end;


end.
  • If you comment on the line if Pos('<|Command_From_Server|>', StrCommand) > 0 then something appears?

  • @Guilhermenascimento, this part is ok. The problem is in the sending part: Form1.CS1.Socket.SendText('<|Hello_To_Server|>' + Edit1.Text + '<<|);

  • I guess you don’t understand, you’re sending and it doesn’t look like you’re getting anything is that? So it seems to me that you’re using Pos wrong way. I will try to formulate a response.

  • Actually the sending is not even leaving the Client.exe, the problem is not related to the receipt by Server.exe, the code I showed above refers only to part Client.exe :-). That one '<|Command_From_Server|>' is Server.exe who sends to upload Forms in Client.exe.

  • I understand I’ll read your question again, I think I missed something.

  • When you say that "not even out", you mean that the requested window does not appear?

  • No, not even the content to be sent :-), IE does not send anything.

  • It looks like you’re focused on Button’s possible problem, which means you hit the button and nothing happened, is that it? If it is try to see if my answer makes sense.

Show 3 more comments

2 answers

0

If I understand correctly:

StrCommand := Socket.ReceiveText;

Receives a text sent by:

Form1.CS1.Socket.SendText('<|Hello_To_Server|>' + Edit1.Text + '<<|);

Then note that you wrote <|Hello_To_Server|> in:

Form1.CS1.Socket.SendText('<|Hello_To_Server|>' + Edit1.Text + '<<|);

But here you requested <|Command_From_Server|> for function Pos:

if Pos('<|Command_From_Server|>', StrCommand) > 0 then

As far as I understand the function Pos looks for a string that contains <|Command_From_Server|> and not <|Hello_To_Server|> and therefore what exists within:

   begin

     FormSender.PopupMode:= pmExplicit;
     FormSender.PopupParent:= FormOnTop;
     FormSender.Show;

   end;

Will never be fired.

Maybe you can use TRegEx.IsMatch(Input, 'SUA REGEX AQUI') instead of Pos.

  • No no, you got it wrong even :-) that part you’re telling me about, how I said it works blah. The problem is not related to strings or receiving reference strings between Client.exe and Server.exe.

  • @Edejofreprg1 Friend I think you are not able to explain, see if I understood your code when you click the button it fires TFormSender.Button1Click and the TForm1.CS1Read must get the correct answer?

  • Not that requisition from Button goes is to Server.exe, does not stay in Client.exe no, all this my code above, is relevant only the Client part.

  • Friend leaves a little of the . exe, to speaking of the methods you created, I said nothing of client or server. I want to know, when you run Form1.CS1.Socket.SendText('<|Hello_To_Server|>' + Edit1.Text + '<<|); he sends the command somewhere correct? Then who receives it? It would be the StrCommand := Socket.ReceiveText;? That’s it?

  • Yes, but the StrCommand := Socket.ReceiveText; that gets it on Server.exe. From what I understand you’re thinking it’s all on Client, but blz, it gets clarified there anyway.

  • @Edejofreprg1 No I am not thinking this, on the contrary, I understand of client and server, and I know how communication of this type of approach works, in no time I cited client or server, I want you to focus on the buddy ways so we can understand the problem together. Let’s go to the second part of the problem, you said and the text (message) doesn’t even leave the Client.exe of my software, that message you refer to is this Form1.CS1.Socket.SendText('<|Hello_To_Server|>' + Edit1.Text + '<<|);?

  • Yeah, that’s the one :-)

  • I suggest you make a practical example using my code above to better understand it. Take an example of the internet communication Client <--> Server and adapt with this my code above correctly and you will see the problem :-) I only believe in a possible solution if doing so, in practice, only analyzing scripts without testing in the IDE gets complicated sometimes.

  • @Edejofreprg1 Ok if it is yes as you said then we come to the conclusion that Form1.CS1.Socket.SendText('<|Hello_To_Server|>' + Edit1.Text + '<<|); sends to the server and the server captures using this StrCommand := Socket.ReceiveText; and then he checks with if Pos('<|Command_From_Server|>', StrCommand) > 0 then if the string (command) is valid. If I understand this, tell me what you type inside Edit1.Text? Is a common text?

  • Then he (THE CLIENT) check with if Pos('<|Command_From_Server|>', StrCommand) > 0 then if the string (command) is valid. The string that goes in TEdit1 is a very simple text. Do what I told you above, that you will actually see what the problem is :-)

  • I’ll make a real example here, and send you the link of the example project, for you to take a look.

  • @Edejofreprg1 ok, if the submitted text is simple, then regardless of whether I have understood the problem or not, the IF that you created will never validate the command <|Hello_To_Server|> for he waits <|Command_From_Server|>, therefore Pos will return zero (0). I hope that I have been able to explain, even if it is not the problem you mentioned in fact, it still seems to be a problem "the most".

  • I’ve already decided, see you there for the interest in helping.

  • If you’ve solved the problem of if Pos( then the event FormSender.PopupMode:= pmExplicit;&#xA;FormSender.PopupParent:= FormOnTop;&#xA;FormSender.Show; Must be shooting right now, huh? @Edejofreprg1

  • There was nothing related to this problem, but the important thing is that it is working :-)

  • @Edejofreprg1 So have you figured out the cause of the problem? Formulate an answer so this can help other users.

Show 11 more comments

0

The problem was an excerpt of code that has on "FormOnTop" and had forgotten to post up on the question, which is this:

uses
 Unit1;

    procedure TFormOnTop.TTimer1Timer(Sender: TObject);
    begin
      {if FormExists(FormOnTop) then begin
        Timer2.Enabled:= True;
        Form1.CS1.Socket.SendText('<|OK|>');
        Timer1.Enabled:= False;
      end;}
    end;

    procedure TFormOnTop.TTimer2Timer(Sender: TObject);
    begin
    //ForceForegroundWindow(FormOnTop.Handle);
    end;

was giving conflict for using the same Socket ("CS1") that sending the message and triggering the Timer could know with the click of the message sending button, getting even worse the situation :-).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.