Recent

Author Topic: can i make server?  (Read 79992 times)

rvk

  • Hero Member
  • *****
  • Posts: 7063
Re: can i make server?
« Reply #165 on: October 24, 2017, 01:58:13 pm »
is that the only way to make clients on local network to connect the server?
No, local network-clients can connect to your internal network IP.

External internet clients can't connect without you manually forwarding the used port.

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: can i make server?
« Reply #166 on: October 24, 2017, 02:03:21 pm »
can i forward the port with my phone? by hot spotting?

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: can i make server?
« Reply #167 on: October 24, 2017, 02:03:56 pm »
i meant with my hotspot ip address

rvk

  • Hero Member
  • *****
  • Posts: 7063
Re: can i make server?
« Reply #168 on: October 24, 2017, 02:04:44 pm »
can i forward the port with my phone? by hot spotting?
Not sure if (and how) you can forward a port on your phones hotspot to a connected computer.

But your computer gets a local ip from your phone when using hotspot so you would need a program which can forward a port to that ip (same as in your router). But it's probably way harder to do on your phone than your router.
« Last Edit: October 24, 2017, 02:07:50 pm by rvk »

rvk

  • Hero Member
  • *****
  • Posts: 7063
Re: can i make server?
« Reply #169 on: October 24, 2017, 02:11:44 pm »
O, and you probably get a different dynamic IP each time you connect your phone to the internet.
So you would need to inform your external clients your IP has changed otherwise they couldn't even connect.

No, it isn't easy to forward a port for a computer tethered to a phone.
https://superuser.com/questions/561459/android-portforwarding
(and your phone should probably be rooted anyway)

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: can i make server?
« Reply #170 on: October 24, 2017, 11:03:45 pm »
so there's no way for external internet client to connect my server?

rvk

  • Hero Member
  • *****
  • Posts: 7063
Re: can i make server?
« Reply #171 on: October 24, 2017, 11:52:38 pm »
so there's no way for external internet client to connect my server?
Yes, there is. Forward the used port in your router to your local ip address.

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: can i make server?
« Reply #172 on: October 25, 2017, 11:13:33 am »
yeh but that's the only way right?

rvk

  • Hero Member
  • *****
  • Posts: 7063
Re: can i make server?
« Reply #173 on: October 25, 2017, 07:08:30 pm »
yeh but that's the only way right?
Yes (and no).

Maybe UPnP could work for you but I have no experience with this so I can't help you with that.

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: can i make server?
« Reply #174 on: October 25, 2017, 10:39:40 pm »
oh okay can you teach me threads btw? if you have time

rvk

  • Hero Member
  • *****
  • Posts: 7063
Re: can i make server?
« Reply #175 on: October 25, 2017, 11:05:40 pm »
You can look here to learn about threads:
http://wiki.freepascal.org/Multithreaded_Application_Tutorial

Threads are a bit like a TTimer but then different.

You saw that sleep(1000) and RecvString(1000) "hangs up" your program. Even with a TTimer this will hang your program because Sleep(1000) and a TTCPBlockSocket will stop the complete "main thread" of your program.

Normally your program has one execution-path. Every command is executed after the other. Because of an elaborate messaging system and the complexity of the whole Lazarus framework you don't notice this all the time but even after your procedure or functions ends... in that framework, every instruction is after another.

Now comes in threads. Do you have knowledge of multi-core CPUs? If you have one CPU, it can only execute one instruction at a time. If you have 2, 4 or even 8 core CPU, that CPU can process multiple instructions at the same time (each instruction in its own core). Threads work kinda the same way. You can see it as a complete function/unit/class which is executed besides your program (at the same time). Or like executing two different programs simultaneously.

I've attached a small example for a thread (with comments).
The example shows that when you click the button, a thread is made which fills in the memo. Meanwhile you can keep on typing in your TEdit without interruption. You could also do this with a TTimer but the upside of a thread is that is really executed separately (unlike a TTimer). When you have long-running procedures or blocking procedures, you can use threads without problems and with a TTimer you end up in trouble.

The only problem with a thread is that you may not update the components of another thread (because it's executed separately). So in your thread you can't just update the Form1.Memo1. You need to call a special procedure Synchronize to execute a small procedure in the thread of Form1. Essentially thread2 forces a procedure to be executed by (the main) thread1 with Synchronize.

Look at the attached example and see if you understand the comments I placed in it.

(anybody else have a beginners guide to threads lying around ?)

Complete example is also attached.
Code: Pascal  [Select][+][-]
  1. // -----------------------------------------------------------
  2. // this is thread code
  3. // -----------------------------------------------------------
  4.  
  5. type
  6.   TMyTickerThread = class(TThread)
  7.   private
  8.     Ticker: integer;
  9.     procedure ShowTickerInMemo;
  10.   protected
  11.     procedure Execute; override;
  12.   public
  13.     constructor Create(CreateSuspended: boolean);
  14.   end;
  15.  
  16. constructor TMyTickerThread.Create(CreateSuspended: boolean);
  17. begin
  18.   inherited Create(CreateSuspended);
  19.   FreeOnTerminate := True; // this will free the thread automatically
  20.   Ticker := 0;
  21. end;
  22.  
  23. procedure TMyTickerThread.ShowTickerInMemo;
  24. begin
  25.   // this procedure is defined in the sub-thread but executed in the main-thread
  26.   // that's why we can update Form1 components safely
  27.   Form1.Memo1.Lines.Add(IntToStr(Ticker));
  28. end;
  29.  
  30. procedure TMyTickerThread.Execute;
  31. begin
  32.   // this will run until ricker hits 20
  33.   while not Terminated and (Ticker < 20) do
  34.   begin
  35.     // wait half a second
  36.     // because we are in a thread it will not effect the TEdit of the main program thread
  37.     Sleep(500);
  38.     Inc(Ticker);
  39.     // because we are in a thread we can't just update the main-threads components
  40.     // we need to call ShowTickerInMemo "in" the main thread
  41.     // we do this with Synchronize
  42.     Synchronize(@ShowTickerInMemo);
  43.   end;
  44. end;
  45.  
  46. // -----------------------------------------------------------
  47. // -----------------------------------------------------------
  48.  
  49. procedure TForm1.btStartThreadClick(Sender: TObject);
  50. var
  51.   MyThread: TMyTickerThread;
  52. begin
  53.   // This creates the thread and executes it directly
  54.   MyThread := TMyTickerThread.Create(False { suspend = false so start directly });
  55.  
  56.   // MyThread is automatically freed because we set FreeOnTerminate in the thread to true
  57.  
  58. end;
  59.  

You can even click the button multiple times and multiple threads will be made and executed. Try it.

I'm not sure I made the best explanation but I did my best :)

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: can i make server?
« Reply #176 on: October 26, 2017, 01:59:14 pm »
Code: Pascal  [Select][+][-]
  1. type
  2.   TMyTickerThread = class(TThread)
  3.   private
  4.     Ticker: integer;
  5.     procedure ShowTickerInMemo;
  6.   protected
  7.     procedure Execute; override;
  8.   public
  9.     constructor Create(CreateSuspended: boolean);
  10.   end;

why are these separated to private, protected and public? why can't it be all under private?

rvk

  • Hero Member
  • *****
  • Posts: 7063
Re: can i make server?
« Reply #177 on: October 26, 2017, 02:02:31 pm »
why are these separated to private, protected and public? why can't it be all under private?
Because Create is a public constructor (you need to be able to call it from outside the object itself of course), Execute is overridden from the procedure in TThread so needs to be in the same section (protected) and Ticker and ShowTickerMemo can be anywhere but I put them in private because you don't need them outside the class itself.

Read about classes here:
http://wiki.freepascal.org/Object_Oriented_Programming_with_Free_Pascal_and_Lazarus#Class
Quote
private - this means that items defined here are only available or visible to other classes or procedures/function defined within the same program unit (this example is from Graphics, so any of the other classes such as TBitMap, TPicture etc in the same unit can use them). They are essentially local variables (eg FColor, FPenHandleCached) or locally used methods (GetHandle, SetHandle) but can be used or referred to in items declared in the protected or public sections.
protected - this means that items defined here are only available or visible to classes that are descended from this ancestor class, and inherit its properties or methods
public - this means that items defined here are available to any programming unit that includes the current unit in its Uses clause
published - is the same as a public section, but the compiler also generates type information that is needed for automatic streaming of these classes. Often the list of published items appear in the Object Inspector of Lazarus; if there is no published list, all the public fields usually appear in the Object Inspector.

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: can i make server?
« Reply #178 on: October 27, 2017, 12:37:18 pm »
oh okay thank you :)
is it possible to make online game btw?

rvk

  • Hero Member
  • *****
  • Posts: 7063
Re: can i make server?
« Reply #179 on: October 27, 2017, 12:39:28 pm »
is it possible to make online game btw?
You would need to specify what you exactly mean.

If the other gamers need to communicate with your game you'll need a "common" server.
And with that you're facing the same problem that you need to open up that server to the internet.
(i.e. forwarding the correct gaming port to the server)

 

TinyPortal © 2005-2018