Recent

Author Topic: Creating a p2p network  (Read 36581 times)

Giu

  • Full Member
  • ***
  • Posts: 131
Re: Creating a p2p network
« Reply #15 on: January 07, 2014, 08:59:42 am »
As. Taaz sais, you must open port in router and redirect to your local ip. You just need to do this, where connection should be established (ex: where server resides  not clients)

But this problem, you have it with Lazarus and whatever language you use to develop your application. It's not a Lazarus problem, it's a "network" problem.

You can avoid NAT, using an intermediate application. Just like Teamviewer for example does. Teamviewer don't need NAT because all communications first goes by its servers.

For this, you need a "bridge" app, communicating with all clients. I mean. Imagine I want to connect to your server. Your server is connected to the bridge, waiting commands. My client connect to the "bridge app", and I tell it, I want to connect to your server. The  "bridge " then pass  communications to your server, and send and receive all communications.  This way, you don't need to do UPNP, NAT and so on, but needs a public server for communication between clients purposes .

I hope I explained correctly.

EDITES:::
Quote
If i reconfigure my ports and use my machine as a server, the clients will need to reconfigure their router too?

No. You just need to reconfigure router where connection "destination" needs to be established. Where server is.

When you try to send a communication on a internet IP, normally  you find a router. What NAT does, is to tell router what to do with the packets received under some ports. If this is not configured  routers don't know what to do with this information  because you can have multiple clients connected to a router, and router can't send all this information to all clients. With NAT you say to router.."hey, communication from port 2345 should go to local ip 192.168.1.3".
« Last Edit: January 07, 2014, 09:05:57 am by Giu »

zeljko

  • Hero Member
  • *****
  • Posts: 1870
    • http://wiki.lazarus.freepascal.org/User:Zeljan
Re: Creating a p2p network
« Reply #16 on: January 07, 2014, 09:10:16 am »
Well, im learning more aboyt networkig development in the last 24 hours that what i learned in all my life.

Can a "tunel" be created using lazarus? Any library? Im actually trying with devart secure bridge.

Yes you can create remote "tunnel" by using libssh2 (I'm using such solution for my apps on linux and mac, haven't tried on windows, but I'm sure that it'll work if libssh2.dll exists) (like ssh with -R option).



torbente

  • Sr. Member
  • ****
  • Posts: 325
    • Noso Main Page
Re: Creating a p2p network
« Reply #17 on: January 07, 2014, 08:00:46 pm »
Ok, lets me see if i understood.

- If i set my IP to work as a server, i can distribute my app so others can connect as clients, right?

If this is correct, i could design the app to be able to work in 2 diferent ways, as a server or client, depending of what the users decides.

Lets make this readable:

- My program starts
- Connects to a known server
- The server knows my external IP and send it to me.
- I try to start my own server; if i had the port redirected, it will works. Now, i can receive incoming conexions meanwhile im still connected to another server. If i cant start my own server im still conected to the network as client.

Is this idea ok? Or did i miss something?

Another thing: AFAIK the cumber of incoming connections is... acording to my system limits. Can someone tell me a reasonable number to do not freeze my PC?

Thanks to you all guys for your time.
Noso Cryptocurrency Main Developer
https://github.com/DevTeamNoso/NosoWallet

jarto

  • Full Member
  • ***
  • Posts: 106
Re: Creating a p2p network
« Reply #18 on: January 07, 2014, 08:53:32 pm »
- My program starts
- Connects to a known server
- The server knows my external IP and send it to me.
- I try to start my own server; if i had the port redirected, it will works. Now, i can receive incoming conexions meanwhile im still connected to another server. If i cant start my own server im still conected to the network as client.

Sure, you can do all that with Lazarus. If you can't do it with Lazarus, you can't do it with any other programming language.

If you have problems to connect from an external ip to your server program, it can be because of these reasons:

1. The operating system itself may want to prevent you from creating listening sockets. For example, in Linux that usually requires superuser rights. If you don't have the necessary rights, there will be an error when you try to create that listening socket in your code.

2. Some other program may already be listening to the same port. For example, you may have a web server listening on port 80. If so, your program can't listen on the same port at the same time.

3. Firewall. Even if you create a program that acts as a server, you need to tell the firewall to allow external computers to connect to it. Because your computer is probably behind a NAT firewall, you need to tell the router to allow the connection and to forward it to a certain port on your computer. Let's assume that your external ip is 112.221.123.121 and your computer's ip-address behind the router is 192.168.0.30 and you listen on port 6610. Then you need to create a NAT rule on your router to forward all traffic from port 6610 to 192.168.0.30:6610.

If you want to create a program that changes those firewall rules itself, well, don't bother thinking about it. The firewalls exist for a reason and they are meant to be hard to circumvent. The best you can do is to instruct your users to create the rules themselves if they want to use your program.

And to be clear, the firewall usually isn't that picky about outgoing connections (your computer accessing a service on another computer on the internet). It's picky about external computers trying to access a service on your computer.

Giu

  • Full Member
  • ***
  • Posts: 131
Re: Creating a p2p network
« Reply #19 on: January 07, 2014, 09:06:31 pm »
Ok, lets me see if i understood.

- If i set my IP to work as a server, i can distribute my app so others can connect as clients, right?

Yes.

Quote
If this is correct, i could design the app to be able to work in 2 diferent ways, as a server or client, depending of what the users decides.

Of course, this is your decission. You can parametrize this from UI or with a INI file for example.

Quote
Lets make this readable:

- My program starts
- Connects to a known server
- The server knows my external IP and send it to me.
- I try to start my own server; if i had the port redirected, it will works. Now, i can receive incoming conexions meanwhile im still connected to another server. If i cant start my own server im still conected to the network as client.

Is this idea ok? Or did i miss something?

I lost in 3rd step. Why the client connects to another server instead to connect just to you? If you have another server, connect all clients to the same. You don't need to act as a server too.

Quote
Another thing: AFAIK the cumber of incoming connections is... acording to my system limits. Can someone tell me a reasonable number to do not freeze my PC?

Thanks to you all guys for your time.

For a simple "internal" chat application, I think you don't have to worry about system limits.

arneolav

  • Full Member
  • ***
  • Posts: 197
Re: Creating a p2p network
« Reply #20 on: January 07, 2014, 10:51:13 pm »
This program has been very useful fore me: PFPortChecker

Just key in the actual Ip adress and portnumber.
In a few seconds the Poerchecker tells you if the port is open or not.




Win 11, win64 , Lazarus 4.0RC3
Delphi/DevExpress

torbente

  • Sr. Member
  • ****
  • Posts: 325
    • Noso Main Page
Re: Creating a p2p network
« Reply #21 on: January 07, 2014, 11:28:11 pm »
ok, i entered in my router config and i found the "port forwarding" section

Port Forwarding:  Disable    Enable     
Protocol:    Comment:      Enable
Local IP Address:       Local Port:  -     
Remote IP Address:     Public Port: - 
               Interface:     

i enabled it
protocol: both-tcp-UDP  I selected TCP
localipadress: 10.0.0.35
local port: 6000-6001
remotetipadress - blank
Public port - blank
Interface Any-VcO I selected Any

I start the server on my machine, using 10.0.0.35:6000
Then i start the client from my machine too, conecting to my public IP 121.xxx.xxx.2:6000

I receive the message error: Socket error 10061 Conection refused

What i did wrong?

Noso Cryptocurrency Main Developer
https://github.com/DevTeamNoso/NosoWallet

Giu

  • Full Member
  • ***
  • Posts: 131
Re: Creating a p2p network
« Reply #22 on: January 08, 2014, 12:13:16 am »
ok, i entered in my router config and i found the "port forwarding" section

Port Forwarding:  Disable    Enable     
Protocol:    Comment:      Enable
Local IP Address:       Local Port:  -     
Remote IP Address:     Public Port: - 
               Interface:     

i enabled it
protocol: both-tcp-UDP  I selected TCP
localipadress: 10.0.0.35
local port: 6000-6001
remotetipadress - blank
Public port - blank
Interface Any-VcO I selected Any

I start the server on my machine, using 10.0.0.35:6000
Then i start the client from my machine too, conecting to my public IP 121.xxx.xxx.2:6000

I receive the message error: Socket error 10061 Conection refused

What i did wrong?

- You can't connect to itself using you public IP if your router doesn't accept loopback connections.
- You must specify public port. You can specify both, to NAT port too. Example: I'm running an Apache instance in port 8080 on my computer, and I want to expose to public, but public port will be 81. Then, all connections to my router from outside (PUBLIC port) from 81 port, will be NAT to my local IP address at port 8080. You get the point?

Attached there are an example of my router. My router accept connections to port 35001, and redirects to 192.168.1.11 on port 35001 (private) too.

Regards.

torbente

  • Sr. Member
  • ****
  • Posts: 325
    • Noso Main Page
Re: Creating a p2p network
« Reply #23 on: January 08, 2014, 04:40:45 am »
I  cant configure the port forwarding in my router, i feel like a dummie. (sceenshot attachment) I remember that 10 years ago or more, i did something like this in my old router to be able to play Age of Empires  :)

What else could be?

How i can intercept runtime errors? I think i remember something like IOresult or something like that in turbo pascal 7, but it was many years ago.

Please, help me with this issue. Im working in other aspects of the app meanwhile im able to fix this.

Noso Cryptocurrency Main Developer
https://github.com/DevTeamNoso/NosoWallet

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Creating a p2p network
« Reply #24 on: January 08, 2014, 05:11:47 am »
why don't you do your own research? http://portforward.com/english/routers/port_forwarding/routerindex.htm it took me 2 minutes to get the above link. In your case leave the external IP empty and try to set the interface to the card/interface that connects to the internet.
« Last Edit: January 08, 2014, 05:25:48 am by taazz »
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

torbente

  • Sr. Member
  • ****
  • Posts: 325
    • Noso Main Page
Re: Creating a p2p network
« Reply #25 on: January 08, 2014, 05:33:28 am »
Sorry if you think im a lazy guy  :( Im constantly searching the web before and after i post here. My router is a Ryge WA41R but i cant find a single link referred to how port forward it. The link you just posted  do not contains nothing usefull.
I tried to configure the port forwarding for hours, until my friend (who where runing the client from his own home) get tired and went to the bed.
Im actually trying to figure how i can intercept errors and how i can obtain the local IP. So i think will be very usefull if i keep the thread ordered:

Looking for:
- How i can get the local IP using indy.
- How i can manage errors using indy (for example, when i call TCPServer.connect and there are no host, i want be able to show a message "Unable to connect" in the memo)

Solved:
- Create a client and a server for simple text exchange using Indy.

I will share the code for a server&client app when im able to do it.
Noso Cryptocurrency Main Developer
https://github.com/DevTeamNoso/NosoWallet

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Creating a p2p network
« Reply #26 on: January 08, 2014, 06:21:53 am »
Sorry if you think im a lazy guy  :( Im constantly searching the web before and after i post here. My router is a Ryge WA41R but i cant find a single link referred to how port forward it. The link you just posted  do not contains nothing usefull.
I tried to configure the port forwarding for hours, until my friend (who where runing the client from his own home) get tired and went to the bed.
Im actually trying to figure how i can intercept errors and how i can obtain the local IP. So i think will be very usefull if i keep the thread ordered:

Looking for:
- How i can get the local IP using indy.

http://stackoverflow.com/questions/15071564/how-to-get-local-ip4-address-using-indy


- How i can manage errors using indy (for example, when i call TCPServer.connect and there are no host, i want be able to show a message "Unable to connect" in the memo)

its a sum of exceptions and events fired eg onstatus onconnect etc. so using something along the lines of
Code: [Select]
  try
    IdTCPClient1.Connect;
  except
    on E : EIdConnectTimeout do begin
      // server did not respond might be down or out of reach.
    end;
    on E:EIdException do begin
      //general Indy exception handler
    end;
  end;

Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

torbente

  • Sr. Member
  • ****
  • Posts: 325
    • Noso Main Page
Re: Creating a p2p network
« Reply #27 on: January 08, 2014, 11:05:24 am »
I received an error, "GStack" and "IPv4ToDWord" as not found identifiers. I supposse i need include the required unit in "uses", but i dont know which one is required  %)

As for error management, indy handles any error like an exception? Are there a complete list of posible errors listed ?

EDIT: Sorry if i have too many questions :(

Im trying to call the procedure to start the TCP server from another form(unit), but i cant find the way since the procedure is tform2.startserver and i dont know how i could make it public. If i call "startserver" or "tform2.startserver" dont work. Any idea?
« Last Edit: January 08, 2014, 11:44:11 am by torbente »
Noso Cryptocurrency Main Developer
https://github.com/DevTeamNoso/NosoWallet

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Creating a p2p network
« Reply #28 on: January 08, 2014, 12:32:27 pm »
grep (aka search in files) the indy folders to find the units that use or declare the missing keywords.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

JD

  • Hero Member
  • *****
  • Posts: 1910
Re: Creating a p2p network
« Reply #29 on: January 08, 2014, 01:18:44 pm »
"IPv4ToDWord" error ------> add IdGlobal to your uses clause
"GStack" error            ------> add IdStack to your uses clause

All Indy exceptions are of the type EIdXXXXXXX.

JD
Linux Mint - Lazarus 4.0/FPC 3.2.2,
Windows - Lazarus 4.0/FPC 3.2.2

mORMot 2, PostgreSQL & MariaDB.

 

TinyPortal © 2005-2018