Recent

Author Topic: Lnet dual communication?  (Read 1684 times)

Jake012345

  • Sr. Member
  • ****
  • Posts: 270
  • Knowledge is the key
Lnet dual communication?
« on: April 10, 2020, 05:21:07 pm »
(sorry I'm bad in english and new in here, so may I'm stupid and avoid the answer in other posts)

Hello!

I want to write a program what can communicate with other running.
But I cant figure out how can I send two Integer data with one TcpComponent.

In the program, we have two shapes and we can control the green with mouse.
In the other window we can control the other shape.(we can see it as green).
So they send them Shape1.coordinate to the other and it set to the Shape2.Coor.
How?
The datas always shaking or just one of them arrive. ;(

Handoko

  • Hero Member
  • *****
  • Posts: 5130
  • My goal: build my own game engine using Lazarus
Re: Lnet dual communication?
« Reply #1 on: April 10, 2020, 05:39:20 pm »
Hello Jake012345,
Welcome to the forum.

I'm not very proficient in network programming. But I remember there was a demo program similar to what you said.

https://wiki.freepascal.org/Portal:HowTo_Demos

The demo is in the link above, in Networking section > Game Client and Server.

Jake012345

  • Sr. Member
  • ****
  • Posts: 270
  • Knowledge is the key
Re: Lnet dual communication?
« Reply #2 on: April 10, 2020, 06:24:32 pm »
Sorry, It's very useful in the future :), but at the moment I'm a beginner of programming %).
Now enough for me a trick to compress the two integer to string and turn back on the other side.
(sure i must know which is wich)
Maybe run more sockets and send messages on it. (if more than one socket can connect to client)
« Last Edit: April 10, 2020, 06:34:10 pm by Jake012345 »

af0815

  • Hero Member
  • *****
  • Posts: 1288
Re: Lnet dual communication?
« Reply #3 on: April 10, 2020, 07:25:05 pm »
look for IntToStr this is the function to convert a integer to string.
regards
Andreas

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Lnet dual communication?
« Reply #4 on: April 10, 2020, 08:08:57 pm »
You can make little helper routines for converting coordinates to a string and back, such as
Code: Pascal  [Select][+][-]
  1. uses SysUtils, Types;
  2.  
  3. function PointToString(aPoint: TPoint; aSeparator: Char): String;
  4. begin
  5.   Result := Format('%d%s%d', [aPoint.X, aSeparator, aPoint.Y]);
  6. end;
  7.  
  8. function StringToPointOK(const aString: String; out Point: TPoint;
  9.   aSeparator: Char): Boolean;
  10. var
  11.   p: SizeInt;
  12. begin
  13.   Point := Default(TPoint);
  14.   p := Pos(aSeparator, aString);
  15.   Result := (Length(aString) > 2) and (p > 1);
  16.   if Result then
  17.     Result := TryStrToInt(Copy(aString, 1, p-1), Point.X) and
  18.               TryStrToInt(Copy(aString, p+1, MaxInt), Point.Y);
  19. end;
                         

Jake012345

  • Sr. Member
  • ****
  • Posts: 270
  • Knowledge is the key
Re: Lnet dual communication?
« Reply #5 on: April 10, 2020, 08:12:30 pm »
I know but i dont know how can i use it...

messagout:=('x'+IntToStr(cursor.x)+'y'+IntToStr(cursor.y));
sendmessage(messageout,socket);
AND
getmessage(messagein,socket);
and here?
For example I have it now:
'x124y33'
How can i cut '124' to use the StrToInt and cut the '33' while I know what number is which?

Jake012345

  • Sr. Member
  • ****
  • Posts: 270
  • Knowledge is the key
Re: Lnet dual communication?
« Reply #6 on: April 10, 2020, 08:15:21 pm »
thanks!
I'm trying it out!

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Lnet dual communication?
« Reply #7 on: April 10, 2020, 08:19:30 pm »
Code: [Select]
sendmessage(PointToString(shape1.BoundsRect.TopLeft), socket);
...var

s: String;  pt: TPoint;
...
getmessage(s, socket);
if StringToPointOK(s, pt) then
begin
  Shape2.Top := pt.Y; 

  Shape2.Left := pt.X;
end;
« Last Edit: April 10, 2020, 08:21:30 pm by howardpc »

Jake012345

  • Sr. Member
  • ****
  • Posts: 270
  • Knowledge is the key
Re: Lnet dual communication?
« Reply #8 on: April 11, 2020, 10:12:35 am »
Thank you!
But my code is probably wrong... :(
The StrToPoint working but the shape2 don't move.


Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls,
  9.   lNetComponents, lNet, Types;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     Button1: TButton;
  17.     Button2: TButton;
  18.     Edit1: TEdit;
  19.     Label1: TLabel;
  20.     Label2: TLabel;
  21.     LTCPComponent1: TLTCPComponent;
  22.     Shape1: TShape;
  23.     Shape2: TShape;
  24.     Timer1: TTimer;
  25.     procedure Button1Click(Sender: TObject);
  26.     procedure Button2Click(Sender: TObject);
  27.     procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
  28.       Shift: TShiftState; X, Y: Integer);
  29.     procedure LTCPComponent1Accept(aSocket: TLSocket);
  30.     procedure LTCPComponent1Connect(aSocket: TLSocket);
  31.     procedure LTCPComponent1Receive(aSocket: TLSocket);
  32.     procedure Timer1Timer(Sender: TObject);
  33.   private
  34.  
  35.   public
  36.  
  37.   end;
  38.  
  39. var
  40.   Form1: TForm1;
  41.  
  42. implementation
  43.  
  44. {$R *.lfm}
  45.  
  46. { TForm1 }
  47.    var s:string;
  48.        started:boolean;
  49.        x,y:TLSocket;
  50.        buffer:TLSocket;
  51.        ptout:Tpoint;
  52.        ptin:TPoint;
  53.        p:TPoint;
  54.        messagein:String;
  55.  
  56.  
  57. function PointToString(aPoint: TPoint; aSeparator: Char): String;
  58. begin
  59. Result := Format('%d%s%d', [aPoint.X, aSeparator, aPoint.Y]);
  60. end;
  61.  
  62. function StringToPointOK(const aString: String; out Point: TPoint;
  63.   aSeparator: Char): Boolean;
  64.   var p: SizeInt;
  65. begin
  66.    Point := Default(TPoint);
  67.    p := Pos(aSeparator, aString);
  68.    Result := (Length(aString) > 2) and (p > 1);
  69.    if Result then
  70.      Result := TryStrToInt(Copy(aString, 1, p-1), Point.X) and
  71.      TryStrToInt(Copy(aString, p+1, MaxInt), Point.Y);
  72. end;
  73.  
  74. procedure TForm1.LTCPComponent1Accept(aSocket: TLSocket);
  75. begin
  76.   LTCPComponent1.SendMessage('start',buffer);
  77.   end;
  78.  
  79. procedure TForm1.LTCPComponent1Connect(aSocket: TLSocket);
  80. begin
  81.   LTCPComponent1.SendMessage('start',buffer);
  82. end;
  83.  
  84. procedure TForm1.LTCPComponent1Receive(aSocket: TLSocket);
  85. begin
  86.   LTCPComponent1.GetMessage(s,buffer);
  87.   if s='start' then started:=true;
  88.  if started=true then begin
  89.  
  90.   LTCPComponent1.GetMessage(messagein,x);
  91.  
  92.  end;
  93.  
  94. end;
  95.  
  96. procedure TForm1.Timer1Timer(Sender: TObject);
  97. begin
  98.   if started=true then begin
  99.  
  100.   Form1.Shape1.Left:=(p.x)-(Shape1.Width div 2);
  101.   Form1.Shape1.Top:=(p.y)-(Shape1.Height div 2);
  102.  
  103.   ptout.x:=Shape1.Left;
  104.   ptout.y:=SHape1.Top;
  105.   LTCPComponent1.sendmessage(PointToString(ptout,'-'),x);
  106.  
  107.   if StringToPointOK(messagein,ptin,'-') then
  108. begin
  109.   Shape2.Top := ptin.Y;
  110.  
  111.   Shape2.Left := ptin.X;
  112. end;
  113.  
  114.   end;
  115. end;
  116.  
  117. procedure TForm1.Button1Click(Sender: TObject);
  118. begin
  119.   LTCPComponent1.Host:=Edit1.Text;
  120.   LTCPComponent1.Listen(22,'sad');
  121. end;
  122.  
  123. procedure TForm1.Button2Click(Sender: TObject);
  124. begin
  125.   LTCPComponent1.Connect(Edit1.Text,22);
  126. end;
  127.  
  128. procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
  129.   Shift: TShiftState; X, Y: Integer);
  130. begin
  131.   p:=ScreenToClient(Mouse.CursorPos);
  132. end;
  133.  
  134. end.
  135.  

Jake012345

  • Sr. Member
  • ****
  • Posts: 270
  • Knowledge is the key
Re: Lnet dual communication?
« Reply #9 on: April 11, 2020, 10:37:19 am »
And I have an other question:

Can I make more connection between two programs with one component?
(Just because I plan to write a little bit more comlicated program with lots of data types)

 

TinyPortal © 2005-2018